Armstrong Number Program In C (5 Different Way To Make)

In this article, we are going to write an Armstrong number program in C

We will make this program in the following way -:

  1. C Program To Find Armstrong Numbe Using between 1 to 1000 (Using For Loop)
  2. C Program To Check Armstrong Number Using While Loop
  3. C Program To Check Armstrong Number Using Do While Loop
  4. C Program To Check Armstrong Number Using Function
  5. C Program To Check Armstrong Number Using Recursion

To make this program, we will use the following concept given below.

If you do not know about these topics well, then you may not understand this program, so you should know about them in detail from the link given above.

Now without wasting time let’s start programming.

Armstrong Number Program In C

Armstrong Number Program In C Using For Loop

Algorithm

  • Program Start
  • Declare Variable
  • Input Numbers
  • Check Condition
  • Print output according to condition
  • Program End

Program

//C Program To Check Armstrong Number Using For Loop

#include<conio.h>
int main()
{
 int n,r,s,x,num;

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

 printf("armstrong number Between 1 to %d :\n",num );
 for(n=1;n<=num;n++)
 {
  x=n;
  s=0;

  while(x!=0)
  {
    r=x%10;
    s=s+(r*r*r);
    x=x/10;
   }
   if(s==n)
    printf("%d\n",n);
 }
    return 0;
}

Output

Enter a number : 1000
armstrong number between 1 to 1000 :
1
153
370
371
407

Armstrong Number Program In C Using While Loop

Program

//C Program To Check Armstrong Number Using While Loop

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

Output

Enter the number : 1000
not armstrong number

C Program To Check Armstrong Number Using Do While Loop

Program

//C Program To Check Armstrong Number Using do While Loop

#include<stdio.h>
void main()
{
//variable declaration
int n,r,sum=0,t;

//input number
printf("Enter the number : ");
scanf("%d",&n);

t=n;

do
{
  r=n%10;
  sum=sum+(r*r*r);
  n=n/10;
}while(n>0);
if(t==sum)
    printf("armstrong  number ");
else
     printf("not armstrong number");

}

Output

Enter the number : 153
armstrong number

C Program To Check Armstrong Number Using Function

Program

//C Program To Check Armstrong Number Using Function

#include<stdio.h>
void arm(int);
int main()
{
int n;
printf("Enter the number : ");
scanf("%d",&n);
arm(n);
return 0;
}
void arm(int n)
{
int r,sum=0,t;
t=n;

while(n>0)
{
  r=n%10;
  sum=sum+(r*r*r);
  n=n/10;
}
if(t==sum)
    printf("armstrong  number ");
else
     printf("not armstrong number");
}

Output

Enter the number : 370 
armstrong number

C Program To Check Armstrong Number Using Recursion

Program

//C Program To Check Armstrong Number Using Recursion

#include<stdio.h>
#include<math.h>
int check_ArmstrongNumber(int num)
{
    if(num>0)
    return (pow((num%10),3) + check_ArmstrongNumber(num/10));
}
int main()
{
    int num, sum;
    printf("Enter a number: ");
    scanf("%d",&num);
    sum = check_ArmstrongNumber(num);
   if(sum==num)
    printf("It is an Armstrong Number");
   else
    printf("It is not an Armstrong Number");
}

Output

Enter the number : 407 
It is  an Armstrong Number

Conclusion

So friends, in this article, we have written an Armstrong number program in C.

  • C Program To Find Armstrong Number Between 1 to 1000 (Using For Loop)
  • C Program To Check Armstrong Number Using While Loop
  • C Program To Check Armstrong Number Using Do While Loop
  • C Program To Check Armstrong Number Using Function
  • C Program To Check Armstrong Number Using Recursion

If you have difficulty to understand any program or have any doubts or questions, then tell me in the comment below.

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.