-3

Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, the program should: output the individual digits of 3456 as 3 4 5 6 and the sum as 18, output the individual digits of 8030 as 8 0 3 0 and the sum as 11, output the individual digits of 2345526 as 2 3 4 5 5 2 6 and the sum as 27, and output the individual digits of 4000 as 4 0 0 0 and the sum as 4.

Moreover, the computer always adds the digits in the positive direction even if the user enters a negative number. For example, output the individual digits of -2345 as 2 3 4 5 and the sum as 14.

This is the question I'm having minor difficulties with, the only part I can't figure out is how can I print the single integers in the order that he wants, from what I learned so far I can only print them in reverse. Here's my code:

    import java.util.*;
public class assignment2Q1ForLoop {
   static Scanner console = new Scanner (System.in);
   public static void main(String[] args) {
      int usernum, remainder;
      int counter, sum=0, N; 
      //Asaking the user to enter a limit so we can use a counter controlled loop
      System.out.println("Please enter the number of digits of the integer");
      N = console.nextInt();
      System.out.println("Please enter your "+N+" digit number");
      usernum = console.nextInt();
      System.out.println("The individual numbers are:");
      for(counter=0; counter < N; counter++) {
         if(usernum<0)
         usernum=-usernum;
         remainder = usernum%10 ;
         System.out.print(remainder+" ");
         sum = sum+remainder ;
         usernum = usernum/10;
      }
      System.out.println();
      System.out.println("the sum of the individual digits is:"+sum);
   }
}

4 Answers4

0

You have to storeremainder variables in an array and then print them in the loop from last index to first as shown in this tutorial.

Community
  • 1
  • 1
Jakub Licznerski
  • 1,008
  • 1
  • 17
  • 33
0

You can either store digits in array and then print them, or you can try something like that:

final Scanner console = new Scanner(System.in);

System.out.println("Please enter your number");
final int un = console.nextInt();

long n = un > 0 ? un : -un;
long d = 1;
while (n > d) d *= 10;

long s = 0;
System.out.println("The individual numbers are:");
while (d > 1) {
    d /= 10;
    final long t = n / d;
    s += t;
    System.out.print(t + " ");
    n %= d;
}
System.out.println();

System.out.println("the sum of the individual digits is:" + s);
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
0

An idea would be : convert int to string and write a method

getChar(int index) : String

which gives you for example 4 from 3456 with

getChar(2);

See Java - Convert integer to string

Community
  • 1
  • 1
jsn
  • 71
  • 4
0

Here, I wrote a code for your problem with using a stack. If you want a simple code, you can comment my solution and I will wrote another one.

    Scanner c1 = new Scanner(System.in);

    System.out.print("Enter the number: ");

    int numb = c1.nextInt();

    numb = Math.abs(numb);

    Stack<Integer> digits = new Stack<Integer>();

    while(numb>0){
        int n = numb%10;
        digits.push(n);

        numb = numb/10;
    }
    int sum = 0;
    while(!digits.isEmpty()){
        int n = digits.pop();
        sum+=n;
        System.out.print(n+" ");
    }
    System.out.print(sum);
Basri
  • 1
  • 1