Given a number the task is to calculate the sum of digits.
Example: 253. 2+5+3 = 10.
C
import java.util.*;
class SumDigits
{
public static void main(String args[])
{
int n=0,r=0,sum=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
n=sc.nextInt();
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
System.out.println("Sum of digits is "+sum);
}
}C