Need help with c++, i am stuck

Hi guys I really need help because im beginner and i tried to search for the solution and nothing showed me right thing, so here is the code

Right now if u input 1 and 10 it prints numbers 3 6 and 9 what i wanna do is that my program would print 3 because it printed 3 numbers.

Is that possible? Please help


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
#include <iostream>
using namespace std;
int main()
{

 int n, m;

 cout << "First number: ";
 cin >> n;
 cout << "Second number: ";
 cin >> m;

 for(n; n <= m; ++n)
    {

if (n%3 == 0)
cout << n << ' ';


    }



return 0;
}
Add a counter variable, e.g. int count = 0;

Around line 18 (inside the if statement), add count++; to increment count.

After the for loop, print out the final value of count.
cout << m/3 << endl; //10 / 3 is 3 in integer math. you are going to print 3 numbers, every third one, so you can just directly compute this output. If it were more complicated, you can also add a counter and inside the if of the loop, add 1 to it, at the end, it will tell you how many it counted.

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
#include <iostream>
using namespace std;
int main()
{

 int n, m, count = 0;

 cout << "First number: ";
 cin >> n;
 cout << "Second number: ";
 cin >> m;

 for(n; n <= m; ++n)
    {

if (n%3 == 0) {
cout << n << ' ';
count++; }

    }

cout << endl << count;

return 0;
}
Thank you so much guys, you really helped me!
Topic archived. No new replies allowed.