0% found this document useful (0 votes)
13 views1 page

Disarium Number

The Java program checks if a given number is a Disarium number, which is defined as a number that is equal to the sum of its digits raised to the power of their respective positions. It includes functions to count the number of digits, calculate the Disarium sum recursively, and determine if the number meets the Disarium condition. The program takes user input and outputs whether the number is a Disarium number or not.

Uploaded by

Utkarsh Dwivedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views1 page

Disarium Number

The Java program checks if a given number is a Disarium number, which is defined as a number that is equal to the sum of its digits raised to the power of their respective positions. It includes functions to count the number of digits, calculate the Disarium sum recursively, and determine if the number meets the Disarium condition. The program takes user input and outputs whether the number is a Disarium number or not.

Uploaded by

Utkarsh Dwivedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Scanner;

public class DisariumNumber {

// Function to calculate the number of digits in the number


public static int countDigits(int num) {
if (num == 0) return 0;
return 1 + countDigits(num / 10);
}

// Recursive function to calculate the sum of digits powered to their positions


public static int calculateDisarium(int num, int power) {
if (num == 0) return 0;
int digit = num % 10;
return (int) Math.pow(digit, power) + calculateDisarium(num / 10, power - 1);
}

// Main function to check if a number is a Disarium number


public static boolean isDisarium(int num) {
int numberOfDigits = countDigits(num);
int sum = calculateDisarium(num, numberOfDigits);
return sum == num;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();

if (isDisarium(num)) {
System.out.println(num + " is a Disarium number.");
} else {
System.out.println(num + " is not a Disarium number.");
}

scanner.close();
}
}

You might also like