Write a C program to check whether the give number is Armstrong or not

Previous
Next

An Armstrong number is a n-digit number that is equal to the sum of the nth power of its digits.

For example –

6 = 6^1 = 6

371 = 3^3 + 7^3 + 1^3 = 371

153 = 1^3 + 5^3 + 3^3 = 153

#include <stdio.h>
#include <math.h>
int main()
{
	int n, temp, r, d=0, sum;
	printf("Enter number : ");
	scanf("%d", &n);

	sum = 0;
	temp = n;
	while(n>0)
	{
		n=n/10;
		d++;
	}
	n = temp;
	while(n>0)
	{
		r = n%10;
		sum = sum + pow(r,d);
		n= n/10;
	}
	n = temp;
	if(n == sum)
		printf("%d is ARMSTRONG NUMBER \n", n);
	else
		printf("%d is NOT ARMSTRONG NUMBER \n", n);
	return 0;
}
Previous
Next

Courses Enquiry Form