In this article, we will write a C language program to check whether a given number is Armstrong number or not.
An Armstrong number is n digit number that sum of its individual digit’s power n equals that number. For example, 370 is an Armstrong number because: (3)^3 + (7)^3 + (0)^3 = 370
Here we have taken power 3 because the number 370 is 3 digit number. If we take a 4 digits number of new power will be 4 to calculate. For example, the power for the number 1667 will be 4.
Read Also: Download and Install Turbo C++ for Windows 10 (Full Installation Guide)
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int number, sum = 0, rem = 0, power = 0, temp, len_temp, len = 0;
printf ("Enter a number to check armstrong number: ");
scanf("%d", &number);
temp = number;
len_temp = temp;
while (len_temp != 0){
len_temp /= 10;
len++;
}
while (number != 0){
rem = number % 10;
power = pow(rem, len);
sum = sum + power;
number = number / 10;
}
if (sum == temp)
printf ("The given number is a valid armstrong number\n");
else
printf ("The given number is not a valid armstrong number\n");
printf("Please Enetr Any Key To Continue");
getch();
clrscr();
} Explanation
This program works for a range of 1 to 1000. You can even explore more by taking a big integer.
I hope this post helps you understand Armstrong’s number and its implementation in the C programming language.
Keep coding 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…