C++ Program to Print First n Armstrong Number using For loop

Write C++ Program to Print First n Armstrong Number using For loop

// CPP Program to Print First n Armstrong Number using For 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));

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

	long int i = 1 ;
	int Count = 0;

	cout << "First " << n << " Armstrong Numbers Are :--> ";

	for(; Count < n; )
	{
		if(isArmstrong(i))
		{
			cout << i << " ";
			Count++;
		}
		i++;
	}

	return 0;
}

Output:

Enter the number ( < 6) :--> 5

First 5 Armstrong Numbers Are :--> 1 153 370 371 407

≪ Previous | Next ≫