Digit sum

 Digit sum

Problem Description


Digit Sum sum of all digits of a number


Input :129


Output:3


Explanation 1+2+9=12,


1+2=3


Solution,


import java.util.Scanner;

import java.io.*;

public class TestClass {

public static void main(String[] args) { 

Scanner sc= new Scanner(System.in); 

int a= sc.nextInt();

int sum=0;

while (a > 9)

{

sum=0;

while(a>0)

{

int r= a%10;

sum=sum+r;

a=a/10;

}

a=sum; 

}

System.out.println(+sum);

}

}

Post a Comment

0 Comments