C Topics
- C Intro
- C String Functions
- C Math Functions
- C Header Files
- C Interview Programs
- Abundant Number
- Amicable Number
- Armstrong Number
- Average of N Numbers
- AutomorphiNumber
- Biggest of three numbers
- Binary to Decimal
- Common Divisors
- Composite Number
- Condense a Number
- Cube Number
- Decimal to Binary
- Decimal to Octal
- Disarium Number
- Even Number
- Evil Number
- Factorial of a Number
- Fibonacci Series
- GCD
- Happy Number
- Harshad Number
- LCM
- Leap Year
- MagiNumber
- Matrix Addition
- Matrix Division
- Matrix Multiplication
- Matrix Subtraction
- Matrix Transpose
- Maximum Value of an Array
- Minimum Value of an Array
- Multiplication Table
- Natural Number
- Number Combination
- Odd Number
- Palindrome Number
- Pascalβs Triangle
- Power of 2
- Power of 3
- ProniNumber
- Perfect Number
- Perfect Square
- Prime Factor
- Prime Number
- Smith Number
- Strong Number
- Sum of Array
- Sum of Digits
- Swap Two Numbers
- Triangular Number
- C Star Pattern
- C Number Pattern
- C Alphabet Pattern
C Program to Check Disarium Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, exploring mathematical patterns is a fascinating endeavor. One such numeric pattern is the Disarium number.
A Disarium number is a number defined by the sum of its digits each raised to the power of its respective position.
In this tutorial, we'll delve into a C program designed to check whether a given number is a Disarium number.
π Example
Let's explore the C code that checks whether a given number is a Disarium number.
#include <stdio.h>
#include <math.h>
// Function to count the number of digits in a given number
int countDigits(int number) {
int count = 0;
while (number != 0) {
count++;
number /= 10;
}
return count;
}
// Function to check if a number is a Disarium number
int isDisarium(int number) {
int originalNumber = number;
int digitCount = countDigits(number);
int sum = 0;
while (number != 0) {
int digit = number % 10;
sum += pow(digit, digitCount);
digitCount--;
number /= 10;
}
return (sum == originalNumber);
}
// Driver program
int main() {
// Replace this value with your desired number
int inputNumber = 89;
// Call the function to check if the number is Disarium
if (isDisarium(inputNumber)) {
printf("%d is a Disarium number.\n", inputNumber);
} else {
printf("%d is not a Disarium number.\n", inputNumber);
}
return 0;
}
π» Testing the Program
To test the program with different numbers, replace the value of inputNumber in the main function.
89 is a Disarium number.
Compile and run the program to check if the number is a Disarium number.
π§ How the Program Works
- The program defines a function countDigits to count the number of digits in a given number.
- The isDisarium function checks if a number is a Disarium number by summing the digits each raised to the power of its position.
- The main function tests the isDisarium function with a sample number (replace it with your desired number).
π Between the Given Range
Let's dive into the C code that checks for Disarium numbers in the specified range.
#include <stdio.h>
#include <math.h>
// Function to calculate the number of digits in a number
int countDigits(int num) {
int count = 0;
while (num != 0) {
num /= 10;
++count;
}
return count;
}
// Function to check if a number is a Disarium number
int isDisarium(int num) {
int originalNum = num;
int digitCount = countDigits(num);
int sum = 0;
while (num != 0) {
int digit = num % 10;
sum += pow(digit, digitCount);
num /= 10;
--digitCount;
}
return (sum == originalNum);
}
// Driver program
int main() {
printf("Disarium numbers in the range 1 to 100:\n");
for (int i = 1; i <= 100; ++i) {
if (isDisarium(i)) {
printf("%d ", i);
}
}
return 0;
}
π» Testing the Program
Disarium numbers in the range 1 to 100: 1 2 3 4 5 6 7 8 9 89
Run the program to check for Disarium numbers in the range from 1 to 100.
π§ How the Program Works
- The program defines a function countDigits that calculates the number of digits in a given number.
- It also defines a function isDisarium that checks if a number is a Disarium number.
- The main function iterates through numbers from 1 to 100 and prints the Disarium numbers in the specified format.
π§ Understanding the Concept of Disarium Numbers
Before we dive into the code, let's grasp the concept of Disarium numbers.
A Disarium number is a number such that the sum of its digits, each raised to the power of its position, equals the number itself.
For example, the number 89 is a Disarium number because 8^1 + 9^2 equals 89.
π Conclusion
Understanding and implementing programs to identify numeric patterns, such as Disarium numbers, is a valuable skill in the world of programming.
The provided C program offers a practical example of checking whether a given number follows the Disarium pattern.
Feel free to use and modify this code for your specific use cases. 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 Disarium Number), please comment here. I will help you immediately.