Skip to main content

disarium number in java

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

Popular posts from this blog

How to Learn whole syllabus of subject in 1 day? + some cool last minute tips

Hi everyone,  In  this article I am going to tell you how to complete the whole syllabus for exam in one day. I will tell you a full one day plan to complete all your syllabus without having any doubt and also prepare for that subject. To follow this, firstly you have to divide the syllabus of your subjects into 3 parts-  The first part is for easy chapters second part is for moderate chapters and third parties for difficult chapters in easy chapters you have to put it at topics for which you can answer all the question.  In moderate you have to put that chapters in which you have very less doubt. In the difficult you have to put that topic whose questions you are not able to answer. HERE IS THE TEMPLATE FOR YOUR PLANNING- PLAN starting in the morning  Prepare a chart of the whole day time table. Begin your day at 8:00 a.m.  In the first three hours revise or learn all the chapters which are easy for you remember after every 50 minutes you have to take 10 m...

Emirp number in java, program to check emirp number

Emirp is simply the reverse prononciation of word "prime". An emirp number is the number which is prime and whose reverse is also a prime number. EXAMPLE-1 17  Reverse of the number = 71 Both the numbers are prime so 17 is an emirp number. EXAMPLE-2 37 Reverse of the number = 73 Both the numbers are prime so 37 is an emirp number. EXAMPLE-3 13 Reverse of the number = 31 Both the numbers are prime so 13 is an emirp number. Emrip number can be easily made in java with the help of methods/functions. Firstly, take an input of number, then check it is prime or not, then reverse the number and again check it is prime or not, if both are true, print the suitable output. ALGORITHM- STEP 1 : start STEP 2 : print "enter a number" STEP 3 : take input of number in e STEP 4 : int n=num, int rev =0 STEP 5 : repeat STEP to STEP until n>0 STEP 6 : int d=n%10 STEP 7 : rev=(rev*10)+d STEP 8 : n=n/10 STEP 9 :  int p= input value from STEP 13 STEP 10 : int d=0, x=num STEP 11 : int i...

Palindrome number in java

A number is said to be palindrome if it reads both forward and backwards same. Example- 175571 454 A program for palindrome number can be made by simply extracting digits of the number in reverse order and adding them with their place value and then checking is it same as the original number or not. To extract a digit in revers order and adding with place value we simply follow the below code. while(n>0) { int d=n%10; rev=(rev*10)+d; n=n/10; } To explain this let us take an example 657 Then  For first iteration =  n=657                                     d=657%10=7                                     rev=(0*10)+7=7 For second iteration = n=65                                         ...