Using Modulus.Help!!

Here is the question.

By using a single compound if statedment write a program to determine whether the user enters an odd positive number.

Im not to advanced in c++ but i do know i have to use the modulus operator.Im just not sure how to write it to figure if it is odd positive number.Also how would i test it by using relational operators?

thanks

Comments

  • [b][red]This message was edited by stober at 2003-2-28 19:1:30[/red][/b][hr]
    : Here is the question.
    :
    : By using a single compound if statedment write a program to determine whether the user enters an odd positive number.
    :
    : Im not to advanced in c++ but i do know i have to use the modulus operator.Im just not sure how to write it to figure if it is odd positive number.Also how would i test it by using relational operators?
    :
    : thanks
    :

    X % 2 returns 0 if X is an even number. So just put an if statement around that and you have the answer to half your question. The other half is testing if X > 0.


  • : Here is the question.
    :
    : By using a single compound if statedment write a program to determine whether the user enters an odd positive number.
    :
    : Im not to advanced in c++ but i do know i have to use the modulus operator.Im just not sure how to write it to figure if it is odd positive number.Also how would i test it by using relational operators?
    :
    : thanks
    :

    You can write if((x%2)==0) cout<<"even"; //assumed x as a value
    else cout<<"odd":
    as x%2 returns remainder of number of x divided by 2
    & if rem=0 it means it is even as for even/2 rem=0
    if rem !=0 it means it is odd as for odd/2 rem !=0
    you can also write
    if((x=getch())%2)==0) cout<<"even";//include conio.h
    else cout<<"odd"; // for getch()
    above is if u want i/p in same expression.


  • Thank you for the help. And thank you for the examples.

  • : Thank you for the help. And thank you for the examples.
    :
    : You r welcome

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories