Armstrong Number in C

What is an Armstrong number ?

A number equal to sum of the power of 3 of its digits. ( for 3 digit number )
A number equal to sum of the power of 4 of its digits. ( for 4 digit number )

A number equal to sum of the power of n of its digits. ( for n digit number )

Example :
153=13 + 53 + 33
So 153 is an Armstrong number

3 digit Armstrong number

Ask user to enter a 3 digit number and then display if it is an Armstrong number or not.
#include <stdio.h>
#include <math.h>
int main(void){

int n;
printf("Enter a three digit numbrer : ");
scanf("%d",&n);
int num=n; 
int sum=0;

while(n != 0){
sum = sum + pow((n%10),3);
n=n/10;
}

if(num==sum){
printf(" %d is an Armstrong number ",num);
}else{
printf(" %d is NOT an Armstrong number ",num);
}

Displaying all 3 digit Armstrong numbers

A list of 3 digit Armstrong numbers
#include <stdio.h>
#include <math.h>
int main(void){
int n,num,sum,i;

for(i=1;i<1000;i++){
n=i;
sum=0;  // At starting of loop it is intialized to 0

while(n != 0){
sum = sum + pow((n%10),3);
n=n/10;
} // end of while loop 

if(i ==sum){
printf(" %d is an Armstrong number \n",i);
}
}// end of for loop
return 0;
}
Output
 1 is an Armstrong number
 153 is an Armstrong number
 370 is an Armstrong number
 371 is an Armstrong number
 407 is an Armstrong number

n digit Armstrong numbers

Check the user entered number of any digits or length. We will find out the number of digits by using one for loop and using the variable no_digits.
#include <stdio.h>
#include <math.h>
int main(void){
int n,num,sum,no_digits=0,j=0;
printf("Enter a number : ");
scanf("%d",&num);
n=num;
sum=0;  // At starting of loop it is intialized to 0

for(j=num;j != 0;++no_digits){
j=j/10;
}
printf("\n No of digits : %d \n",no_digits);

while(n != 0){
sum = sum + pow((n%10),no_digits);
n=n/10;
} // end of while loop 

if(num ==sum){
printf(" %d is an Armstrong number \n",num);
}else{
printf(" %d is NOT an Armstrong number \n",num);
}

return 0;
}
List all armstrong numbers upto 10000 ( or n ) . As we are using one for loop , upper limit can be increased.
#include <stdio.h>
#include <math.h>
int main(void){
int n,num=0,sum,no_digits=0,j=0;


for(num=10;num<10000;num++){ // increse the upper limit. 
n=num;
sum=0;  // At starting of loop it is intialized to 0
no_digits=0;
for(j=num;j != 0;++no_digits){
j=j/10;
}
//printf("\n No of digits : %d \n",no_digits);

while(n != 0){
sum = sum + pow((n%10),no_digits);
n=n/10;
} // end of while loop 

if(num ==sum){
printf("\n %d is an Armstrong number \n",num);
}

} // end of for loop with i
return 0;
}
Output is here
 153 is an Armstrong number

 370 is an Armstrong number

 371 is an Armstrong number

 407 is an Armstrong number

 1634 is an Armstrong number

 8208 is an Armstrong number

 9474 is an Armstrong number

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    30-04-2022

    #include <stdio.h>
    #include <conio.h>
    int main()
    {
    int grade=74;

    if (grade>74) {
    printf(“Passed”);
    }
    else if (grade<75 && grade>69) {
    printf(“Conditional Pass”);
    }
    else (grade<70) {
    printf(“Failed!”);
    }
    getch();
    return 0;
    }

    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer