C++ program/code to check wether a number is Armstrong or Not

Leave a Comment
 //This program lets the user to enter a number, and check whether that is Armstrong or not

#include<iostream.h>                           //For Input/Output Stream Function
#include<conio.h>                                 //For Console Input/Output function like clrscr & getche
#include<math.h>                                 //For mathematical functions
void main()
{
clrscr();                                                  //For clearing the screen
int n,i,s=0,r;                                           //'s' is initialized by '0'
cout<<"Enter no.:";
cin>>n;                                                  //user inputs the number
i=n;                                                        //Storing a temporary copy
while(n!=0)
{
r=n%10;                                               //Calculation of the remainder when divided by 10
n=n/10;                                                 //'n' is decreased by a factor of 10
s=s+pow(r,3);                                       //cube of remainder is added to the sum
}
if(s==i)                                                 //If sum is equal to the original copy of the input data
cout<<"\nTne No. is Armstrong.";
else
cout<<"\nThe No. is not Armstrong.";
getche();                                              //To exit the program after a key stroke
}

0 comments: