armstrong number in c

C Program to Check Armstrong Number

A number can be called as an Armstrong number only if the sum of cubes of its digits is equal to the number itself.
Here in this c program, we are going to check whether the input of the number is Armstrong or not.

Let’s take a case where an Armstrong number consists of 3 digits. so let us try to understand with an example considering the number to be 153

example:

153 = (1*1*1) + (5*5*5) + (3*3*3)  
That is:  
(1*1*1) = 1  
(5*5*5) = 125  
(3*3*3) = 27  
So:  
1 + 125 + 27 = 153

hence we can Consider 153 as an Amstrong number.

Now let us move to our check out our program to check Armstrong Number in C.

Example #1: Check Armstrong Number of three digits

#include <stdio.h>
int main() {
int number, originalNumber, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0){
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}
if(result == number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);
return 0;
}

 

Output

Enter a three-digit integer: 153
371 is an Armstrong number.

Example #2: Check Armstrong Number of n digits

#include<stdio.h>  
int main()  {    
int n,r,sum=0,temp;    
printf("enter the number=");    
scanf("%d",&n);    
temp=n;    
while(n>0)   {    
r=n%10;    
sum=sum+(r*r*r);    
n=n/10;    
}    
if(temp==sum)    
printf("armstrong  number.");    
else    
printf("not armstrong number");    
return 0;  
}

Output

Enter an integer: 1221
armstrong number.

In short, this c program tends to calculate the number of digits of an integer and stores in a variable named n. And then uses the pow() function to find the power of individual digits using while loop.

nv-author-image

Anurag batra

A Tech Enthusiast who loves to work on his craft and make unique stuff From his Programming Skill at a same time Sharing that Knowledge with the outside world to help others.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments