Sum all digits of a number and show the digits separately in Java

I use this site quite a lot, but I never wrote anything. Today I came across a problem whose solution I can’t find.

The problem is what I have int variablewith an unknown number of digits. I am invited to summarize all these numbers, and then print / shown_mechanical_value with all these numbers.

For example, the user enters the number "12345678", and at the end I need to get the message "Numbers 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36". I know how to separate and sum all these numbers, but I don’t know how to save each number, and then show it, because not only do I not know the required number of digits, but I do not know their meanings either.

Usually it is not difficult to do, but for me this is the tricky part, because we have not studied such things as arrays, strings, indexes, etc., we are not allowed to use them. So, I have to do this with the simplest type of code.

So far I have the following:

import java.util.Scanner;
public class Numbers {
    public static void main(String[] args) {
        int number, result;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number:");
        number = sc.nextInt();
        result = 0;
        while (number > 0){
            result = result + (number % 10);
            number = number / 10;
        }
        System.out.println("This is where the message would go.");
    }
}
+4
source share
7 answers
static final Scanner input = new Scanner(System.in);

public static void main(String[] args) {
    int number, result;
    System.out.println("Enter number:");
    number = input.nextInt();
    result = 0;
    System.out.print("The numbers: ");

    //Reverses the number
    int reversedNum = 0;
    while (number != 0) {
        reversedNum = reversedNum * 10 + number % 10;
        number = number / 10;
    }

    //Iterates over the number and prints it out
    while (reversedNum > 0) {
        System.out.print((reversedNum % 10));

        result = result + (reversedNum % 10);
        reversedNum = reversedNum / 10;

        if (reversedNum > 0) {
            System.out.print(" + ");
        }
    }
    System.out.println(" = " + result);
}

This works to print all numbers in the correct order without using arrays and strings.

Example:

Enter number:
12345
The numbers: 1 + 2 + 3 + 4 + 5 = 15
BUILD SUCCESSFUL (total time: 5 seconds)
+2
source

Maybe something like this when you are not using any container. Just concatenate the line every time you find a new number.

    int number, result;
    String finalOutput = "";
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number:");
    number = sc.nextInt();
    result = 0;

    while (number > 0){
        result = result + (number % 10);

        finalOutput += (number % 10);

        number = number / 10;
        if(number > 0)
        {
            finalOutput += " + ";
        }else{
            finalOutput += " = ";
        }
    }
    System.out.println("The numbers " + finalOutput + result);
+1
source

, . :

while (number > 0){
     System.out.print( (number % 10) + " + " );
     result = result + (number % 10);
     number = number / 10;
}

.

+1

. , , , String.valueOf(int) (Integer.toString(int) ), char .

Scanner sc = new Scanner(System.in);

int sum= 0;

System.out.println("Number?");
int num = sc.nextInt();
sc.close();

String countNum = String.valueOf(num);

for(int i = 0; i < countNum.length(); i++) {
   int j = Character.digit(countNum.charAt(i), 10);
   sum += j;
}
System.out.println("The sum is: " + sum);
+1

:

int number,result;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number:");
number = sc.nextInt();
//sum to the result and print last digit and the result
int last = number%10;
result = reverse(number/10);
System.out.print(last + " = "+ (result + last));

, ( ):

private static int reverse(int number) {
    if(number>0){
        int p = number % 10 + reverse(number/10);
        System.out.print(number % 10 + " + ");
        return p;
    }
    return 0;
}
+1

, , replaceAll():

"12345678".replaceAll("\\B", " + ") // returns "1 + 2 + 3 + 4 + 5 + 6 + 7 + 8"

\\B - , (\\B - , ). , , - , .

replaceAll() " + ", , .

+1

( OP).

!

, Google:

String → "12345678".replaceAll( "\ B", "+" ) ➜ 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 - Andreas

Recursive solution user6904265 .

Thanks everyone for the great help. Very grateful. <3

0
source

Source: https://habr.com/ru/post/1661113/


All Articles