Checking Armstrong Numbers through while loop (homework

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  #include <iostream>
using namespace 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; 
}
Why do you think that 1634 is an Armstrong number?

There are only 4 Armstrong numbers bigger than 1, and you appear to have found them.
Last edited on
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...

Thanks for the info, :)
Topic archived. No new replies allowed.