A number is said to be disarium if the sum of its digit powered with their positions is equal to the number itself.
EXAMPLE- 135 = (1^1) + (3^2) + (5^3)
= 1 + 9 + 125
= 135
A simple java program to check disarium number can be made by firstly calculating the length of the number and then adding digits of the number raised to the power of its position in the number.
ALGORITHM-
STEP 1 : Start STEP 2 : print "enter a number" STEP 3 : taking input of integer in n STEP 4 : int l= Integer.toString().length() STEP 5 : int no=n STEP 6 : int s=0 STEP 7 : repeat STEP 8 to STEP 11 until (no!=0) STEP 8 : int d=no%10 STEP 9 : s=s+(int)(Math.pow(d,l) STEP 10 :no=no/10 STEP 11 : l-- STEP 12 : if (s==n) then goto STEP 13 else goto STEP 14 STEP 13 : print "the number is disarium" STEP 14 : print "the number is not disarium" STEP 15 : end |
JAVA CODE-
import java.util.*;
public class disarium
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
int n=sc.nextInt();
int l=Integer.toString(n).length();
int no=n;
int s=0;
while(no!=0)
{
int d=no%10;
s=s+(int)(Math.pow(d,l));
no=no/10;
l--;
}
if(s==n)
{
System.out.println("The number is disarium");
}
else
{
System.out.println("the number is not disarium");
}
}
}
Output-
enter a number
135
The number is disarium
Similar java code-
If you find helpful please share
Comments
Post a Comment