Write a java program to input any number and find the sum of the digits using while loop.
Answers
Answered by
15
import java .util.*;
class abc
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter a no=");
int n= sc.nextInt();
int temp =n;
int sum= 0;
while(n>0)
{
int r = n%10;
sum= sum+ r;
n=n/10;
}
System.out.println("Sum="+sum );
}
}
Answered by
12
import java .util.Sacnner ;
or
import java.util.*;
class sum
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter a no=");
int n= sc.nextInt();
int temp =n;
int sum= 0;
while(n>0)
{
int d = n%10;
sum= sum+ d;
n=n/10;
}
System.out.println("Sum of the digits ="+sum );
}
}
Similar questions
Math,
5 months ago
English,
5 months ago
Social Sciences,
5 months ago
Chemistry,
10 months ago
English,
10 months ago
Social Sciences,
11 months ago
Math,
11 months ago