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

Java program for prime numbers

A prime number is the number which is is  divisible by itself and not by other numbers. A program to check prime number can be made by using for and while loop combination . This article contains java program for finding prime number ,write a java program for prime number between 1- 100 ,simple program to find prime factorial of a number in java. EXAMPLE 1 - program to check whether a number is prime number  or not. import java.util.*; public class pr { public static void main() { Scanner sc=new Scanner(System.in); System.out.println("enter number"); int num=sc.nextInt(); int d=0; for(int i=2;i<=num;i++) { if(num%i==0) d++; } if(d==0) System.out.println("it is a prime number"); else System.out.println("it is not a prime number"); } } Output enter number 23 it is a p...

Niven number in java

A number is said to be niven or harshad if the number is divisible by the sum of its digits. EXAMPLE-135 The sum of digits are: 1 + 3 + 5                                                                  135/9=15 A simple java program to check whether a number is harshad / niven or not can be made with the help of while loop. For this, firstly we extract the digits of the number and then we sum up all the digits.After all the digits are summed then we check whether rhe number when divided by the sum of its digits leaves any remainder or not. If the remainder is zero then number is niven / harshad number else not. ALGORITHM - STEP 1: Start STEP 2 : print "enter a number" STEP 3 : taking input of integer in n STEP 4 : int s = 0 STEP 5 : int no = n STEP 6 : repeat STEP 7 to STEP 9 until no!=0 STEP 7 : int d=no%10 STEP 8 : s=s+d...