Logan f
Logan f

Reputation: 11

Can someone please explain the structure of this while loop?

I need to know why sum=0 was initialized. If it is unnecessary, I need to know a reason why. Thanks.

The original question was: Given an input positive integer, output each digit on its own line, starting with the rightmost digit. Ex: If the input is 935, the output is: 5 3 9

import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      int number, sum=0, r;
      number = scnr.nextInt();
  
  while(number!=0) {
     r=number %10;
     System.out.println(r + "");
     number = number / 10;
  }
  /* Type your code here. */
}

Upvotes: 0

Views: 153

Answers (1)

abhinav bandaru
abhinav bandaru

Reputation: 60

sum = 0 is not necessary because you never use it in the program, why would write a variable that you would never use? It would be like buying a TV but never using it

Upvotes: 1

Related Questions