C Program to Check a number is a Magic Number or Not

Write a program to check if a number is a magic number or not

Note: find the sum of digits repeatedly until a single-digit occurs. If the final sum is 1, then it is called the magic number

Example:
406 => 4+0+6 = 10 => 1+0 = 1

Input Format
The input contains an integer N

Output Format
Print Magic Number or Not Magic Number

Constraint
1<=N<=1000000000

Sample Input 1

1072

Sample Output 1

Magic Number

Code:

#include<stdio.h> int main(){ int n,sum=0; scanf("%d",&n); while(n > 0 || sum > 9) { if(n == 0) { n = sum; sum = 0; } sum += n % 10; n /= 10; } if(sum==1) printf("Magic Number"); else printf("Not Magic Number"); return 0; }
Code language: C/AL (cal)

Leave a Comment