In this tutorial you will learn about the C++ Program to Display Armstrong Number Between Two Numbers and its application with practical example.
C++ Program to Display Armstrong Number Between Two Numbers
In this tutorial, we will learn to create a C++ program that will Display Armstrong Number Between Two Numbers in C++ programming.
Prerequisites.
Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:
- Operators in C++ Programming.
- Basic Input and Output function in C++ Programming.
- Basic C++ programming.
- While loop in C++ programming.
Program to Display Armstrong Number Between Two Numbers:-
In today’s tutorial, we will create a program that will find the Armstrong numbers between the given number range. First will take the number frame from the user and then will find the Armstrong Number.
With the help of this program, we can Display Armstrong Number Between Two Numbers.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declare the variables for the program. 2. Taking the input numbers from and to by the user. 3. Calculating the <strong>Armstrong Numbers</strong>. 4. Print the Result. 5. End the program. |
Program to Display Armstrong Number Between Two Numbers:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <iostream> using namespace std; int main() { //Decalring the variables for the program int num1, num2, i, num, digit, sum; //Taking input number first for the starting range cout << "Enter first number: "; cin >> num1; //Taking input number second for the ending range cout << "Enter second number: "; cin >> num2; //Finfing the Armstrong numbers in the input range cout << "Armstrong numbers between " << num1 << " and " << num2 << " are: " << endl; for(i = num1; i <= num2; i++) { sum = 0; num = i; for(; num > 0; num /= 10) { digit = num % 10; sum = sum + digit * digit * digit; } if(sum == i) { //printing the Armstrong numbers cout << i << endl; } } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- num1 = it will hold the integer value.
- num2 = it will hold the integer value.
- flag = it will hold the integer value.
- digit = it will hold the integer value.
- num = it will hold the integer value.
- sum = it will hold the integer value.
- i = it will hold the integer value.
Taking the input integer number interval from the user.