JAVA PROGRAM TO FIND THE SUM OF THE DIGITS OF A NUMBER

 

Example : 

Input: 987

Output: 9+8+7=24

 

 

Java program

 

public  class  SumofDigits {

         public  static   void   main(String  args[] ) {

         int  num, digit,sum=0;

         Scanner  sc=new  Scanner(System.in);

         System.out.println(“Enter  a  number “);

         int   num=sc.nextInt();

         while(num>0) {

         //Get  the  digit  from right side  of  the  number

 

              digit=num%10; 

 

              sum=sum+digits;

              num=num/10;

 

          }

      System.out.println(“Sum  of  digits  of  the  number:”+sum);

        }

}

 

 

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *