Check if a number is an Armstrong number or not in C++

Armstrong numbers are the numbers whose sum of third power of each digit is equal to the number itself. Calculating Armstrong numbers is fair easy but will become difficult if many numbers are to be checked at once. To make calculation easy, a program which is preloaded with the conditions for Armstrong number must be used.

This article explains how to check if a number is an Armstrong number or not in C++.

The libraries included

#include<iostream.h>
#include<conio.h>
void main()
{

In this program, there are two header files needed. They are <iostream.h> and <conio.h>. The <iostream.h> file is used to perform the basic input output functions in the program. Whereas, the <conio.h> file is used to perform input output functions in the console and to use the getch() function.

Use of getch()

The main() function is opened .

The Program

The program is divided into three parts for better understanding.

Part 1.




clrscr();
int arm=0,dig=0,n;
cout<<"Enter the number to be checked";
cin>>n;
int a=n;

In the first line,the function clrscr() is used.

Use of clrscr()

Then, three integer variables ‘arm’, ‘dig’ and ‘n’ are initialized. The value of ‘arm’ and ‘dig’ is set to zero,so that it does not use any garbage value. After that, the user is asked to enter the number which is to be checked for Armstrong condition in the third and fourth lines of the code. The number entered by the user is stored in the variable ‘n’. In the last line of part 1 code, the value of n is stored in integer variable ‘a’ or in other words a copy is generated.

Part 2.

while(n!=0)
{
  dig=n%10;
  arm=arm+(dig*dig*dig);
  n=n/10;
}

In part 2 of the code, a while loop is used. This loop will keep on running until the value of ‘n’ is not equal to zero. When ‘n’ equals to zero, the loop will be exited. In the while loop, the variable ‘dig’ is assigned the value of the remainder of (n/10). Taking an example, if n equals to 18. The value of (n%10) will be 8 which is the last digit of number ‘n’. Using this method the last digit of the variable ‘n’ is stored in variable ‘dig’. In the next line, the cube of the digit generated in the last step is added to the variable ‘arm’. And finally, the variable ‘n’ is divided by 10 and stored in n itself to delete out the last digit from the variable ‘n’ as the integer variable cannot consist digits after a decimal.

This loop will continue running until the cube of the first digit of the variable ‘n’ is added into the variable ‘arm’

Part 3.

if(a==arm)
{
  cout<<"The number entered is an Armstrong Number";
}
else
{
  cout<<"The number entered is not an Armstrong Number";
}
getch();
}

In part 3 of the program, the values of variable ‘a’ and ‘arm’ are checked for equivalence using if-else loop. If their values are equal, the number is verified as an Armstrong number as it satisfies the condition. Otherwise, the number is not an Armstrong number.

The use of ‘a'(the copy of variable ‘n’) for comparing with variable ‘arm’ is to be understood. In the while loop, during the extraction of digits from variable ‘n’, the original number ‘n’ is lost. Hence, to compare the original number entered by the user, a copy of the variable ‘n’ is stored in ‘a’.

In the end, getch() function is used to stay on the console screen till the input output operations are completed successfully.

Also, read:

 

Complete C++ Program:

#include<iostream.h> 
#include<conio.h> 
void main() 
{
clrscr(); 
int arm=0,dig=0,n; 
cout<<"Enter the number to be checked"; 
cin>>n; 
int a=n;
while(n!=0) 
{ 
dig=n%10; 
arm=arm+(dig*dig*dig); 
n=n/10; 
}
if(a==arm) 
{ 
cout<<"The number entered is an Armstrong Number";
} 
else 
{ 
cout<<"The number entered is not an Armstrong Number";
} 
getch(); 
}

 


Leave a Reply

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