package myfirstproject;
import java.util.Scanner;
public class whileloop {
public static void main(String[] args) {
// TODO Auto-generated method stub
long n,sum=0;
System.out.println("Enter a number:");
Scanner sc=new Scanner(System.in);
n=sc.nextLong();
while(n!=0)
{
n/=10;
sum+=n%10;
}
System.out.println("The sum of digits of number is " +sum);
}
}
output:
enter a number:
For example if I input 745, it adds the first 2 digits and gives the result as 11. But it is not adding the 3rd digit.