Loading...
C++ Program to Check Armstrong Number

C++ Program to Check Armstrong Number

In this example, we will learn a Program to check whether an integer (entered by the user) is a prime number or not using for loop and if...else statement.

To understand this example, you should have the knowledge of the following C++ programming topics:


Armstrong Number

A positive integer is called an Armstrong number (of order n) if

abcd... = an + bn + cn + dn + ...

In the case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. For example, 153 is an Armstrong number because

153 = 1*1*1 + 5*5*5 + 3*3*3 

Example 1: Program to Check Armstrong Number of 3 Digits

#include <iostream>
using namespace std;

int main() {
    int num, originalNum, remainder, result = 0;
    cout << "Enter a three-digit integer: ";
    cin >> num;
    originalNum = num;

    while (originalNum != 0) {
        // remainder contains the last digit
        remainder = originalNum % 10;
        
        result += remainder * remainder * remainder;
        
        // removing last digit from the orignal number
        originalNum /= 10;
    }

    if (result == num)
        cout << num << " is an Armstrong number.";
    else
        cout << num << " is not an Armstrong number.";

    return 0;
}

Output 1

Enter a positive integer: 371
371 is an Armstrong number.
Working

In the program, we iterate through the while loop until originalNum is 0.

In each iteration of the loop, the cube of the last digit of orignalNum is added to result.

remainder = originalNum % 10;        
result += remainder * remainder * remainder;

And, the last digit is removed from the orignalNum. When the loop ends, the sum of the individual digit's cube is stored in result.


Example 2: Program to Check Armstrong Number of n Digits

#include <iostream>
#include <cmath>
using namespace std;

int main() {
   int num, originalNum, remainder, n = 0, result = 0, power;
   cout << "Enter an integer: ";
   cin >> num;

   originalNum = num;

   while (originalNum != 0) {
      originalNum /= 10;
      ++n;
   }
   originalNum = num;

   while (originalNum != 0) {
      remainder = originalNum % 10;

      // pow() returns a double value
      // round() returns the equivalent int
      power = round(pow(remainder, n));
      result += power;
      originalNum /= 10;
   }

   if (result == num)
      cout << num << " is an Armstrong number.";
   else
      cout << num << " is not an Armstrong number.";
   return 0;
}

Output 1

Enter an integer: 1634
1634 is an Armstrong number.
Working

In this program, the number of digits of the entered number is calculated first and stored in n.

And, the pow() function computes the power of individual digits in each iteration of the while loop.


Next Example

We hope that this Example helped you develop better understanding of the concept of "Check Whether a number is Armstrong or not" in C++.

Keep Learning : )

In the next Example, we will learn about C++ Display Armstrong Numbers b/w Two Intervals.


- Related Topics