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?
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?
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.
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.
ya correct .. start with initial point 1 and final value of 20 , increment by 5
for(int count=1; count<=20; count=count+5)
ya correct .. start with initial point 1 and final value of 20 , increment by 5
for(int count=1; count<=20; count=count+5)
start with initial point 1 and final value of 20 , increment by 5
for(int count=1; count<=20; count=count+5)