Q:

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

belongs to collection: Java Basic Solved Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java Program to display n Prime Numbers us... >>
<< Write a Java Program to find given number is Palin...