Write a Java Program to Find Sum of Digits of given Number

By | 21.01.2017

Write a Java Program to Find Sum of Digits of given Number


 

Sum of digits means add all the digits of entered number.

For example: we take any number like 121. Its sum of all digits is 1+2+1=4.

 

SOURCE CODE ::

import java.util.Scanner;
class SumofDigit
{
public static void main(String[] args) 
{
    int a,no,sum=0;
    Scanner s=new Scanner(System.in);
    System.out.println("Enter any number: ");
    no=s.nextInt();
while(no>0)
{
    a=no%10;
    no=no/10;
    sum=sum+a;
}
    System.out.println("Sum of Digits :"+sum);
}
}

 

OUTPUT ::

 

Enter any number: 
1234
Sum of Digits :10

 

4 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments