Example #1: Check Armstrong Number of Three Digits
Introduction
In this section, we’ll explore how to check if a number of three digits is an Armstrong number or not. We’ll discuss the concept of Armstrong numbers, provide an algorithm to determine whether a number meets the criteria, and present C code examples with outputs.
Armstrong Number
An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits, each raised to the power of the number of digits. For example, 153 is an Armstrong number because:
Algorithm to Check Armstrong Number of Three Digits
To determine if a three-digit number is an Armstrong number:
- Take an input number
num
. - Extract each digit from
num
. - Calculate the sum of the cubes of the individual digits.
- Compare the sum with the original number
num
. - If the sum equals
num
, it’s an Armstrong number; otherwise, it’s not.
C Program for Checking Armstrong Number of Three Digits
#include <stdio.h>
#include <math.h>
int main() {
int num, originalNum, remainder, result = 0, n = 0;
printf("Enter a three-digit number: ");
scanf("%d", &num);
originalNum = num;
// Finding the number of digits
while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;
// Check if num is Armstrong number
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);
return 0;
}
Output Example
Input: 153
Enter a three-digit number: 153
153 is an Armstrong number.
Input: 370
Enter a three-digit number: 370
370 is an Armstrong number.
Input: 123
Enter a three-digit number: 123
123 is not an Armstrong number.
Example #2: Check Armstrong Number of n Digits
Introduction
In this section, we’ll extend the concept of Armstrong numbers to any number of digits. We’ll provide a generalized algorithm and demonstrate it with C code examples and outputs.
Algorithm to Check Armstrong Number of n Digits
To determine if a number of n digits is an Armstrong number:
- Take an input number
num
. - Find the number of digits in
num
. - Extract each digit from
num
. - Calculate the sum of the nth power of the individual digits.
- Compare the sum with the original number
num
. - If the sum equals
num
, it’s an Armstrong number; otherwise, it’s not.
C Program for Checking Armstrong Number of n Digits
#include <stdio.h>
#include <math.h>
int main() {
int num, originalNum, remainder, result = 0, n = 0;
printf("Enter a number: ");
scanf("%d", &num);
originalNum = num;
// Finding the number of digits
while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;
// Check if num is Armstrong number
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);
return 0;
}
Output Example
Input: 9474 (4-Digit Armstrong Number)
Enter a number: 9474
9474 is an Armstrong number.
Input: 1634 (4-Digit Armstrong Number)
Enter a number: 1634
1634 is an Armstrong number.
Input: 12345 (5-Digit Number, Not Armstrong)
Enter a number: 12345
12345 is not an Armstrong number.
Conclusion
In this blog post, we discussed the concept of Armstrong numbers and provided C programs to check if a given number (of either three digits or n digits) is an Armstrong number. These examples illustrate how to implement the Armstrong number check in C and verify