Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Magic Number.cpp #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Magic Number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// CPP program to check if a number is Magic
// number.
#include<iostream>
using namespace std;

bool isMagic(int n)
{
int sum = 0;

// Note that the loop continues
// if n is 0 and sum is non-zero.
// It stops when n becomes 0 and
// sum becomes single digit.
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;
}