In this tutorial, we are going to create a program in C and C++ to check whether a user-entered number is an Armstrong number or not but first let's see what is an Armstrong number.

An Armstrong Number number is an n digit integer such that the sum of each digit to the power n is equal to the number itself, for example:

```
Ex-1 : 1634
n = 4
1^n + 6^n + 3^n + 4^n = 1634
Ex-2 : 371
n = 3
3^n + 7^n + 1^n = 371
```

Now let us see the code to check whether a number is Armstrong or not in C and C++.

#### C program to check whether a number is Armstrong or not.

```
#include<stdio.h>
#include<math.h>
int main(){
int n,y,len = 0,x,sum = 0;
printf("Enter a number: ");
scanf("%d",&n);
y = n; //copy variable to preserve the value of the entered input
//below while loop will calculate the number of digits in the number.
while(y!=0){
y = y/10;
len++; //length of the number
}
y = n;
while(n!=0){
x = n%10; //this expression will give us each digit of the given number.
sum += pow(x,len); //sum variable to calculate x1^n + x2^n +...+ xm^n
n = n/10;
}
if(sum == y)
printf("The number is an Armstrong Number.");
else
printf("Number is not an armstrong number.");
}
```

#### C++ program to check whether a number is Armstrong or not

```
#include<iostream>
#include<math.h>
using namespace std;
int main(){
int n,y,len = 0,x,sum = 0;
cout<<"Enter a number: ";
cin>>n;
y = n; //copy variable to preserve the value of the entered input
//below while loop will calculate the number of digits in the number.
while(y!=0){
y = y/10;
len++; //length of the number
}
y = n;
while(n!=0){
x = n%10; //this expression will give us each digit of the given number.
sum += pow(x,len); //sum variable to calculate x1^n + x2^n +...+ xm^n
n = n/10;
}
if(sum == y)
cout<<"The number is an Armstrong Number.";
else
cout<<"Number is not an Armstrong Number.";
}
```

**Output:**

```
Enter a number: 371
The number is an Armstrong Number.
--------------------------------
```

## Share your thoughts