-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathmagicNumber.cpp
More file actions
37 lines (33 loc) · 776 Bytes
/
magicNumber.cpp
File metadata and controls
37 lines (33 loc) · 776 Bytes
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
#include<iostream>
using namespace std;
bool isMagic(int n)
{
int sum = 0;
/*A number is said to be a magic number, if the sum of
its digits are calculated till a single digit recursively
by adding the sum of the digits after every addition. If
the single digit comes out to be 1,then the number is
a magic number*/
while (n > 0 || sum > 9)
{
if (n == 0)
{
n = sum;
sum = 0;
}
sum += n % 10;
n /= 10;
}
// Return true if sum becomes 1.
return (sum == 1);
}
// Driver code
int main()
{
int n = 1234;
if (isMagic(n))
cout << "Magic Number";
else
cout << "Not a magic Number";
return 0;
}