PROGRAM No.
:-
PROGRAM DEFINITION :-
A disarium number is a number in which the sum of the digits to the power of their
respective position is equal to the number itself.
Example: 135 = 11 + 32 + 53
Hence, 135 is a disarium number.
Design a class Disarium to check if a given number is a disarium number or not.
Some of the members of the class are given below:
Class name: Disarium
Data members/instance variables:
int num: stores the number.
int c: stores the number of digits.
Methods/Member functions:
Disarium(int n): parameterized constructor to initialize the data members
num = n and c = 0.
void countDigit(): counts the total number of digits and assigns it to size.
int cal(int n): returns the sum of the digits of the number ‘n’ to the power of
their respective positions using recursive technique.
void check(): checks whether the number is a disarium number and displays
the result with an appropriate message.
Write main() and comment lines to accomplish the above tasks.
ALGORITHM :-
STEP 1 - START
STEP 2 - Define class Disarium with variables num and c
STEP 3 - Create constructor Disarium(int n) to initialize num and c
STEP 4 - Define method countDigit() to count the number of digits in num
STEP 5 - Set variable n to num
STEP 6 - While n is greater than 0, divide n by 10 and increment c
STEP 7 - Define method cal(int n) to calculate the Disarium sum
STEP 8 - If c is 0, return 0
STEP 9 - Else, return (n % 10 raised to the power of c) + cal(n / 10) while decrementing c
STEP 10 - In the main method, create a Scanner object for user input
STEP 11 - Print "Enter the number:"
STEP 12 - Read integer n from user input
STEP 13 - Create Disarium object ds with n
STEP 14 - Call countDigit() method on ds
STEP 15 - Call cal(n) method on ds and store the result in res
STEP 16 - If res is equal to n, print "Yes, it is a Disarium."
STEP 17 - Else, print "No, it is not a disarium number."
STEP 18 - Close the Scanner object
STEP 19 – END.
PROGRAM CODE :-
import [Link].*;
public class Disarium {
int num;
int c;
Disarium(int n) {
num = n;
c = 0;
}
void countDigit() {
int n = num;
while (n > 0) {
n /= 10;
c++;
}
}
int cal(int n) {
if (c == 0) {
return 0;
} else {
return (int) (([Link](n % 10, c--)) + cal(n / 10));
}
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number :");
int n = [Link]();
Disarium ds = new Disarium(n);
[Link]();
int res = [Link](n);
if (res == n)
[Link]("Yes, it is a Disarium.");
else
[Link]("No,it is not a disarium no.");
[Link]();
}
}
INPUT AND OUTPUT :-
VARIABLE DESCRIPTION TABLE :-
Name Type Scope Purpose
num int Class Holds the input number to check if it is a Disarium.
c int Class Counts the number of digits in num.
n int Method (countDigit, cal, Temporary variable to hold the current number during
main) calculations.
sc Scan Method (main) Reads user input from the console.
ner
res int Method (main) Stores the result of the Disarium calculation.
METHOD DESCRIPTION TABLE :-
Name Return Argument Argument Purpose
Type Name Type
Disariu N/A n int Constructor to initialize num and c.
m
countDi void N/A N/A Counts the number of digits in num.
git
cal int n int Recursively calculates the Disarium sum.
main void args String[] Entry point of the program, handles user input and
output.