Q:

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

belongs to collection: Basic C Programming Examples

0

you have to 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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Basic C Programming Examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program To Swap Two Numbers (7 Different Ways)... >>
<< Prime Number Program In C (3 Simple Ways)...