Need help with assignment

I have an assignment that wants me to do the following

Write a program that prompts the user to enter two integers.
The program outputs how many numbers are multiples of 3 and how many numbers are multiples of 5 between the two integers (inclusive).

Below is my current code that I have yet to figure out

#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 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;
        }
          }
   
   cout << endl << "The multiples of 3 are " << multiple3 << "and the multiples of 5 are " << endl;
   
   
   
    return 0;
}
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.
Last edited on
The last thing I need is to be able to still output the multiples if n1 is less than n2
Thank you greatly though!!
The last thing I need is to be able to still output the multiples if n1 is less than n2

I don't understand how that's different from what I wrote. Explain precisely what the problem is again?
Last edited on
Topic archived. No new replies allowed.