This is a Java Program to Compute the Sum of Digits in a given Integer.
Enter any integer number as input. After that we use modulus and division operation respectively to find the sum of digits of number as output.
Here is the source code of the Java Program to Compute the Sum of Digits in a given Integer. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Digit_Sum
{
public static void main(String args[])
{
int m, n, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:");
m = s.nextInt();
while(m > 0)
{
n = m % 10;
sum = sum + n;
m = m / 10;
}
System.out.println("Sum of Digits:"+sum);
}
}
Output:
$ javac Digit_Sum.java $ java Digit_Sum Enter the number:456 Sum of Digits:15
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Apply for Computer Science Internship
- Apply for Java Internship
- Check Programming Books
- Practice BCA MCQs
- Check Java Books