Programming

C++ Program to Print Armstrong Number from 1 – 500

Write a Program to Print the Armstrong Number from 1 – 500.

When a number’s digits are raised to the power of the number of digits, the number itself is considered an Armstrong number. Among them are 0, 1, 153, 370, 371, 407, 1634, 8208, 9474, and many more three-digit Armstrong numbers.

Algorithm for Armstrong Number

  • Start the program.
  • i=1
  • if (i<=500) then goto step 4 else goto step 9
  • s=0, j=i
  • if (j!=0) then goto step 6 ese goto step 8
  • s=s+(pow(i%10),3)), j=j/10
  • if (s=i) then display i
  • i=i+1
  • End the program

Source Code

#include<iostream>

#include<math.h>

using namespace std;

int main() {

    int i, j, s;

        for(i=1; i<=500; i++){

            s=0;

            for(j=i; j!=0; j=j/10){

                s=s+(pow((j%10), 3));              

                if(s==i)

                cout<<i<<endl;

        }

    }

}

Output

pow function

Tuts

About Author

Tutsmaster.org provides tutorials related to tech and programmings. We are also setting up a community for the users and students.