Write the program to find out the magic number in c++ programming software
Answers
Answered by
0
#include<iostream>
#include<conio.h>
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;
}
Similar questions
Math,
6 months ago
Physics,
6 months ago
Social Sciences,
6 months ago
English,
1 year ago
Hindi,
1 year ago