C PROGRAM
Find the armstrong numbers within a given range |C Program|
Introduction
Armstrong numbers, also known as narcissistic numbers or pluperfect digital invariants, are a fascinating concept in number theory. This C program exemplifies how to find Armstrong numbers within a given range. The blog post will delve into the significance of Armstrong numbers and explain the implementation of this program step by step.
Armstrong Numbers:
An Armstrong number (or narcissistic number) is a number that is the sum of its own digits, each raised to the power of the number of digits. For example, 153 is an Armstrong number because: 1^3 + 5^3 + 3^3 = 153.
Code
#include<stdio.h>
#include<conio.h>
void main() {
int num, i, rem, up, low, temp;
clrscr();
// Input
printf("Enter the lower and upper limit: ");
scanf("%d%d", &low, &up);
// Finding Armstrong Numbers
printf("Armstrong Numbers in the range %d to %d are: ", low, up);
for (i = low; i <= up; i++) {
temp = i;
num = 0;
// Checking if the number is Armstrong
while (temp != 0) {
rem = temp % 10;
num = num + (rem * rem * rem);
temp = temp / 10;
}
if (i == num) {
printf("%d\t", i);
}
}
getch();
}
Output
Enter the lower and upper limit: 100 400
Armstrong Numbers in the range 100 to 1000 are: 153 370
Explanation
1. Header Files:
The program includes the standard input/output header <stdio.h> and the console input/output header <conio.h>.
2. Main Function:
The main function serves as the entry point of the program.
3. Input:
Users input the lower and upper limits for the range.
4. Finding Armstrong Numbers:
The program iterates through the numbers within the specified range.
For each number, it checks if it is an Armstrong number by summing the cubes of its digits.
5. Output:
The program outputs the Armstrong numbers found within the specified range.
Conclusion
This C program provides a practical example of identifying Armstrong numbers. Understanding Armstrong numbers can be both interesting and useful in various mathematical and programming contexts.
Feel free to run the program with different ranges to observe how it correctly identifies Armstrong numbers. This example serves as a valuable introduction to Armstrong numbers in C. Happy coding!
Post a Comment
0 Comments