Sunday, October 11, 2009

Determining odd and even numbers in C++ using loops(for and if)??

I really need help in this one because I'm not familiar on using modulus(%) operation in C++..im just a beginner.. Well anyway this is the problem.





"Using for loop. Enter any 10 numbers and determine the odd and even numbers among the 10 numbers. Count the # of odd values and even numbers.."





I really can't figure this out but my instructor said that it's just a simple solution..Please help..

Determining odd and even numbers in C++ using loops(for and if)??
Use the AND operation.





for(int x=0;x%26lt;=10;x++)


{


if(x%26amp;1)


{


printf("Odd\n");


}


else


{


printf("Even\n");


}


}
Reply:You just need the if statement check if there's a remainder if the number is divided by 2. If so it's an odd number.
Reply:I don't know how to code in C, but this may help anyhow.





Any number MOD 2 that equals 0 is even, otherwise it's odd.





The modulus operation gives you the REMAINDER portion of any division problem. So 5 MOD 4 = 1 (5/4=1 remainder 1)





fix this code to work in C++:





for(int i=1;i%26lt;=10;i++){if((i % 2)==0){evens++}else{odds++}}



performing arts

No comments:

Post a Comment