C++ Program/Code For Printing all the armstrong number between 1 to n

Leave a Comment
//In this program, the maximum limit 'n' will be asked from user & the Armstrong series will be printed on screen

#include<iostream>                            //For Input/Output Stream Related Functions
#include<conio.h>                              //For Console Input/Output function like clrscr & getche
#include<math.h>                              //For mathematical functions
using namespace std;                         //Using the "standard" namespace of iostream class
void main()
{
clrscr();                                               //For Clearing the screen
long n,i,s,r,k;                                
cout<<"Enter no.:";
cin>>n;                                               //Value of 'n' entered by the user                  

for(i=1;i<=n;i++)
{
k=i;                                                    //Copying the value of 'k' in the variable 'i'
s=0;                                                    //Value of 's', is initialized with '0' at every looping turn
while(k!=0)                                       //Continue till 'k' is not equal to '0'
{
r=k%10;                                           //remainder will be stored in 'r'
k=k/10;                                             //reducing the value of 'k' by a factor of '10'
s=s+pow(r,3);                                   //'pow(r,3)' will give the cube of variable 'r'
}
if(s==i)                                             //If sum 's' is equal to 'i' then its an Armstrong Number
cout<<s<<"\t";
}
getche();                                         //To exit the program after an user's keystroke
}

0 comments: