7-1 sdut-JAVA-the sum of the six digits
分数 10
全屏浏览
切换布局
作者 马新娟
单位 山东理工大学
Write a program that declares a variable and assigns it a six-digit integer number 912846. Your program should display the sum of the six digits. Use the / and % operators to extract the digits from the number.
Input Format:
No input.
Output Format:
Sum of the digits is: 30
Sample Input:
no input
Sample Output:
Sum of the digits is: 30
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
public class Main
{
public static void main(String [] args)
{
int n=912846;
int sum=0;
while(n>0)
{
sum+=n%10;
n=n/10;
}
System.out.println("Sum of the digits is: "+sum);
}
}