In mathematics, the digit sum of a given integer is the sum of all its digits (e.g. the digit sum of 84001 is calculated as 8+4+0+0+1 = 13).
import java.util.*;
class sum_of_digit
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no.");
int a,r=0,s=0;
a=sc.nextInt();
while(a>0)
{
r=a%10;
s=s+r;
a=a/10;
}
System.out.println("The sum of digit is="+s);
}
}
Output:
Enter the no:
213
The sum of digits is 6
import java.util.*;
class sum_of_digit
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no.");
int a,r=0,s=0;
a=sc.nextInt();
while(a>0)
{
r=a%10;
s=s+r;
a=a/10;
}
System.out.println("The sum of digit is="+s);
}
}
Output:
Enter the no:
213
The sum of digits is 6