C program to Print Armstrong Number Between 1 to n using While Loop

C program to Print Armstrong Number Between 1 to n using While Loop

Write C program to Print Armstrong Number Between 1 to n using While Loop

//Write a C program to Print Armstrong Number Between 1 to n using While 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));

	while(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;
	printf("Enter the number :--> ");
	scanf("%d", &n);

	int i = 1 ;
	printf("Armstrong numbers between 1 to %d :--> ", n);
	while(i < n)
	{
		if(isArmstrong(i))
		{
			printf("%d ",i);
		}
		i++;
	}

	return 0;
}

Output:

Enter the number :--> 1000

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