Check if a number is magic (Recursive sum of digits is 1)
Last Updated :
16 Feb, 2023
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<iostream>
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.
import java.io.*;
public 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
<?php
// PHP program to check if
// a number is Magic number.
function 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 || $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.
?>
JavaScript
<script>
// JavaScript program to check if
// a number is Magic number.
function isMagic( n)
{
var 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
var n = 1234;
if (isMagic(n))
document.write("Magic Number");
else
document.write("Not a magic Number");
// This code is contributed by shivanisinghss2110
</script>
Time Complexity: O(log10n)
Auxiliary Space: O(1), As constant extra space is used.
Efficient Approach(Shortcut): There is also a shortcut method to verify Magic Number. The function will determine if the remainder on dividing the input by 9 is 1 or not. If it is 1, then the number is a magic number. The divisibility rule of 9 says that a number is divisible by 9 if the sum of its digits are also divisible by 9. Therefore, if a number is divisible by 9, then, recursively, all the digit sums are also divisible by 9. The final digit sum is always 9. An increase of 1 in the original number will increase the ultimate value by 1, making it 10 and the ultimate sum will be 1, thus verifying that it is a magic number.
C++
// C++ program to check
// Whether the number is Magic or not.
#include <iostream>
using namespace std;
int main() {
// Accepting sample input
int x = 1234;
// Condition to check Magic number
if(x%9==1)
cout << ("Magic Number");
else
cout << ("Not a Magic Number");
return 0;
}
C
// C program to check
// Whether the number is Magic or not.
#include <stdio.h>
int main() {
// Accepting sample input
int x = 1234;
// Condition to check Magic number
if(x%9==1)
printf("Magic Number");
else
printf("Not a Magic Number");
return 0;
}
Java
// Java program to check
// Whether the number is Magic or not.
import java.io.*;
public class GFG{
public static void main(String[] args)
{
// Accepting sample input
int x = 1234;
// Condition to check Magic number
if (x % 9 == 1)
System.out.printf("Magic Number");
else
System.out.printf("Not a Magic Number");
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program to check
# Whether the number is Magic or not.
# Accepting sample input
x = 1234
# Condition to check Magic number
if (x % 9 == 1):
print("Magic Number")
else:
print("Not a Magic Number")
# This code is contributed by kirti
C#
// C# program to check
// Whether the number is Magic or not.
using System;
using System.Collections.Generic;
class GFG{
public static void Main(String[] args)
{
// Accepting sample input
int x = 1234;
// Condition to check Magic number
if (x % 9 == 1)
Console.Write("Magic Number");
else
Console.Write("Not a Magic Number");
}
}
// This code is contributed by Princi Singh
JavaScript
<script>
// JavaScript program to check
// Whether the number is Magic or not.
// Accepting sample input
var x = 1234;
// Condition to check Magic number
if (x % 9 == 1)
document.write("Magic Number");
else
document.write("Not a Magic Number");
// This code is contributed by shivanisinghss2110
</script>
Time Complexity: O(1)
Auxiliary Space: O(1) As constant extra space is used.
Similar Reads
Check if the sum of digits of a number N divides it
Given a number n, the task is to check if the sum of digits of the given number divides the number or not. Examples: Input : n = 12Output : YesExplanation: Sum of digits = 1+2 =3 and 3 divides 12.Input : n = 15Output : NoExplanation: Sum of digits = 1+5 =6 and 15 % 6 != 0.Using Iterative Method - O(
6 min read
Recursive sum of digits of a number is prime or not
Given a number n, we need to find the sum of each digits of the number till the number becomes a single digit. We need to print "yes" if the sum is a prime or "no" if it is not prime. Examples: Input : 5602 Output: No Explanation: Step 1- 5+6+0+2 = 13 Step 2- 1+3 = 4 4 is not prime Input : 56 Output
5 min read
Program to check if a number is divisible by sum of its digits
Given an integer N, the task is to check whether the number is divisible by the sum of its digits or not. If divisible, then print âYESâ else print âNOâ. Examples: Input: N = 12 Output: YES Explanation: As sum of digits of 12 = 1 + 2 = 3 and 12 is divisible by 3 So the output is YES Input: N = 123 O
7 min read
Check if all digits of a number divide it
Given a number n, find whether all digits of n divide it or not.Examples: Input : 128 Output : Yes 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. Input : 130 Output : No We want to test whether each digit is non-zero and divides the number. For example, with 128, we want to test d != 0 && 128
9 min read
Program to check if a number is divisible by any of its digits
Given an integer N where 1 \leq n \leq 10^{18} . The task is to check whether the number is not divisible by any of its digit. If the given number N is divisible by any of its digits then print "YES" else print "NO". Examples: Input : N = 5115Output : YESExplanation: 5115 is divisible by both 1 and
10 min read
Check if the product of digit sum and its reverse equals the number or not
Given a number, check whether the product of digit sum and reverse of digit sum equals the number or not.Examples: Input : 1729 Output : Yes Explanation: digit sum = 1 + 7 + 2 + 9 = 19 Reverse of digit sum = 91 Product = digit sum * Reverse of digit sum = 19 * 91 = 1729 Input : 2334 Output : No Flow
6 min read
Check if a number is a perfect square having all its digits as a perfect square
Given an integer N, the task is to check if the given number is a perfect square having all its digits as a perfect square or not. If found to be true, then print âYesâ. Otherwise, print âNoâ.Examples: Input: N = 144 Output: Yes Explanation: The number 144 is a perfect square and also the digits of
11 min read
Check if a given number is one less than twice its reverse
Given an integer N, the task is to check if it is a solution to the equation 2 * reverse(N) - 1 = N Examples: Input: N = 73Output: YesExplanation:2 * reverse(N) = 2 * 37 = 74N + 1 = 73 + 1 = 74 Input: N = 83Output: No Naive Approach: The simplest approach is to find the reverse of the given number a
7 min read
Check if the frequency of all the digits in a number is same
Given a positive number 'N', the task is to find whether 'N' is balanced or not. Output 'YES' if 'N' is a balanced number else 'NO'. A number is balanced if the frequency of all the digits in it is same i.e. all the digits appear the same number of times. Examples: Input: N = 1234567890 Output: YES
8 min read
Recursive sum of digits of a number formed by repeated appends
Given two positive number N and X. The task is to find the sum of digits of a number formed by N repeating X number of times until sum become single digit. Examples : Input : N = 24, X = 3 Output : 9 Number formed after repeating 24 three time = 242424 Sum = 2 + 4 + 2 + 4 + 2 + 4 = 18 Sum is not the
5 min read