C++ Program to Display Armstrong Number Between Two Intervals.

C++ Program to Display Armstrong Number Between Two Intervals.


#include <iostream.h>
int main()
{
  int num1, num2, i, num, digit, sum;

  cout << "Enter first number: ";
  cin >> num1;

  cout << "Enter second number: ";
  cin >> num2;

  cout << "Armstrong numbers between " << num1 << " and " << num2 << " are: " << endl;
  for(i = num1; i <= num2; i++)
  {
        sum = 0;
        num = i;

        for(; num > 0; num /= 10)
        {
            digit = num % 10;
            sum = sum + digit * digit * digit;
        }

        if(sum == i)
        {
            cout << i << endl;
        }
  }

  return 0;
}


Output

Enter first number: 100
Enter second number: 400
Armstrong numbers between 100 and 400 are:
153
370
371

Post a Comment

Previous Post Next Post