Here is the homework question,
Write a program that inputs two numbers (interval) from user and print all the Armstrong numbers in that interval. In case, if there is no Armstrong number in the interval, the program should again take two numbers from user and this goes on until it found Armstrong number in the interval. (Note: An Angstrom number is a number whose digits when cubed and summed make the number itself. For example, the number 153 = 1 + 125 + 27, and hence it is an Angstrom number.)
This is what I made.. But I am getting one problem here.. If I type a range between 100-500, it does end the loop and show me the Armstrong numbers as well.. But for any number greater than 500 or 1000, it is not calculating Armstrong numbers. For example, it doesn't count 1634 as an Armstrong number when I give him the range of 1000-2000... Can anyone tell me what's the problem in the program? Thank you
Here is a pic btw of result I am getting: https://imgur.com/a/907AutL
#include <iostream>
usingnamespace std;
int main()
{
int a, b, c, d, e, s = 0, z =0, x;
bool h = false;
while (!h)
{
cout << "Enter First Limit: ";
cin >> a;
cout << "Enter Second Limit: ";
cin >> b;
cout << "Armstrong Numbers: ";
for (int i = a; i <= b; i++, s = 0)
{
e = i;
d = i;
while (d > 0)
{
x = d % 10;
d = d / 10;
s = (x*x*x) + s;
}
if (s == e)
{
cout << e << " ";
z++;
}
}
if (z > 0)
{
h = true;
}
else
{
cout << "No Armstrong number found. Please enter numbers again\n";
}
}
cout << "\nArmstrong Numbers found in your range: " << z << endl;
system("pause");
return 0;
}
Ok, so that is the mistake.. It was actually written in the examples of Armstrong numbers on the internet (if you search them up on Google, you will find it easily as well)
Now, looking at it one more time, I found that for numbers greater than 1000, we need to take cube of their squares (basically 4th power) and that is how you can get 1634 as an Armstrong number...