Single digit sum

/*189=9
* 1+8+9=18
* 1+8=9 (output)
*/

class Main{
static int rem=0,s=0;
static void sum(int num){
s=0;
while(num>0){
rem=num%10;
num=num/10;
s=s+rem;
}
if(s>=10){
sum(s);
}
}
public static void main(String ar[]){
int d=1891;
sum(d);
System.out.println("Single Digit sum ="+s);
}
}

No comments:

Popular Posts