In this tutorial you will learn about the C++ Program to Check Armstrong Number and its application with practical example.
C++ Program to Check Armstrong Number
In this tutorial, we will learn to create a C++ program that will Check Armstrong Number 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 Check Armstrong Number:–
In today’s tutorial, we will create a program that will find the Armstrong numbers from the given number. First will take the number from the user and then will find the Armstrong Number.
With the help of this program, we can Check Armstrong Number.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declare the variables for the program. 2. Taking the input numbers from the user. 3. Checking the Armstrong Numbers. 4. Print the Result. 5. End the program. |
Program to Check Armstrong Number:-
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 |
/* C++ Program to Check Armstrong Number */ #include <iostream> using namespace std; int main() { //declaring the variables for the program int num, ognum, rmdr, result = 0; //taking input number from the user to check the Armstrong number cout << "Enter a 3-digit integer: "; //Scanning the input number given by the user in program cin >> num; ognum = num; //checking number if its a arm strong number or not while (ognum != 0) { // remainder contains the last digit rmdr = ognum % 10; result += rmdr * rmdr * rmdr; // removing last digit from the orignal number ognum /= 10; } if (result == num) //printing the number is an arm strong number cout << num << " is an Armstrong number."; else //printing the number is not an arm strong number cout << num << " is not an Armstrong number."; return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- ognum = it will hold the integer value.
- rmdr = it will hold the integer value.
- result = it will hold the integer value.
- num = it will hold the integer value.
Taking the input integer number from the user.
Checking the Armstrong numbers.
Printing the output.