Question: Write a Java program and compute the sum of an integer's digits.
Input Data:
Input an integer: 25
Expected Output
The sum of the digits is: 7 Ans: package learning;
import java.util.Scanner;
public class learnNew {
public static void main(String[] args){
Scanner scan = new Scanner(System.in) ;
int n = 25;
int sum = 0;
while(n > 0) {
int r = n % 10;
sum = sum + r;
n = n/10;
}
System.out.println(sum);
}
}
7
Comments
Post a Comment