Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C++ Program to Check Magic Number

Posted in C++ Tutorial
Updated on Oct 06, 2024
By Mari Selvan
πŸ‘οΈ 79 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
C++ Program to Check Magic Number

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the world of programming, the concept of magic numbers is a fascinating one.

A magic number is a number in which the eventual sum of its digits, when the sum is recursively calculated, leads to a single-digit number that is 1. If the final sum is not 1, the number is not considered magic.

In this tutorial, we will delve into a C++ program that checks whether a given number qualifies as a magic number.

πŸ“„ Example

Let's explore the C++ code that checks for the magic number property.

isMagicNumber.cpp
Copied
Copy To Clipboard
#include <iostream>

// Function to check if a number is magic
bool isMagicNumber(int num) {
  while (num > 9) {
    int sum = 0;

    // Calculate the sum of digits
    while (num > 0) {
      sum += num % 10;
      num /= 10;
    }

    num = sum;
  }

  // Check if the final sum is 1
  return (num == 1);
}

// Driver program
int main() {
  // Replace this value with your desired number
  int number = 19;

  // Check if the number is magic
  if (isMagicNumber(number)) {
    std::cout << number << " is a Magic Number." << std::endl;
  } else {
    std::cout << number << " is not a Magic Number." << std::endl;
  }

  return 0;
}

πŸ’» Testing the Program

To test the program with different numbers, simply replace the value of number in the main function.

Output
19 is a Magic Number.

Compile and run the program to see whether the number is a magic number or not.

🧠 How the Program Works

  1. The program defines a function isMagicNumber that takes an integer as input and checks if it is a magic number.
  2. Inside the function, it iteratively calculates the sum of the digits of the number until the number becomes a single-digit number.
  3. It then checks if the final sum is equal to 1, indicating a magic number.

πŸ“ Between the Given Range

Let's take a look at the C++ code that checks for Magic Numbers within the specified range.

isMagicNumber.cpp
Copied
Copy To Clipboard
#include <iostream>

// Function to calculate the sum of digits until a single-digit number is obtained
int getSingleDigitSum(int num) {
  while (num > 9) {
    int sum = 0;
    while (num > 0) {
      sum += num % 10;
      num /= 10;
    }
    num = sum;
  }
  return num;
}

// Function to check if a number is a magic number
bool isMagicNumber(int num) {
  return (num == 1 || num == 10);
}

// Driver program
int main() {
  std::cout << "Magic Numbers in the range 1 to 50:\n";

  // Loop through the range of 1 to 50
  for (int i = 1; i <= 50; ++i) {
    int sum = getSingleDigitSum(i);

    // Check if the current number is a magic number
    if (isMagicNumber(sum)) {
      std::cout << i << " ";
    }
  }

  std::cout << std::endl;

  return 0;
}

πŸ’» Testing the Program

Output
Magic Numbers in the range 1 to 50:
1 10 19 28 37 46 

Compile and run the program to see the magic numbers within the range of 1 to 50.

🧠 How the Program Works

  1. The program defines two functions: getSingleDigitSum to calculate the sum of digits until a single-digit number is obtained, and isMagicNumber to check if a number is a magic number.
  2. The main function iterates through the range of 1 to 50, calculates the sum of digits, and checks if the current number is a magic number.
  3. Magic numbers are then printed in the specified output format.

🧐 Understanding the Concept of Magic Numbers

A magic number is a number in which the sum of its digits leads to a single-digit number, and that single-digit number is 1.

For example, the number 19 is a magic number because the sum of its digits (1 + 9) is 10, and the sum of 1 and 0 is 1.

🎒 Optimizing the Program

While the provided program is effective, consider exploring and implementing optimizations or additional features based on your specific requirements.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
11 months ago

If you have any doubts regarding this article (C++ Program to Check Magic Number), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy