Saturday, 13 January 2018

Sum of digit in number in java

package com.tutorial4u;

import java.util.Scanner;

public class SumOfDigit {
       public static void main(String[] args) {
             int num,sum=0 ,rem;
             System.out.print("Enter any number to add : ");
             Scanner sc = new Scanner(System.in);
             num = sc.nextInt();
             while(num!=0){
                    rem = num %10;
                    num = num/10;
                    sum = sum+rem;
             }
             System.out.println("Sum of digit is : "+sum);
       }

}


Output : 

Enter any number to add : 12345
Sum of digit is : 15

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...