# code help!

• 03-24-2006
invictus1er
code help!

Code:

```main()         {                 for (int i = 0; i < 8; i++)                         if (i%2 == 0) cout << i +1 << “\t”;                           else if (i%3) == 0) cout << i * i  << “\t”;                           else if (i%5 == 0) cout << 2*i – 1 << “\t”;                           else cout << i << “\t”;         }```
• 03-24-2006
bumfluff
Surely you have a problem if the number divides by 3 and 2 like 6

if int i/2 gives no remainder then it will output i+1
else if i/3 gives no remainder then the output will be i^2
else if i/5 gives no remainder the output will be 2*i - 1
otherwise the output will be i
• 03-24-2006
major_small
well, if you #include <iostream>, use namespace std, and put an int before main, you get... nothing.
Code:

```jshao@MCP ~/Programming/C++/test \$ g++ test.cpp -Wall -W -ansi -pedantic -o test.exe && ./test.exe test.cpp: In function `int main()': test.cpp:9: error: syntax error before `==' token test.cpp:10: error: syntax error before `else'```
that for loop is constructed wrong, and your parenthesis are messed up.

after fixing the loop and parenthesis and making it more readable, and adding comments, it becomes more clear:
Code:

```#include <iostream> using namespace std; int main() {         for (int i=0;i<8;i++)         {                 if (i%2==0)                //if the remainder of i/2 is zero                 {                         cout<<i+1<<'\t';        //output i+1 and a tab                 }                 else if (i%3 == 0)        //if the remainder of i/3 is zero                 {                         cout<<i*i<<'\t';        //output i*i and a tab                 }                 else if (i%5 == 0)        //if the remainder of i/5  is zero                 {                         cout<<2*i-1<<'\t';        //output (2*i)-1 and a tab                 }                 else                        //otherwise                 {                         cout<<i<<'\t';                //just output i                 }         } }```
and now when you compile and run that, you get:
Code:

```jshao@MCP ~/Programming/C++/test \$ g++ test.cpp -Wall -W -ansi -pedantic -o test.exe && ./test.exe 1      1      3      9      5      9      7      7```
basically, if i is an even number (in this case, 2,4,6,8), it'll add one to i, and if it's a multiple of three (in this case, only when i is 3), it'll square the nubmer, and when it's a multiple of 5 (in this case only when it's 5), it'll multiply i by 2 and subtract 1. That leaves you with 1 and 7, which just gets printed for what it is.