简体   繁体   中英

How to separate two or more digits number and add them separately

I want to do an activity that if I have a certain integer for ex. 456 I want it to separate this integer to 4, 5, 6 and add separately like 4+5+6 and add it until the last result is equal to or less than 9 or to a single digit number.

for example:

4+5+6=15, now 15 is also a 2 digits number now again it separate 15 as 1, 5 and add 1+5 ie 6

So I have to get final result in a single digit number.

How can I achieve this in Java or Android?

This is the following code I have tried:

if(sum > 9)
{
    int numbers = 0;
    int number = sum;
    int[] digits = getDigitsOf(number);

    for (int i = 0; i < digits.length; i++) {
        numbers = (digits[i]);
    }

    tv.setText(numbers);
}

And the function is:

public int[] getDigitsOf(int num)
{        
    int digitCount = Integer.toString(num).length();

    if (num < 0) 
        digitCount--;           

    int[] result = new int[digitCount];

    while (digitCount-- >0) {
        result[digitCount] = num % 10;
        num /= 10;
    }        
    return result;
}

I'm not getting the desired results.

You should have learned recursive programming and also the use of percentage "%" operator to find each digit in a number

 456%10 = 6;// which is the remainder

please use both these and find your answer.

This is an easy program. Try around with both the ideas and the answer will click for you

EDIT: i would prefer such a logic

public static void main(String[] args) {
    int number = 456;
    System.out.println(splitnumberAndSum(number));

}

private static int splitnumberAndSum(int number) {
    int sum = 0;
    int rem;
    while (number>0) {
        rem=number%10;
        sum+=rem;
       number /= 10;
    }
    if(sum>9){
        sum = splitnumberAndSum(sum);
    }
    return sum;
}
String str_no="1235";
String answer="";


 while (check_sum() > 9)
 {
            calc_sum();
 }

    Log.w("final value...",answer); 

public void calc_sum()
{

        int temp=0;
        String temp_str="";

    for (int i=0;i<str_no.length();i++)
        {
                try
                {
                    temp_str=""+str_no.charAt(i);
                    temp=temp+Integer.parseInt(temp_str);
                }
                catch (Exception e) 
                {
                    // TODO: handle exception
                }
        }

        str_no=""+temp;


}

public int check_sum()
{
        int temp=0;
        String temp_str="";
        for (int i=0;i<str_no.length();i++)
        {
            try
            {
                temp_str=""+str_no.charAt(i);
                temp=temp+Integer.parseInt(temp_str);
            }
            catch (Exception e) 
            {
                // TODO: handle exception
            }
        }
        answer=""+temp;
        return temp;
}

Well you can try something like following:-

int var = 0, sum = 0;
System.out.println("Enter an integer:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
    var = Integer.parseInt(br.readLine());
} catch (IOException e) {
    e.printStackTrace();
}
System.out.println("Value of var is: " + var);
do
{
    sum=0;
    while(var>0)
    {
        sum += var%10;
        var = var/10;
    }
    var = sum;
} while(var>10); 
System.out.println("Sum of digits is: " + var);

I hope this helps...

public static void main(String[] args) 
    {
        int num = 656869787;
        String numStr = num+"";
        while(Integer.parseInt(numStr)>9){
            numStr = addDigits(numStr);
        }
        System.out.print(numStr);
    }

    private static String addDigits(String numStr) {
        int sum = 0;
        char[] arr = numStr.toCharArray();
        for(char c : arr){
            sum = sum + Integer.parseInt(c+"");
        }
        return sum+"";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM