Write a C++ program to print out all Armstrong numbers between 1 and 500. 


Program Code:
#include<iostream>
using namespace std;
int main()
{
int number;
int x,y,z;
for(int number = 1;number <= 500; number++)
{
x = number / 100; // divide the number with 100 and store it in x.
y = number / 10 - x * 10; // (divide number with 10) - (x with multiplication with 10)
z = number % 10; // take modulus of number with 10 and store it in z
if(x*x*x + y*y*y + z*z*z == number)
{
cout<<"The number is Armstrong number: "<<number;
cout<<endl;
}
}
return 0;
}

The program output is tested on www.jdoodle.com
Output:
The number is Armstrong number: 1
The number is Armstrong number: 153
The number is Armstrong number: 370
The number is Armstrong number: 371
The number is Armstrong number: 407


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: