forked from GDSCTINT/hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
specialno.java
40 lines (40 loc) · 1003 Bytes
/
specialno.java
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
import java.util.Scanner;
public class SpecialNumberExample1
{
public static void main(String args[])
{
int num, number, last_digit, sum_Of_Fact = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
//reads an integer from the user
number = sc.nextInt();
num = number;
//executes until the condition becomes false
while (number > 0)
{
//finds the alst digit from the given number
last_digit = number % 10;
//variable stores factorial
int fact=1;
//factorial logic
for(int i=1; i<=last_digit; i++)
{
//calculates factorial
fact=fact*i;
}
//calculates the sum of all factorials
sum_Of_Fact = sum_Of_Fact + fact;
//divides the number by 10 and removes the last digit from the number
number = number / 10;
}
//compares the sum with the given number
if(num==sum_Of_Fact)
{
System.out.println(num+ " is a special number.");
}
else
{
System.out.println(num+ " is not a special number.");
}
}
}