Check if a number is magic ( Recursive sum of digits is 1 ) | CPP program to check if a number is Magic number . ; 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 . ; Return true if sum becomes 1. ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isMagic ( int n ) { int sum = 0 ; while ( n > 0 sum > 9 ) { if ( n == 0 ) { n = sum ; sum = 0 ; } sum += n % 10 ; n /= 10 ; } return ( sum == 1 ) ; } int main ( ) { int n = 1234 ; if ( isMagic ( n ) ) cout << " Magic ▁ Number " ; else cout << " Not ▁ a ▁ magic ▁ Number " ; return 0 ; }