

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a number is magic (Recursive sum of digits is 1) in C++
Here we will see one program, that can check whether a number is magic number or not. A number is said to be magic number, when the recursive sum of the digits is 1. Suppose a number is like 50311 = 5 + 0 + 3 + 1 + 1 = 10 = 1 + 0 = 1, this is magic number.
To check whether a number is magic or not, we have to add the digits until a single-digit number is reached.
Example
#include <iostream> using namespace std; int isMagicNumber(int n) { int digit_sum = 0; while (n > 0 || digit_sum > 9) { if (n == 0) { n = digit_sum; digit_sum = 0; } digit_sum += n % 10; n /= 10; } return (digit_sum == 1); } int main() { int number = 50311; if(isMagicNumber(number)){ cout << number << " is magic number"; } else { cout << number << " is not magic number"; } }
Output
50311 is magic number
- Related Questions & Answers
- Recursive sum of digits of a number is prime or no in C++
- C Program to check if a number is divisible by sum of its digits
- Recursive sum all the digits of a number JavaScript
- Recursive sum of digits of a number formed by repeated appends in C++
- Recursive program to check if number is palindrome or not in C++
- C Program to check if a number is divisible by any of its digits
- Check if the frequency of all the digits in a number is same in Python
- Recursive function to check if a string is palindrome in C++
- Recursive product of all digits of a number - JavaScript
- Check whether sum of digits at odd places of a number is divisible by K in Python
- Check if a given number divides the sum of the factorials of its digits in C++
- Check if product of digits of a number at even and odd places is equal in Python
- Check if a number is a power of another number in C++
- Check if a number is formed by Concatenation of 1, 14 or 144 only in C++
- Check if a number is Palindrome in C++
Advertisements