java program to find sum of each digit and repeat the process until users exit:
package javaapplication1;
import java.util.*;
public class SumofeachDigit {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
boolean quite=false;
int choice=0;
do{
System.out.println("1.Enter a number to find sum of each digit");
System.out.println("2.Quit");
choice=in.nextInt();
switch(choice){
case 1:
System.out.println("1.Enter a number");
int og=in.nextInt();
int num=og;
int sum=0;
int digit=0;
int i=0;
if(num>0){
while((num!=0)){
digit=num%10;
sum +=digit;
num=num/10;
}
System.out.println("sum of\t"+og+"="+sum);
}
else{
System.out.println("Error");
}
break;
case 2:
quite=true;
break;
default:System.out.println("Invalid");
};
}while(!quite);
System.out.println("Thank u");
}
}
0 comments:
Post a Comment