forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
krishnamurthy_number.cpp
67 lines (55 loc) · 1.51 KB
/
krishnamurthy_number.cpp
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
66
67
/*
If the factorial of individual digits of a number when summed up yields the number itself then the number is called Krishnamurthy
Number.
Example :
Consider the number 145
Summing of factorials of the individual digits we get = 1! + 4! + 5! = 1 + 24 + 120 =145.
Hence 145 is not a Krishnamurthy number.
*/
//CPP Program to find whether a number is a Krishnamurthy number
#include<iostream>
//Function to calculate the factorial of the individual digits
int Factorial ( int n )
{
if ( n == 1 || n == 0 )
{
return 1;
}
return n * Factorial( n - 1 );
}
int Krishnamurthy_No ( int num )
{
int digit, sum = 0 ,copy = num;
//Running a loop till we have extracted all the digits from the copy of the given number
while ( copy > 0 )
{
digit = copy % 10;
sum = sum + Factorial ( digit );
copy = copy / 10;
}
return sum;
}
int main ()
{
int number, digit, sum = 0;
std::cout << "Enter a natural number " << std::endl;
std::cin >> number;
int answer = Krishnamurthy_No ( number );
//Comparing if calculated sum adds up to the number
if ( answer == number)
{
std:: cout << number <<" is a krishnamurthy number. ";
}
else
{
std:: cout << number <<" is not a krishnamurthy number. ";
}
}
/*
Test Case 1 :-
INPUT : 37
OUTPUT : 37 is not a krishnamurthy number.
Test Case 2 :-
INPUT : 145
OUTPUT: 145 is a krishnamurthy number.
*/