for (int num1 = n1; n1 <= n2; num1++){
You're confusing yourself with your choice of variable names.
Your looping condition is n1 <= n2, but you never change n1 or n2 in your loop.
Your indentation is also severely erratic. Please use proper code formatting by adding [
code] and [
/code] tags around your code.
Here is what your code looks like with proper formatting...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
for (int num1 = n1; n1 <= n2; num1++) {
if (num1 %3 ==0) {
multiple3 += 1;
}
if (num1 %5 ==0) {
multiple5 += 1;
}
}
for (int num2 = n2; n2 >= n1; num2++) {
if (num2 %3 ==0) {
multiple3 += 1;
}
if (num2 %5 ==0) {
multiple5 += 1;
}
}
|
How will your second for loop ever end, if n2 >= n1? Again, similar problem as before, with the added problem of num2 will always be greater than n1, so your loop will never end (well, until you reach the max int, after which your program has undefined behavior).
Why even have two loops at all when one loop will suffice?
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
|
#include <iostream>
using namespace std;
int main() {
int multiple3 = 0;
int multiple5 = 0;
int n1, n2;
cout << "Give the range of numbers.";
cout << endl << "The smallest number is: " << endl;
cin >> n1;
cout << "The larger number is: " << endl;
cin >> n2;
for (int n = n1; n <= n2; n++) {
if (n % 3 == 0) {
multiple3 += 1;
}
if (n % 5 == 0) {
multiple5 += 1;
}
}
cout << endl << "There are " << multiple3 << " multiples of 3, and " << multiple5 << " multiples of 5 in the range specified." << endl;
return 0;
}
|
On an unrelated note, I'm very surprised the forum didn't remove your whitespace. I hope this is a sign of good things to come as far as site maintenance.