### 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.

 ``1234567891011121314151617181920212223242526`` ``````#include 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. ``12345678910111213141516171819202122232425`` ``````#include 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.