Write a java program to accept a number from user and find its sum of digits.
In this tutorial we are going to learn how we can Write a java program to accept a number from user and find its sum of digits.
Program :-
import java.util.*;
class digit
{
int remainder;
int sum;
void sumDigit(int number)
{
while(number!=0)
{
remainder=number%10;
sum=sum+remainder;
number=number/10;
}
System.out.println("Sum of Digit of a number is :"+sum);
}
public static void main(String a[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Any Number :");
int num=sc.nextInt();
digit d=new digit();
d.sumDigit(num);
}
}
Output :-
Comments
Post a Comment