C Basic
C Interview Programs
- C Interview Programs
- C Abundant Number
- C Amicable Number
- C Armstrong Number
- C Average of N Numbers
- C Automorphic Number
- C Biggest of three numbers
- C Binary to Decimal
- C Common Divisors
- C Composite Number
- C Condense a Number
- C Cube Number
- C Decimal to Binary
- C Decimal to Octal
- C Disarium Number
- C Even Number
- C Evil Number
- C Factorial of a Number
- C Fibonacci Series
- C GCD
- C Happy Number
- C Harshad Number
- C LCM
- C Leap Year
- C Magic Number
- C Matrix Addition
- C Matrix Division
- C Matrix Multiplication
- C Matrix Subtraction
- C Matrix Transpose
- C Maximum Value of an Array
- C Minimum Value of an Array
- C Multiplication Table
- C Natural Number
- C Number Combination
- C Odd Number
- C Palindrome Number
- C Pascalβs Triangle
- C Power of 2
- C Power of 3
- C Pronic Number
- C Perfect Number
- C Perfect Square
- C Prime Factor
- C Prime Number
- C Smith Number
- C Strong Number
- C Sum of Array
- C Sum of Digits
- C Swap Two Numbers
- C Triangular Number
C Program to Check Magic Number
Photo Credit to CodeToFun
π Introduction
In the world of programming, the concept of magic numbers is a fascinating one.
A magic number is a number in which the eventual sum of its digits, when the sum is recursively calculated, leads to a single-digit number that is 1. If the final sum is not 1, the number is not considered magic.
In this tutorial, we will explore a simple yet insightful C program that checks whether a given number is a magic number or not.
π Example
Let's delve into the C code that checks for the magic number property.
#include <stdio.h>
// Function to check if a number is magic
int isMagicNumber(int num) {
while (num > 9) {
int sum = 0;
// Calculate the sum of digits
while (num > 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
// Check if the final sum is 1
return (num == 1);
}
// Driver program
int main() {
// Replace this value with your desired number
int number = 19;
// Check if the number is magic
if (isMagicNumber(number)) {
printf("%d is a Magic Number.\n", number);
} else {
printf("%d is not a Magic Number.\n", number);
}
return 0;
}
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the main function.
19 is a Magic Number.
Compile and run the program to see whether the number is a magic number or not.
π§ How the Program Works
- The program defines a function isMagicNumber that takes an integer as input and checks if it is a magic number.
- Inside the function, it iteratively calculates the sum of the digits of the number until the number becomes a single-digit number.
- It then checks if the final sum is equal to 1, indicating a magic number.
π Between the Given Range
Let's take a look at the C code that checks for Magic Numbers within the specified range.
#include <stdio.h>
// Function to check if a number is a Magic Number
int isMagicNumber(int num) {
while (num > 9) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
return (num == 1);
}
// Driver program
int main() {
printf("Magic Numbers in the range 1 to 50:\n");
for (int i = 1; i <= 50; ++i) {
if (isMagicNumber(i)) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}
π» Testing the Program
Magic Numbers in the range 1 to 50: 1 10 19 28 37 46
Run the program to see the Magic Numbers within the range of 1 to 50.
π§ How the Program Works
- The program defines a function isMagicNumber that checks if a given number is a Magic Number.
- Inside the function, it uses a while loop to iteratively sum the digits until a single-digit number (1-9) is obtained.
- The main function iterates through numbers from 1 to 50 and prints those that are Magic Numbers.
π§ Understanding the Concept of Magic Numbers
A magic number is a number in which the sum of its digits leads to a single-digit number, and that single-digit number is 1.
For example, the number 19 is a magic number because the sum of its digits (1 + 9) is 10, and the sum of 1 and 0 is 1.
π’ Optimizing the Program
While the provided program is effective, consider exploring and implementing optimizations or additional features based on your specific requirements.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (C Program to Check Magic Number), please comment here. I will help you immediately.