Tuesday, July 28, 2009

Write a program using c language & function that will determine if a number is a even or odd number?

hello,





i am not much on scanf and all those user input stuff but i think the simplest algorithm is :





if (number %26amp; 1) {


printf("\n %d is an odd number.",number);


}


else {


printf("\n %d is an even number.",number);


}





this is what the other guy is talking about when he said check the first bit :)





cheers and hope this helps. :)

Write a program using c language %26amp; function that will determine if a number is a even or odd number?
I don't know how to write in C, but in Java I can do it. I give you a hint:





declare an integer x and an integer y


y=x%2


if y=0 then print out "The number is an even number"


else print out "The number is an odd number"
Reply:All you have to do is to determine if the fist bit (LSB) is set,


The Number AND 1 should do it.


No comments:

Post a Comment