Saturday, December 12, 2015

java program to find sum of each digit and repeat the process until users exit:


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");
   }
}

Related Posts:

  • String Length in java String Length in java: The length of the String is the actual number of character that contains in it.To get this value,call the length() method as … Read More
  • replace() method Modifying a String: replace();     The replace method is used to replace the one character of string.It has the general form:   &nbs… Read More
  • Character Extraction Character Extraction: 1) charAt() charAt() method is used to extract single character from String.Here is a small program to demonstrate chatAt() … Read More
  • Access Protection in java: Access Protection in java: private     No modifier protected public  Same class … Read More
  • Character Extraction Character Extraction: 2)getChars() If you want to extract more than one character at a time,you can use the getChars() method. It has general form… Read More

0 comments:

Post a Comment