Write a program in java to enter
a number and print the sum of its digits.
Answers
Answered by
3
Explanation:
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);
}
}
Answered by
1
Answer:
Java Program to Compute the Sum of Digits in a given Integer
1) public class Digit_Sum.
2) int m, n, sum = 0;
3) Scanner s = new Scanner(System.
4) System. out. print("Enter the number:");
5) m = s. nextInt();
6) while(m > 0)
7) n = m % 10;
8) sum = sum + n;
Similar questions