Helppp please

#include<iostream>
using namespace std;
void main()
{ int i;
for(i=4;i<=24;i++)
{
if()
{ cout<<i<<endl;
}
}

system("pause");
}

how can the out put of this fuction be: 4 9 14 19 24
hint they are all plus 5
first: please use the code tags
second: in line 5 you substitute it with:
for(i=4;i<=24;i+=5)//or i=i+5
closed account (iAk3T05o)
Do:
1
2
3
4
5
6
7
for (int i = 4; i <= 24; i+= 5)
{
std::cout << i << std::endl;
}

return 0;
}


closed account (iAk3T05o)
i = i+5 didn't work.
Nathan2222 wrote:
i = i+5 didn't work.
Funny, because "i += 5" is defined to mean "i = i + 5" since i is a primitive type.
Last edited on
closed account (j3Rz8vqX)
Your original code has a bit of issues.

-main must return an integer, so use int main.

-system was not declared, you must include the cstdlib if you plan to use it.

-if_statement isn't testing anything.

-As other folks have said, i+=5 should work; spoiler implementation below.

Use the code tags next time please.

Spoiler Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
using namespace std;
int main()
{
    int i;
    for(i=4;i<=24;i+=5)
    {
        cout<<i<<endl;
    }
    cout<<"\nPress Enter to exit"<<endl;
    cin.get();
    return 0;
}

@Dput: while I understand you were making minimal corrections to the code to make it work, you should at least fix the other poor practices while you are at it ;)
1
2
3
4
5
6
7
8
9
#include<iostream>

int main(){
   for(int K=4; K<=24; K+=5)
      std::cout << K << ' ';

   std::cout << '\n';
   return 0;
}

- limit the scope of the variables. Given that the counter is only used in the loop, it should only live in the loop.

- std::endl would flush the stream, which is not necessary. Could be replaced with '\n', but that would be wrong too.
The assignment ask the output to be: 4 9 14 19 24, separated with spaces.

The last newline is so the command prompt starts at the beginning.

- The program should terminate when it ends, no wait for a key press.

- Don't pay attention to the style differences.
You can get your desired output by this code.

1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
     for(int i = 4; i <= 24; i +=5)
         std::cout<<i<<" ";
     std::cout<<"\n";
     return 0;
}


But i would like to suggest your few thing
1. Don't use standard namespace. For more detail visit http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
2. Change the return type of main from void to int and return 0 at the end of main, By default compilers return 0. For more detail visit http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c
Last edited on
Topic archived. No new replies allowed.