java 2.6** Summing the digits in an integer

来源:互联网 发布:太仓市司法拍卖淘宝网 编辑:程序博客网 时间:2024/06/09 23:52

例如 : 999

process: sum=9+9+9=27;

例如 : 932

process: sum=9+3+2=14;

 

import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner input=new Scanner(System.in);int number;System.out.println("Enter a number between 0 and 1000: ");number=input.nextInt();int sum=0;while(number!=0){sum=sum+number%10;number=number/10;} System.out.println("The sum of the digits is "+sum);}}


 

0 0
原创粉丝点击