Super Number in java
Steps to do this:-
1. take 4 integers x,n,sum,r ;// (n is for user input number x will store the n's value sum will calculate the addition of the each numbers and r will store the remainder )
2. take a value from user
3.and do this
while(n>=10)
{
x=n;
sum=0;
while(x>0)
{
r=x%10;
sum=sum+r;
x=x/10;
}
n=sum;
}
first we start while(n>=10) here we store the n's value in x and we do sum=0 because, we start the loop from n>=10 it means if n is <=9 the loop will terminate and we can get a single digit value
and we just put the user input value in x and initialize sum=0 then we just start the simple while loop to calulate each digit sum and the sum we stored in n
now if n is <=9 then the loop will be terminated and if not then loop will be restart until we get a single digit value inside n
4. now print " the super digit is"+n outside the all loop
5.save and run
The Source Code
import java.util.Scanner;
class superd
{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int x;
int n,sum,r;
// x is for to store n's value which will be user input
//sum is for calculating the each digit sum
// r is for remainder
System.out.println("enter the Number");
n=s.nextInt();
while(n>=10)
{
x=n;
sum=0;
while(x>0)
{
r=x%10;
sum=sum+r;
x=x/10;
}
n=sum;
}
System.out.println("the super digit of the given number is "+n);
}
}
Watch My Video
Comments
Post a Comment
if you like it or have any doubts please let me know