You can find quality tech news,tricks,study materials etc contact me:facebook.com/ashwin.sunilkumar
Support Me...........
Please support me by giving donation
contact: aswinavofficial@gmail.com
Total Pageviews
Monday, September 1, 2014
C++ program to find Armstrong Numbers
// Program to find/generate Armstrong numbers
#include
using namespace std;
bool isArmstrong(int x);
int pow(int n, int power);
int digits(long int n);
int main () {
int min=0, max=1000;
cout << "Program to find Armstrong numbers..." << endl;
cout << "Enter a range for the Armstrong number..." << endl;
cout << endl << "Enter lowest value to test: ";
cin >> min;
cout << endl << "Enter highest value to test: ";
cin >> max;
cout << endl;
for (int i=min; i<=max; i++)
if (isArmstrong(i))
cout << i << endl;
}
bool isArmstrong (int x) {
int n = x;
int d = digits(x);
int y=0, z=x;
while (z>0) {
x = z%10;
y = y + pow(x, d);
z /= 10;
}
if (y==n)
return true;
else return false;
}
int pow (int n, int power) {
if (power == 1)
return n;
else
return n * pow(n, power-1);
}
int digits(long int n) {
if (n<10)
return 1;
else
return 1+digits(n/10);
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment