According to the concept of armstrong numbers a number is a armstrong number when a number is equal to the sum of it’s own digits raised to the power of the number of digits it contains. For example numbers like 153(1^3+5^3+3^3), 407(4^3+0^3+7^3) etc. In this article we are going to print all armstrong numbers in the given range.
Table of Contents
C Program to Print all Armstrong Numbers
Let’s write a C program that prints all armstrong number in the given range.
#include <stdio.h>
#include <math.h>
void main()
{
int start, end;
printf("Enter the start of range");
scanf("%d", &start);
printf("Enter the end of range");
scanf("%d", &end);
for (int i = start; i <= end; i++)
{
int num = i;
int sum = 0;
int digits = (int)log10(num) + 1;
while (num > 0)
{
int digit = num % 10;
sum = sum + pow(digit, digits);
num = num / 10;
}
if (sum == i)
{
printf("%d ", i);
}
}
}
Output
Happy Coding
2 thoughts on “Armstrong Numbers in C”