# Check if a number is magic (Recursive sum of digits is 1)

A number is said to be a magic number, if the sum of its digits are calculated till a single digit recursively by adding the sum of the digits after every addition. If the single digit comes out to be 1,then the number is a magic number.
For example-
Number= 50113
=> 5+0+1+1+3=10
=> 1+0=1
This is a Magic Number
For example-
Number= 1234
=> 1+2+3+4=10
=> 1+0=1
This is a Magic Number

Examples :

```
Input : 1234
Output : Magic Number

Input  : 12345
Output : Not a magic Number
```

The approach used brute force. The function keeps adding digits until a single digit sum is reached. To understand how i am calculating the sum upto a single digit view this page- Finding sum of digits of a number until sum becomes single digit

## C++

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

## Java

 `// Java program to check if  ` `// a number is Magic number. ` `class` `GFG ` `{ ` `   ``public` `static` `boolean` `isMagic(``int` `n) ` `   ``{ ` `       ``int` `sum = ``0``; ` `      `  `       ``// 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. ` `       ``while` `(n > ``0` `|| sum > ``9``) ` `       ``{ ` `           ``if` `(n == ``0``) ` `           ``{ ` `               ``n = sum; ` `               ``sum = ``0``; ` `           ``} ` `           ``sum += n % ``10``; ` `           ``n /= ``10``; ` `       ``} ` `      `  `       ``// Return true if sum becomes 1. ` `       ``return` `(sum == ``1``); ` `   ``} ` `   `  `   ``// Driver code ` `   ``public` `static` `void` `main(String args[]) ` `    ``{ ` `     ``int` `n = ``1234``; ` `     ``if` `(isMagic(n)) ` `        ``System.out.println(``"Magic Number"``); ` `          `  `     ``else` `        ``System.out.println(``"Not a magic Number"``); ` `    ``} ` `} ` ` `  `// This code is contributed by Anshika Goyal. `

## Python3

 `# Python3 program to check  ` `# if a number is Magic ` `# number. ` ` `  `def` `isMagic(n): ` `    ``sum` `=` `0``; ` `     `  `    ``# 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. ` `    ``while` `(n > ``0` `or` `sum` `> ``9``): ` `        ``if` `(n ``=``=` `0``): ` `            ``n ``=` `sum``; ` `            ``sum` `=` `0``; ` `        ``sum` `=` `sum` `+` `n ``%` `10``; ` `        ``n ``=` `int``(n ``/` `10``); ` `         `  `    ``# Return true if ` `    ``# sum becomes 1. ` `    ``return` `True` `if` `(``sum` `=``=` `1``) ``else` `False``; ` ` `  `# Driver code ` `n ``=` `1234``; ` `if` `(isMagic(n)): ` `    ``print``(``"Magic Number"``); ` `else``: ` `    ``print``(``"Not a magic Number"``); ` `     `  `# This code is contributed  ` `# by mits. `

## C#

 `// C# program to check if  ` `// a number is Magic number. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `    ``public` `static` `bool` `isMagic(``int` `n) ` `    ``{ ` `        ``int` `sum = 0; ` `         `  `        ``// 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. ` `        ``while` `(n > 0 || sum > 9) ` `        ``{ ` `            ``if` `(n == 0) ` `            ``{ ` `                ``n = sum; ` `                ``sum = 0; ` `            ``} ` `            ``sum += n % 10; ` `            ``n /= 10; ` `        ``} ` `         `  `        ``// Return true if sum becomes 1. ` `        ``return` `(sum == 1); ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 1234; ` `        ``if` `(isMagic(n)) ` `            ``Console.WriteLine(``"Magic Number"``); ` `             `  `        ``else` `            ``Console.WriteLine(``"Not a magic Number"``); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` 0 || ``\$sum` `> 9) ` `    ``{ ` `        ``if` `(``\$n` `== 0) ` `        ``{ ` `            ``\$n` `= ``\$sum``; ` `            ``\$sum` `= 0; ` `        ``} ` `        ``\$sum` `+= ``\$n` `% 10; ` `        ``\$n` `/= 10; ` `    ``} ` `     `  `    ``// Return true if ` `    ``// sum becomes 1. ` `    ``return` `(``\$sum` `== 1); ` `} ` ` `  `// Driver code ` `\$n` `= 1234; ` `if` `(isMagic(``\$n``)) ` `    ``echo``"Magic Number"``; ` `else` `    ``echo` `"Not a magic Number"``; ` ` `  `// This code is contributed  ` `// by nitin mittal. ` `?> `

Output :

```Magic Number
```

This article is contributed by Ayush Saxena. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up

Improved By : nitin mittal, Mithun Kumar