Checking Armstrong Numbers in C++

Beginner

Introduction

In this lab, we will learn how to check if a number is an Armstrong number or not in C++. An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits. This is a simple program that can help you practice your C++ coding skills.

Include necessary libraries

We will create a new file named main.cpp in the ~/project directory using the following command:

touch ~/project/main.cpp

In this step, we will include the necessary libraries.

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

The iostream library is used for working with input/output streams, while the cmath library adds functionality for mathematical operations.

Define the main() function

In this step, we will define the main() function.

int main() {
    // code here
    return 0;
}

The main function is the entry point of the program. All the code to be executed must be written inside this function.

Declare and intialize variables

In this step, we will declare and initialize the required variables for the program.

int num, originalNumber, remainder, result = 0, n = 0;
  • num is the input number
  • originalNumber is the un-modified input number
  • remainder stores the last digit of the input number
  • result stores the result of the Armstrong calculation
  • n is used to determine the number of digits in the input number

Take user input

In this step, we will take the input value from the user.

cout << "Enter a positive integer: ";
cin >> num;

This code snippet takes input from the user and stores it in the variable num.

Determine the number of digits in the input number

In this step, we will determine the number of digits in the input number.

originalNumber = num;

// count number of digits
while (originalNumber != 0) {
    originalNumber /= 10;
    ++n;
}

This code block counts the number of digits in the input number. The number is stored in the variable n.

Calculate the result

In this step, we will calculate the result of the Armstrong calculation.

originalNumber = num;

while (originalNumber != 0) {
    remainder = originalNumber % 10;
    result += pow(remainder, n);
    originalNumber /= 10;
}

This code block iterates over each digit in the input number and performs the calculation for an Armstrong number. The result is stored in the variable result.

Check the result

In this step, we will check if the input number is an Armstrong number or not.

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

This code block outputs the result of the program. If the result is equal to the input number, the input number is an Armstrong number. Otherwise, it is not.

To run the code in the terminal:

g++ main.cpp -o main && ./main

Summary

In this lab, we have learned how to check if a number is an Armstrong number or not. We have used the cmath library to perform the calculations required for this task. We also learned how to iterate over the digits of a number and count them. With this basic program, you can further develop your C++ coding skills.

Other Tutorials you may like