Learn Programming and technology with ease @ developerpublish.com

HomeC++C++ Program to Check Armstrong Number

# C++ Program to Check Armstrong Number

In this post, you will learn how to Check Armstrong Number using C++ programming language.

This lesson will teach you how to Check Armstrong Number, with a while loop, assignment operator and decision making statement using the C++ Language. Letâ€™s look at the below source code.

## Source Code

```#include <iostream>
using namespace std;
int main()
{
int n,r,sum=0,temp;
cin>>n;
cout<<"Enter the Number = "<<n<<endl;
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
cout<<"\n"<<temp<<" is a Armstrong Number."<<endl;
else
cout<<"\n"<<temp<<" is not a Armstrong Number."<<endl;
return 0;
}  ```

`370`

## Output

```Enter the Number = 370
370 is a Armstrong Number.```

The statementsÂ #include<iostream>, using namespace std, int mainÂ are the main factors that support the function of the source code. Now we can look into the working and layout of the codeâ€™s function.

1. An Armstrong number, also known as narcissistic number, is a number that is equal to the sum of the cubes of its own digits. For example,Â 370Â is an Armstrong number since 370 = 3*3*3 + 7*7*7 + 0*0*0 .Â
2. Declare the variables and strings n,r,sum,tempanÂ integersÂ and assign the value 0 to sum.
3. Collect the number from the user and store it in n using function `cin>>`Â  and display the value using `cout<<`Â and the Insertion Operators'<<â€˜ , â€˜>>â€™.
4. Using theÂ assignment operatorÂ assign `temp=n` and create aÂ while loopÂ with the condition `(n>0)`
5. Create the loop statement with the following functions `r=n%10` to find the reminder of n and 10 and store the value inÂ r, perform the mathematical function `sum=sum+(r*r*r)`and store the value in sum.
6. `n=n/10` is the final function in the loop statement and the answer is stored in n.
7. The loop is executed multiple times until the condition is false and the loop is exited.
8. Using theÂ decision making if else statement with the condition `(temp==sum)` , display the respective output statement according to whether the condition is satisfied or not.

Note: The â€˜ << endl â€˜ in the code is used to end the current line and move to the next line and â€˜\nâ€™ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.

Share:

### You May Also Like

#### C++ Program to Generate Multiplication Table

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
• C++
• January 30, 2022

#### C++ Program to Display Fibonacci Sequence

In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
• C++
• January 30, 2022

#### C++ Program to Find GCD of two Numbers

In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
• C++
• January 30, 2022