C Program to Print Armstrong Number Between 1 to n using For Loop

Write a Program to Print Armstrong Number Between 1 to n using For Loop

// C Program to Print Armstrong Number Between 1 to n using For Loop

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

bool isArmstrong(int n)
{
	int temp = n,ans = 0, rem;
	int digits = ceil(log10(n));

	for(; n != 0; )
	{
		int remainder = n % 10;
		ans = ans + (remainder * remainder * remainder);
		n = n / 10;
	}
	if(temp == ans)
	{
		return true;
	}
	return false;
}

int main()
{
	int n, i;
	printf("Enter Number :--> ");
	scanf("%d", &n);

	printf("Armstrong Numbers B1 to %d :--> ", n);
	for(i = 1; i < n; i++)
	{
		if(isArmstrong(i))
		{
			printf("%d ",i);
		}
	}

	return 0;
}

Output:

Enter the number :--> 1000
Armstrong numbers between 1 to 1000 :--> 1 153 370 371 407

≪ Previous | Next ≫