Write a C program given number Armstrong number or not (only three digits)

Write a C program given number Armstrong number or not (only three digits)

Examples of Armstrong Numbers: 0, 1, 2, 3, 153, 370, 407 etc.

Program:

#include<stdio.h>
int main(){
int num,a,p,total=0;
printf(“Enter a number:”);
scanf(“%d”,&num);
a=num;
while(a!=0){
p=a%10;
total=total+(p*p*p);
a=a/10;
}
if(total == num){
printf(“%d is a Armstrong number.\n”,num);
}else{
printf(“%d is not a Armstorng number.\n”,num);
}
return 0;
}

Output:

Enter a number:371
371 is a Armstrong number.

N digits:

Program:

#include <stdio.h>
#include <math.h>
int main()
{
int number, a, p, total = 0, b = 0 ;
printf(“Enter an integer: “);
scanf(“%d”, &number);
a = number;
while (a != 0)
{
a =a/10;
++b;
}
a = number;
while (a != 0)
{
p = a%10;
total = total + pow(p, b);
a=a/10;
}
if(total==number){
printf(“%d is an Armstrong number.\n”,number);
}else{
printf(“%d is not an Armstorng number.\n”,number);
}
return 0;
}

Output:

Enter an integer: 8208
8208 is an Armstrong number.

No Comments

Post a Reply