In the previous article, we have discussed Java Program to Check Unique Number
In this article we are going to understand what Disarium number is and how we can check whether a number is Disarium or not in Java with examples.
Program to Check Disarium Number
Disarium numbers are numbers where the digits raised to the power of their position order(from left to right) add upto the number itself.
Example: 135: (1)1+(3)2 +(5)3 =1+9+125= 135 is Disarium Number 121: (1)1 +(2)2 +(1)3 =1+2+1 = 4 is not Disarium Number
Where, (n)p represents power 'p' of number 'n
‘.
In the above examples the numbers 135 and 518 are Disarium numbers. 121 digits’ with powers add upto 4, hence it is not a Disarium number.
Let’s see different ways to check disarium number.
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.
Approach :
- We ask the user to enter a number or declare a number, which we store in two integer variables.
- We then use a while loop to iterate through all the digits in the number.
- All the digits are raised to power of their positions and added together.
- If the resultant sum is equal to the number then it is a disarium number else not.
Method-1: Java Program to Check Disarium Number By Using Static Value
import java.util.Scanner; public class DisariumNumber{ public static void main(String args[]) { //A number declared int num = 135; int temp=num,dig=0,remainder,sum=0; // Loop to count the number of dig while(temp>0) { dig++; temp=temp/10; } //Reassigning values to temp temp=num; //Adds up the digits with power while (temp > 0) { remainder = temp % 10; sum = sum + (int)Math.pow(remainder, dig); temp = temp / 10; dig--; } if(sum==num) { System.out.println(num+" is a Disarium number"); } else { System.out.println(num+" is not a Disarium number"); } } }
Output: 135 is a Disarium number
Method-2: Java Program to Check Disarium Number By User Input Value
import java.util.Scanner; public class DisariumNumber{ public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); int temp=num,dig=0,remainder,sum=0; // Loop to count the number of dig while(temp>0) { dig++; temp=temp/10; } //Reassigning values to temp temp=num; //Adds up the digits with power while (temp > 0) { remainder = temp % 10; sum = sum + (int)Math.pow(remainder, dig); temp = temp / 10; dig--; } if(sum==num) { System.out.println(num+" is a Disarium number"); } else { System.out.println(num+" is not a Disarium number"); } } }
Output: Case-1 Enter a number : 135 135 is a Disarium number Case-2 Enter a number : 56 56 is a Disarium number
By C Programming Language
#include<stdio.h> #include<math.h> int main() { int n; printf("Enter a number:"); scanf("%d",&n); int num=n,c=0; while(num!=0) { num/=10; c++; } num=n; int sum=0; while(num!=0) { int rem=num%10; sum+=pow(rem,c); num/=10; c--; } if(sum==n) printf("Disarium Number."); else printf("Not a Disarium Number."); }
Output: Enter a number:135 It is a Disarium Number.
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.
Related Java Programs: