Write a program to check Armstrong number in C?

#include
#include
main()
{
int n,r,sum=0,temp; //declaration of variables.
clrscr(); //It clears the screen.
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”);
getch(); //It reads a character from the keyword.
}

Sure, here’s a C program to check if a number is an Armstrong number:

c
#include <stdio.h>
#include <math.h>

int isArmstrong(int num) {
int originalNum, remainder, n = 0, result = 0;

// Store the number of digits of num in n
originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
++n;
}

// Check if num is Armstrong
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}

if (result == num)
return 1; // If num is Armstrong
else
return 0; // If num is not Armstrong
}

int main() {
int num;

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

if (isArmstrong(num))
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);

return 0;
}

In this program:

  • The isArmstrong() function checks if the given number is an Armstrong number.
  • It calculates the number of digits in the given number and then calculates the sum of each digit raised to the power of the number of digits.
  • Finally, it compares the result with the original number to determine if it’s an Armstrong number.
  • In the main() function, the user is prompted to enter a number, and then the isArmstrong() function is called to check if the entered number is an Armstrong number or not.