The intent of this page is to collection the best program and help programmers to find good blog posts to read. Some of these blogs may not be written by Java developers, but Java developers should find it useful or interesting. Reading those blogs should be fun and often bring some fresh ideas

Sunday, February 9, 2020

C program : Armstrong Number up to Nth place

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

No comments:

Post a Comment

If you have any doubt . Please let me know

String Operation

String Operation Using collection Framework Sorting Order  public static void sortAccending(String s) { char[] arr = s.toCharArray(); ...

Adbox