Find the digital sum Java Avg part3
A number N is passed as an input to the program. The program must print the digital sum of the number.
Note: The digital sum of a number is defined as the recursive sum of digits of a number till it reaches a single digit.
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int temp=n,sum=0;
while(n>0)
{
sum+=n%10;
n/=10;
}
if(sum>9)
System.out.print(temp%9);
else
System.out.print(temp%9);
}
}
Comments
Post a Comment