Sum of Digits using while loop

Sum of Digits using while loop




PROGRAM:



/*Syntax
 while(condition)
 {

 }
*/

import java.util.*;

public class While {
    public static void main(String args[]) {
        Scanner sc= new Scanner(System.in);
        int a = sc.nextInt();
        int sum = 0;
        while (a > 0)
         {
            int b = a % 10;
            sum += b;
            a /= 10;

        }
        System.out.println(sum);
    }
}

/*
I/P:

523

O/P:
10

*/

Comments

Popular posts from this blog

PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION

PROGRAM TO FIND THE COUNT OF PARTICULAR NUMBER IN AN ARRAY

TELEPHONE BOOK USING HASHMAP