I am trying to learn objective C with my brother and I have stumbled onto a problem involving one trying to collect a user's input and checking if it is odd or even.

I made this, and this works to test if a number is even or odd:

#include <stdio.h>

int main(){
    int num = 22;

    if (num % 2 == 0)
{
    printf("Yep. ");
}
else
{
    printf("Nope. ");
}

}

But how do i get it so that I can take what a user input is and run it to see if it is even or odd?

This is what I have so far:

#include <stdio.h>

int main(){
    int num = 0;

    printf("Enter your string: ");
    scanf("%s", num);
    printf("Your string is %s\n", num);

    if (num % 2 == 0)
{
    printf("Yep. ");
}
else
{
    printf("Nope. ");
}

}

You need to scan into an integer value, not a string. Also, tell the user to enter a number instead of a string. IE:

int main()
{
    int num = 0;

    printf("Enter your number: ");
    fflush(stdout);
    scanf("%d", num);
    printf("Your number is %d\n", num);

    if (num % 2 == 0)
    {
        printf("Even.\n");
    }
    else
    {
        printf("Odd.\n");
    }
    return 0;
}
Comments
thanks
This article has been dead for over six months. Start a new discussion instead.