Write a C++ program to display Armstrong numbers between two intervals.

Write a C++ Program to Display Armstrong Numbers Between Two Intervals

An Armstrong number is an important concept in number-based programming problems. It helps students understand loops, conditional statements, and mathematical logic in C++. In this tutorial by cslearning.org, we will learn how to write a simple and student-friendly C++ program to display Armstrong numbers between two given intervals.

This guide includes introduction, logic, simple C++ source code, sample output, and viva questions with answers.

1. Introduction of Armstrong Number

An Armstrong number is a number that is equal to the sum of the cubes of its digits (for 3-digit numbers).

Example:

153
1³ + 5³ + 3³
= 1 + 125 + 27
= 153

So, 153 is an Armstrong number.

Other examples:
370
371
407

For simplicity, in this program we will check Armstrong numbers for three-digit numbers using cube logic.

2. Logic of the Program

To display Armstrong numbers between two intervals, follow these steps:

  1. Start the program.
  2. Take the starting and ending numbers from the user.
  3. Use a loop from start to end.
  4. For each number:
    • Store the number in a temporary variable.
    • Find each digit using % 10.
    • Calculate the cube of each digit and add it.
    • Remove the last digit using / 10.
  5. Compare the sum with the original number.
  6. If equal, print the number as an Armstrong number.
  7. Repeat until the loop ends.

Example:
For number 153
Digit = 3 → 3³ = 27
Digit = 5 → 5³ = 125
Digit = 1 → 1³ = 1
Sum = 153
Since sum equals original number, it is an Armstrong number.

3. C++ Source Code (Easy Student Friendly Code)

#include <iostream>
using namespace std;

int main()
{
    int start, end;

    cout << "Enter starting number: ";
    cin >> start;

    cout << "Enter ending number: ";
    cin >> end;

    cout << "Armstrong numbers between " << start << " and " << end << " are:" << endl;

    for(int num = start; num <= end; num++)
    {
        int temp = num;
        int sum = 0;
        int remainder;

        while(temp != 0)
        {
            remainder = temp % 10;
            sum = sum + (remainder * remainder * remainder);
            temp = temp / 10;
        }

        if(sum == num)
        {
            cout << num << endl;
        }
    }

    return 0;
}

Explanation:
The outer for loop checks each number between the given intervals.
The while loop extracts digits one by one.
Each digit is cubed and added to sum.
If sum equals the original number, it is an Armstrong number.

4. Sample Output

Example:

Enter starting number: 100
Enter ending number: 500
Armstrong numbers between 100 and 500 are:
153
370
371
407

5. Viva Questions with Answers

Q1: What is an Armstrong number?
Answer: An Armstrong number is a number that is equal to the sum of the cubes of its digits (for 3-digit numbers).

Q2: Which operator is used to extract digits?
Answer: The modulus operator % is used to extract digits.

Q3: Which loop is used to check numbers in the interval?
Answer: A for loop is used.

Q4: Why do we use a temporary variable?
Answer: To preserve the original number while extracting its digits.

Q5: Can this program be modified for any number of digits?
Answer: Yes, it can be modified by calculating the power according to the number of digits.

This program helps beginners understand loops, number logic, and conditional statements in C++. Practice more programs on cslearning.org to improve your programming skills.

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top