Wednesday, May 16, 2018

C program to check whether a given number is Armstrong number or not.


If  the sum of, each digit raised to the power of number of digits in the number, is equal to the number itself, then the number is called Armstrong number.
For example, 153=(13)+(53)+(33) . So 153 is an Armstrong number.

Program code
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
/*To check whether a number is Armstrong number.*/
long int number=0, check=0, temp=0, power=0, temp1, temp2;
clrscr();
printf("Please input a whole number to find out whether it is Armstrong number.\n");
scanf("%ld",&number);
temp=number;
/* Steps to find the number of digits start*/
temp1=0;
temp2=10;
while (temp2<pow(10,7))
{
power++;
if ((temp>temp1)&&(temp<temp2))
break;
if (temp1==0)
temp1=temp1+10;
else temp1=temp1*10;
temp2=temp2*10;
}
/* Steps to find the number of digits ends*/
/*Steps to calculate check start*/
for (temp=number;temp>0;temp=temp/10)
check=check+pow((temp%10),power);
/*Steps to calculate check ends*/
if (check==number)
printf("%ld is an Armstrong number.\n", number);
else
printf("%ld is not an Armstrong number.\n", number);
getch();
}

Example of output
Please input a whole number to find out whether it is Armstrong number.
153
153 is an Armstrong number.

No comments:

Post a Comment

Please write here, your opinion about this C program