Monday, May 24, 2010

Im trying to write some C++ code that starts at 2 and counts up by 2 to 100. Can anyone help?(Exam in details)

int main()


{


counter=2;


do {


cout %26lt;%26lt; counter;counter%26gt;=2%26lt;%26lt; endl;


} while (counter %26lt;100);

Im trying to write some C++ code that starts at 2 and counts up by 2 to 100. Can anyone help?(Exam in details)
int main()


{


int counter=2;


do


{


cout %26lt;%26lt; counter%26lt;%26lt;endl; //print out the current value of counter


counter+=2; //increase counter by 1 each time


}


while (counter %26lt;=100); // You want it to get 100 so u need the equal











OR THIS ONE


int main()


{


for(int counter=2;counter%26lt;=100;counter+=2)


{


//print out the current value of counter


cout %26lt;%26lt; counter%26lt;%26lt;endl;


}


}





OR THIS ONE





int main()


{


int counter =2;


while(counter%26lt;=100)


{


cout%26lt;%26lt;counter%26lt;%26lt;endl;


counter =counter+2;


}


}











Im a Programmer from Microsoft
Reply:I've forgotten how to code in C but you are not adding to your counter





It sounds like your a beginner in C you should really get a reference book.
Reply:int main()


{


int counter=2;


do{


cout%26lt;%26lt; counter %26lt;%26lt; endl;


counter+=2;


}


while (counter%26lt;100);





}





Another one...


int main()


{


for (int index=0;index%26lt;100;index+=2)


{


cout%26lt;%26lt;index%26lt;%26lt;endl;


}


}
Reply:you can use eifther for loop or while loop.


example





int i=2;


for(;i%26lt;102;i+=2)


{





... your code


}





or





int i=2;


do


{


...your code


i +=2;


}while(i%26lt;102);
Reply:well, try this inside do {} instead of what you have





cout %26lt;%26lt; counter %26lt;%26lt;endl;


counter+=2;





and Im not sure if you missed this or not, there should a closing bracket to close the opened bracket after int main ()





This } should go after while statement.


No comments:

Post a Comment