Write individual for statements for the following cases a U
Write individual for( ) statements for the following cases:
a. Use a counter named count that has an initial value of 1, a final value of 20, and an increment of 5.
My answer is:
for(int count=1; count<=20; count=count+5)
cout<<count<<endl;
However, with this answer, my outcome is 1, 6, 11, 16. The question asks for me to have a final vlue of 20, how do I achieve this? What am I doing wrong?
Solution
Nothing worng in it ... you just need to use for loop which has counter variable count. We run the loop from an initial value of 1 and run until a final value of 20 with an increment of 5 for each execution of the loop.
initial value of counter is 1
and run the loop untill the final value 20.
BTW there did not mentioned about outcome of for loop so no need to worry about the output..
Answer is :
for(int count=1; count<=20; count=count+5)
{
// loop body
}
