C++ Program to Check if Given Number is Armstrong or Not using For Loop

Write C++ Program to Check if Given Number is Armstrong or Not using For Loop

// CPP Program to Check if Given Number is Armstrong or Not using For Loop

#include <iostream>

using namespace std;

int main()
{
	int n, ans = 0;

	cout << "Enter the number:--> ";
	cin >> n;

	int temp = n;
	for(; n != 0; )
	{
		int remainder = n % 10;
		ans = ans + (remainder * remainder * remainder);
		n = n / 10;
	}
	if(temp == ans)
	{
		cout << "The given number is an Armstrong number";
	}
	else
	{
		cout << "The given number is not an Armstrong number";
	}

	return 0;
}

Output:

Run 1:
Enter the number:--> 372
The given number is not an Armstrong number

Run 2:
Enter the number:--> 371
The given number is an Armstrong number

≪ Previous | Next ≫