Thursday, September 6, 2012

Program to check ARMSTRONG number in c++

// program to check the armstrong number

#include <iostream>
using namespace std;

int main()
{
   int num, sum = 0, temp, remainder;

   cout << "Enter a number to check: " << endl;     
   cin >> num;

   temp = num;

   while( temp != 0 )
   {
      remainder = temp%10;
      sum = sum + remainder*remainder*remainder;
      temp = temp/10;

   }

   if ( num == sum )
      cout << "Your Number is an armstrong number." << endl;
   else
      cout << "Your Number is not an armstrong number." << endl;      
}

___________________________________________________________________________________

//program to generate armstrong number

#include <iostream>
using namespace std;

int main()
{
   int r;
   long long number = 0, c, sum = 0, temp;

   cout << "Enter the maximum number upto which you want to find armstrong numbers ";
   cin >> number;

   cout << "Following armstrong numbers are found from 1 to " << number << endl;

   for( c = 1 ; c <= number ; c++ )
   {
      temp = c;
      while( temp != 0 )
      {
         r = temp%10;
         sum = sum + r*r*r;
         temp = temp/10;
      }


      if ( c == sum )
         cout << c << endl;
      sum = 0;
   }
}

No comments:

Post a Comment