C++ Program to Print Armstrong Number Between n and m using For Loop

Write C++ Program to Print Armstrong Number Between n and m using For Loop

// CPP Program to Print Armstrong Number Between n and m using For Loop

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

using namespace std;

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

int main()
{
	int n,m;
	cout << "Enter n :--> ";
	cin >> n;

	cout << "Enter m :--> ";
	cin >> m;

	cout << "Armstrong Numbers Between " << n << " to " << m << " :--> ";
	int i = n ,j = m;
	for(; i < j; i++ )
	{
		if(isArmstrong(i))
		{
			cout << i << " ";
		}
	}

	return 0;
}

Output:

Enter n :--> 300
Enter m :--> 400

Armstrong numbers between 300 to 400 :--> 370 371

≪ Previous | Next ≫