wap to accept a number and check if it disarium number or not ( in java)
Answers
Answer:
WAP to accept a number and check if it disarium number or not
Explanation:
import java.util.Scanner;
public class Example11 {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Input a number : ");
int num = sc.nextInt();
int copy = num, d = 0, sum = 0;
String s = Integer.toString(num);
int len = s.length();
while(copy>0)
{
d = copy % 10;
sum = sum + (int)Math.pow(d,len);
len--;
copy = copy / 10;
}
if(sum == num)
System.out.println("Disarium Number.");
else
System.out.println("Not a Disarium Number.");
}
}
Output:
Input a number: 45
Not a Disarium Number.
Disarium number:-
Sum of the digit of number power by its own position in the number, generate a number.If this generated number equal to the original number this would be said disarium number.
Example:-
135
1¹+3²+5³
1+9+125
135
import java.util.*;
class disarium
{
public static void main( )
{
Scanner sc=new Scanner (System.in);
System.out.println("ENTER A NUMBER");
int n=sc.nextInt( );
int m=n;
int c=0;
while (n>0)
{
c++;
n=n/10;
}
int s=0;
while (m>0)
{
int r=m%10;
s=s+(int)Math.pow(r,c);
c--;
m=m/10;
}
if(s==on)
System.out.println(It is a disarium number);
else
System.out.println(It is not a disarium number);
}
}