Java Program to Find Disarium Number by Using Recursion

In the previous article, we have discussed about Java Program to Reverse a Number by Using Recursion

In this program we are going to see how to find Disarium number in java using Recursion in Java programming language. Look what is disarium number in java, Disarium Number Java, Disarium Number Program In Java, Disarium No In Java, Recursion Geeks For Geeks.

Java Program to Find Disarium Number by Using Recursion

A number in which the sum of the digits to the power of their respective position is equal to the number itself is called as a disarium number.

Let’s assume there is a number say N = 135
Then the sum of the digits to the power of their respective position of = 1^1+3^2+5^3 = 1 + 9 + 125 = 135

Now let’s see different ways to find disarium number using Recursion.

Method-1: Java Program to Find Disarium Number By Using Static Input and Recursion

Approach:

  • Declare and initialize an integer variable say ‘n
  • Call a user defined method Disarium() method and pass ‘n’ as parameter.
  • Inside the user defined method we will first store the original number into a temporary integer variable as “num” and the size of the number is stored in another integer variable size
  • Inside that Disarium() method we will call another sumofDigits() method to find the sum of digits of the number.
  • Now inside sumofDigits() method we will check if the number is equal to 0 then we will return the value as 0 else we will call  sumofDigits() method recursively and return the value to the Disarium() method.
  • Now the returned valued call sumofDigits() method is stored in an integer variable “value
  • If the sum of digits of the number is equal to the original number then print disarium number else print not a disarium number.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String[] args)
    {
        // declare and initialize an integer variable ‘n’
        int n = 135;
        // calling Disarium() method
        Disarium(n);
    }
    
    //defining Disarium() method
    static void Disarium(int n)
    {
        // integer num is used to store the original number
        int num = n;
        // integer size is used to store the size of the original number
        int size = (""+num).length();
        // calling sumofDigits() method to find the sum of digits of the number
        int value = sumofDigits(num, size);
        // if the sum of digits of the number is equal to the original number then print disarium number else print not a disarium number
        if(num == value)
            System.out.println(num + " is a Disarium Number");
        else
            System.out.println(num + " is not a Disarium Number");
    }
    
    //defining sumofDigits() method
    static int sumofDigits(int num, int size)
    {
        // if the number is 0 then return 0 to the Disarium() method
        if(num==0)
            return 0;
        // if the number is not 0 then recursively call sumofDigits() method return the value to the Disarium() method
        else
            return (int)Math.pow((num%10),size) + sumofDigits((num/10), size-1);
    }
}
Output:

135 is a Disarium Number

Method-2: Java Program to Find Disarium Number by Using Recursion By Using User Input and Recursion

Approach:

  • Declare and initialize an integer variable say ‘n
  • Prompt the user to enter a value.
  • Call a user defined method Disarium() method and pass ‘n’ as parameter.
  • Inside the user defined method we will first store the original number into a temporary integer variable as “num” and the size of the number is stored in another integer variable size
  • Inside that Disarium() method we will call another sumofDigits() method to find the sum of digits of the number.
  • Now inside sumofDigits() method we will check if the number is equal to 0 then we will return the value as 0 else we will call  sumofDigits() method recursively and return the value to the Disarium() method.
  • Now the returned valued call sumofDigits() method is stored in an integer variable “value
  • If the sum of digits of the number is equal to the original number then print disarium number else print not a disarium number.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String[] args)
    {
        // create a scanner class
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the number:");
        int n = s.nextInt();
        // calling Disarium() method
        Disarium(n);
    }
    
    //defining Disarium() method
    static void Disarium(int n)
    {
        // integer num is used to store the original number
        int num = n;
        // integer size is used to store the size of the original number
        int size = (""+num).length();
        // calling sumofDigits() method to find the sum of digits of the number
        int value = sumofDigits(num, size);
        // if the sum of digits of the number is equal to the original number then print disarium number else print not a disarium number
        if(num == value)
            System.out.println(num + " is a Disarium Number");
        else
            System.out.println(num + " is not a Disarium Number");
    }
    
    //defining sumofDigits() method
    static int sumofDigits(int num, int size)
    {
        // if the number is 0 then return 0 to the Disarium() method
        if(num==0)
            return 0;
        // if the number is not 0 then recursively call sumofDigits() method return the value to the Disarium() method
        else
            return (int)Math.pow((num%10),size) + sumofDigits((num/10), size-1);
    }
}
Output:

Enter the number:
786
786 is not a Disarium Number

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Related Java Programs: