- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- 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++
- Check if a given number divides the sum of the factorials of its digits in C++
- Recursive function to check if a string is palindrome in C++
- C Program to check if a number is divisible by any of its digits
- C++ Program to check if a given number is Lucky (all digits are different)
- In a magic square each row, column, and diagonal have the same sum. Check which of the following is a magic square.
- n-th number whose sum of digits is ten in C++
- Recursive product of all digits of a number - JavaScript
- Check if the frequency of all the digits in a number is same in Python
- The sum of digits of a two-digit number is 8. If 36 is added to the number then the digits reversed. Find the number.
- Check whether sum of digits at odd places of a number is divisible by K in Python

Advertisements