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

// CPP Program to Print Armstrong Number Between 1 to n using While Loop

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

using namespace std;

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;
	cout << "Enter the number :--> ";
	cin >> n;

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

	return 0;
}

Output:

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