Create a class Armstrong and include startnumber and endnumber as data member and aslo include member function findArmstrongNumbers() to find all the Armstrong numbers between startnumber and endnumber. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number
Run time input 1and 500
#include <iostream>
using namespace std;
class Armstrong
{
public:
Armstrong(int start, int end);
void Find();
private:
int startNumber;
int endNumber;
};
Armstrong::Armstrong(int start, int end) : startNumber(start), endNumber(end)
{}
void Armstrong::Find()
{
for (int i = startNumber; i <= endNumber; i++)
{
int temp = i;
int sum = 0;
while (true)
{
if (temp < 10)
{
sum += (temp % 10) * (temp % 10) * (temp % 10);
break;
}
sum += (temp % 10) * (temp % 10) * (temp % 10);
temp /= 10;
}
if (sum == i)
{
cout << i << " is Armstrong number!" << endl;
}
}
}
int main()
{
int start, end;
cout << "Enter start number: ";
cin >> start;
cout << "Enter end number: ";
cin >> end;
Armstrong obj(start, end);
obj.Find();
cout << endl;
system("pause");
return 0;
}
Comments
Leave a comment