# include <stdio.h>
# include <conio.h>
# include <math.h>
void main ()
{
int a,b=0,sum=0;
long int n;
printf("Enter the number to check\n ");
scanf("%i",&n);
if(n<1)
{
printf ("\nThe number should be greater than 0");
}
else
{
a=n;
//counting the digits
while (a>0)
{
a=a/10;
b++;
}
a=n;
//adding up bth power of digits
while(a>0)
{
sum=sum+pow(( a%10) ,b);
a=a/10;
}
if(sum==n)
printf ("\nThe number is an ARMSTRONG number");
else
printf ("\nThe number is NOT an ARMSTRONG number");
}
getch();
}
Search This Blog
Showing posts with label Armstrong numbers. Show all posts
Showing posts with label Armstrong numbers. Show all posts
Program to Check Armstrong Number
An Armstrong number is a positive integer for which the sum of 'n'th power of its digits is equal to the number itself where n is the number of digits in the number. This is a c program to check whether a given number is an Armstrong number or not. For example, 153 is a 3 digit Armstrong number because 13+53+33=153.
Program to Display All Armstrong Numbers upto 1000
This post is for C program to display all Armstrong numbers less than 1000. But before all, I should tell what an Armstrong number is. An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. Consider an example. 153 is an Armstrong number. Since it is a 3 digit number, to check whether it is an Armstrong number or not, we should add the 3rd power digits together. So, in the program, we should have a code to determine number of digits in number, then add the nth powers (where n is the number of digits) of each digit in the number and check whether both the sum and original number are the same.
#include<stdio.h>
#include<math.h>
void main()
{
int n,sum,r,digits;
for(n=1;n<=1000;n++)
 {
digits=0;
r=n;
sum=0;
while(r!=0) //Counting Digits
{
digits++;
r/=10;
}
r=n;
while(r!=0) /*Adding Digits Raised To a Power That Is Equal To Number of Digits *
{
sum+=pow((r%10),digits);
r=r/10;
}
if(sum==n)
{
printf("%5d",sum);
}
}
}
Subscribe to:
Posts (Atom)