Pages

Tuesday, December 27, 2011

Program to find Armstrong number

Definition :

From c's programming point of view, Those numbers whose sum of the cube of its digits is equal to that number are known as Armstrong numbers .
For example

153 = 1^3 + 5^3 + 3^3 = 1+ 125 + 9 =153
or
370 = 3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370

But in general the definition is :

Those numbers whose sum of its digits to power of number of its digits is equal to that number are known as Armstrong numbers.

Let k be the number of digits in a number, n, and d1,d2,d3,d4... be the digits of n.
n=d1k+d2k+d3k+d4k+...


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


#include<stdio.h>

void main(){
int num,sum=0,temp,i;
clrscr();
printf("Enter number: ");
scanf("%d",&num);

temp=num;
while(num!=0){
i=num%10;
num=num/10;
sum=sum+(i*i*i);

}

if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not a Armstrong number",temp);
getch();

}


Output:

Enter number: 153
153 is an Armstrong number

Enter number: 207
207 is not a Armstrong number

Armstrong number in c using for loop

#include<stdio.hgt;

void main(){
int temp,sum=0,i,num;
clrscr();

printf("Enter number : ");
scanf("%d",&num);

for(temp=num;num!=0;num=num/10){
i=num%10;
sum=sum+(i*i*i);

}
if(temp==sum)
printf("%d is an Armstrong Number ",temp);
else
printf("%d is not a Armstrong Number ",temp);
getch();

}

No comments: