C++ program to check if a number is a Magic number in 3 ways

C++ program to check if a number is a Magic number:

Let’s learn how to find if a number is a Magic number or not in C++. If the recursive sum of the digits of a number is equal to 1, it is called a Magic number.

The recursive sum of digits is the sum of digits of the number recursively until a single digit number is found. For example, if the number is 499, the sum of its digits is 21. We need to find the sum of the digits of 21 i.e. 3, which is the required recursive sum of the digits of 499.

1234 is a Magic number. Because, the sum of its digits is 1 + 2 + 3 + 4 is 10. And, the sum of digits of 10 is 1, i.e. the recursive sum of digits of 1234 is 1.

For example, 226, 10, 1, 55, 325 etc. are Magic numbers.

We will write one program, that will take one number as an input from the user, find out if the number is a Magic number or not and print one message on console.

Method 1: C++ Magic number program by using a while loop:

Let’s write this program with a while loop. We will use one single while loop to write this program:

#include <iostream>
using namespace std;

int isMagic(int num)
{
    int sum = 0;
    while (num > 0 || sum > 9)
    {
        if (num == 0)
        {
            num = sum;
            sum = 0;
        }
        sum += num % 10;
        num /= 10;
    }

    return sum == 1;
}

int main()
{
    int num;

    cout << "Enter a number: " << endl;
    cin >> num;

    if (isMagic(num))
    {
        cout << "It is a Magic number." << endl;
    }
    else
    {
        cout << "It is not a Magic number." << endl;
    }
}

Here,

  • The isMagic method is used to check if a number is a Magic number or not.
  • It takes one number, num as its parameter. The integer variable sum is used to hold the recursive sum. It is initialized as 0.
  • The while loop will run until the number is positive and the recursive sum is not one digit.
  • It removes the last digit of the number and adds it to the sum variable. Once the number become zero, it assigns the value of the sum to the number and assigns zero to the sum variable.
  • So, at the end of the loop, the sum variable holds the recursive sum of the number.
  • It returns 1 if the recursive sum is equal to 1, else it returns 0.

If you run this program, it will print output as below:

Enter a number: 
44
It is not a Magic number.

Enter a number: 
1234
It is a Magic number.

C++ check magic number

Method 2: C++ Magic number program by using a for loop:

We can change the above program to use a for loop instead of a while loop. It will look as below:

#include <iostream>
using namespace std;

int isMagic(int num)
{
    int sum;
    for (sum = 0; num > 0 || sum > 9; sum += num % 10, num /= 10)
    {
        if (num == 0)
        {
            num = sum;
            sum = 0;
        }
    }

    return sum == 1;
}

int main()
{
    int num;

    cout << "Enter a number: " << endl;
    cin >> num;

    if (isMagic(num))
    {
        cout << "It is a Magic number." << endl;
    }
    else
    {
        cout << "It is not a Magic number." << endl;
    }
}

The for loop works in a similar way.

  • It will run until num > 0 || sum > 9, i.e. until the number is greater than zero or the sum is not a single digit value.
  • At the end of each iteration, it updates the sum and num variables, sum += num % 10, num /= 10.

It will print similar output.

Enter a number: 
1234
It is a Magic number.

Enter a number: 
12345
It is not a Magic number.

Method 3: C++ Magic number program by using a recursive method:

The following program changes the isMagic method to work recursively. It will call the isMagic method recursively until the result is found.

#include <iostream>
using namespace std;

int isMagic(int num, int sum)
{
    if (sum < 10 && sum != 0 && num == 0)
    {
        return sum == 1;
    }
    if (num == 0)
    {
        return isMagic(sum, 0);
    }

    sum += num % 10;
    num /= 10;

    return isMagic(num, sum);
}

int main()
{
    int num;

    cout << "Enter a number: " << endl;
    cin >> num;

    if (isMagic(num, 0))
    {
        cout << "It is a Magic number." << endl;
    }
    else
    {
        cout << "It is not a Magic number." << endl;
    }
}

Note that this method takes both the number and the sum values as its params.

You will get similar result with this program.

Enter a number: 
226
It is a Magic number.

Enter a number: 
325
It is a Magic number.

Enter a number: 
344
It is not a Magic number.

You might also like: