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

Disarium Number 3

The document contains a Java program that checks if a given number is a Disarium number. It includes functions to count the digits, calculate the sum of digits raised to their respective positions, and determine if the number meets the Disarium criteria. The program prompts the user for input and displays the result accordingly.

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)
10 views1 page

Disarium Number 3

The document contains a Java program that checks if a given number is a Disarium number. It includes functions to count the digits, calculate the sum of digits raised to their respective positions, and determine if the number meets the Disarium criteria. The program prompts the user for input and displays the result accordingly.

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