C Program to Check if Given Number is Armstrong or Not using For Loop

C Program to Check if Given Number is Armstrong or Not using For Loop

Write a Program to Check if Given Number is Armstrong or Not using For Loop

// C Program to Check if Given Number is Armstrong or Not using For Loop

#include <stdio.h>

int main()
{
	int n, ans = 0;

	printf("Enter the number:--> ");
	scanf("%d", &n);

	int temp = n;
	for(; n != 0; )
	{
		int remainder = n % 10;
		ans = ans + (remainder * remainder * remainder);
		n = n / 10;
	}
	if(temp == ans)
	{
		printf("The given number is an Armstrong number");
	}
	else
	{
		printf("The given number is not an Armstrong number");
	}

	return 0;
}

Output:

Run 1:
Enter the number:--> 372
The given number is not an Armstrong number

Run 2:
Enter the number:--> 371
The given number is an Armstrong number