$ads={1}
Java Questions :Digits - II
$ads={1}
Write a program to read a non-negative integer n, compute the sum of its digits. If sum is greater than 9 repeat the process and calculate the sum once again until the final sum comes to single digit.Return the single digit.
Include a class UserMainCode with a static method getDigitSum which accepts the integer value. The return type is integer.
Create a Class Main which would be used to accept the string and call the static method present in UserMainCode.
Input and Output Format:
Input consists of a integer.
Output consists of integer.
Refer sample output for formatting specifications.
Sample Input 1:
9999
Sample Output 1:
9
Sample Input 2:
698
Sample Output 2:
5
Create a Class Main which would be used to accept the string and call the static method present in UserMainCode.
Input and Output Format:
Input consists of a integer.
Output consists of integer.
Refer sample output for formatting specifications.
Sample Input 1:
9999
Sample Output 1:
9
Sample Input 2:
698
Sample Output 2:
5
$ads={1}
import java.io.*;import java.util.*;public class Main {public static void main(String[] args) {int rem,sum=0,dsum=0,rem1;Scanner sc=new Scanner(System.in);int digit=sc.nextInt();while(digit!=0){rem=digit%10;sum=sum+rem;digit/=10;}if(sum<9){System.out.println(sum);}else{while(sum!=0){rem1=sum%10;dsum+=rem1;sum/=10;}System.out.println(dsum);}}}
Post a Comment