Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers. Let's try to understand why 153 is an Armstrong number
#include<stdio.h>
int Digit_count(int no){
int res,c=0;
while(no){
res=res*10+(no%10);
c++;
no/=10;
}
return c;
}
int power(int n,int p){
int i,res=1;
for(i=1;i<=p;i++)
res=n*res;
return res;
}
int Armstrong(int number){
int sum=0,temp=number,rem,digit=Digit_count(number);
while(number){
rem=number%10;
sum+=(power(rem,digit));
number/=10;
}
if(sum==temp)
return 1;
else
return 0;
}
void upto_N_Armstrong(int n){
int i;
for(i=1;i<=n;i++){
if(Armstrong(i))
printf("%d\t",i);
continue;
}
}
void main(){
int num;
printf("Enter the Number: ");
scanf("%d",&num);
upto_N_Armstrong(num);
}
output:
Enter the Number: 10000
1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474
#include<stdio.h>
int Digit_count(int no){
int res,c=0;
while(no){
res=res*10+(no%10);
c++;
no/=10;
}
return c;
}
int power(int n,int p){
int i,res=1;
for(i=1;i<=p;i++)
res=n*res;
return res;
}
int Armstrong(int number){
int sum=0,temp=number,rem,digit=Digit_count(number);
while(number){
rem=number%10;
sum+=(power(rem,digit));
number/=10;
}
if(sum==temp)
return 1;
else
return 0;
}
void upto_N_Armstrong(int n){
int i;
for(i=1;i<=n;i++){
if(Armstrong(i))
printf("%d\t",i);
continue;
}
}
void main(){
int num;
printf("Enter the Number: ");
scanf("%d",&num);
upto_N_Armstrong(num);
}
output:
Enter the Number: 10000
1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474
No comments:
Post a Comment
If you have any doubt . Please let me know