Please It's a request don't give irRelevent AnswerS
Answers
- Write a java program to check whether a number is a disarium number or not.
- There are many approaches. I'm showing you one of them.
- Here is the source code.
--------------------------------------------
import java.util.*;
class x
{
public static void main(String s[ ])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int n=sc.nextInt();
if(isDisarium(n))
System.out.println("Disarium Number.");
else
System.out.println("Not a Disarium Number.");
}
static boolean isDisarium(int num)
{
int x=num, s=0;
String s=Integer.toString(num);
int len=s.length();
while(x!=0)
{
int d=x%10;
s+=(int)Math.pow(d, len);
--len;
x/=10;
}
if(s==num)
return true;
else
return false;
}
}
------------------------------------------------
I hope you are satisfied with my answer.
Question:-
Write a Program to check whether a given Number is Disarium or Not.
_______________________________
What is Disarium Number?
A number will be called Disarium if the sum of its digits powered with their respective position is equal with the number itself.
_______________________________
Example:-
For example 175 is a Disarium number:-
_______________________________
Logic applied in Coding:-
First we will count the Terms of Given number using Counter.Then using another loop we will do the sum of Term with their respective power(increasing by 1 till terms).
Then, we will check by using Condition wether the Number Matched or Not.
At last we will find the Output.
Remember:-
Before Doing any Calculation always try to make The copy of Real of Given number And do Calculation with that copy which will help full to You.
_______________________________
CODE:-
class Disarium
{
public static void main(int n)
{
int m=n,sum,j=0;
for(;m>0;j++)
{
m=m/10;
}
for(int k=n;k>0;k=k/10,j=j-1)
{
sum=(int)sum+Math.pow(k%10,j);
}
if(sum==n)
System.out.println("Yes\n"+n+" is a Disarium Number");
else
System.out.println("No");
}
}
______________________________
Input:-
135
Output:-
Yes
135 is a Disarium Number