مشكلة2....+ الحل

1- Write a java program that reads an integer between 0 and 1000 and summarize all digits in the integer. For example, if integer is 932, the sum of all digits is 14.
Solution:
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/** * @param args the command line arguments */
public static void main(String[] args) {
//Let user enter input string, then covert it to integer
int x=Integer.parseInt(JOptionPane.showInputDialog("please enter the number"));
int sum=0;
while(x>0) {
//Make a summation of reminders
sum+=x%10;
x=x/10;
} //Show resulted summation
JOptionPane.showMessageDialog(null, "the sum is "+sum);
}
}

0 comments: