belongs to collection: Java Basic Solved Programs
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
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
SOURCE CODE :
OUTPUT ::
need an explanation for this answer? contact us directly to get an explanation for this answer