-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesarium.java
More file actions
65 lines (62 loc) · 1.58 KB
/
Desarium.java
File metadata and controls
65 lines (62 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Program: Check if a Number is a Desarium Number Using Recursion
// Topic: Recursion and Number Properties
// Description: A Desarium number is one in which the sum of its digits powered with their respective positions equals the number itself.
// This program calculates the number of digits, then recursively computes the sum of digits raised to their positions using `sumofdigits()`.
// Demonstrates recursion, mathematical computation, and object-oriented encapsulation in Java.
package recursion;
import java.util.*;
/**
*
* @author Samim
*/
public class Desarium
{
int num;
int size;
Desarium(int nn)
{
num=nn;
size=0;
}
void digits()
{
for(int i=num;i!=0;i/=10)
{
size++;
}
}
int sumofdigits(int nn1,int p)
{
int d;
if(nn1==0)
{
return 0;
}
else
d = nn1%10;
return (int)Math.pow(d,p)+sumofdigits(nn1/10,--p);
}
void Check()
{
int sum=0;
sum=sum+sumofdigits(num,size);
if(sum==num)
{
System.out.println("Desarium Number");
}
else
{
System.out.println("Not Desarium Number");
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter The Number :");
n=sc.nextInt();
Desarium obj=new Desarium(n);
obj.digits();
obj.Check();
}
}