-3

Use a counter named count that has an initial value of 1, a final value of 20, and an increment of 5.

for(int count=1; count<20; count=count+5)
cout<<count<<endl;

Is the correct approch?

RJV
  • 287
  • 1
  • 7
  • 20

5 Answers5

1

Yes, or the shorthand version :

for(int count=1; count<=20; count+=5)

Your values will be 1 , 6 , 11 , 16 though. It won't get to 20 so the question is a bit off.

Side note, I see you using cout, this means you either have using namespace std; or using std::cout;, the latter is fine, the former not so much.

Community
  • 1
  • 1
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
1

I would use Boost.Range. The interface is quite straighforward and you are explicitly showing your intention which is importan with custom loops.

#include <iostream>
#include <boost/range/irange.hpp>

int main() {
    for (auto i: boost::irange(1, 20, 5)) {
        std::cout << i << std::endl;
    }
    return 0;
}

Stream output operator is already overload so you'll get the value from the type you want.

You can try it live.

Dam
  • 76
  • 1
  • 4
0

ya correct .. start with initial point 1 and final value of 20 , increment by 5

for(int count=1; count<=20; count=count+5)

0

ya correct .. start with initial point 1 and final value of 20 , increment by 5

for(int count=1; count<=20; count=count+5)
RJV
  • 287
  • 1
  • 7
  • 20
0

start with initial point 1 and final value of 20 , increment by 5

for(int count=1; count<=20; count=count+5)