[Legacy Content] This page contains very old information, which might not be valid anymore, they are only kept here for legacy purposes.
if you have any inquiries, feel free to contact me! Click here

Determine Even/Odd and Positive/Negative Number

Created: Tuesday, 24 September 2013 Written by Ehab Eldeeb

Write a program that reads a number and determines if its:
- positive and odd
- positive and even
- negative and even
- negative and odd
- zero

There are many ways to solve this issue, but this one is the first that came into my mind :-)

#include <stdio.h>
#include <conio.h>

int main(){
    int x;
    printf("Enter number to determine whatever..: ");
    scanf("%d", &x); // read the number
    if (x > 0){ // check if it's positive?
        printf("%d is positive number \n", x);
        if( x%2 == 0) // another check, is it even (divisible by 2) ?
            printf("%d is even number \n", x);
        else // not divisible by 2
            printf("%d is odd number \n", x);
    } else if (x < 0) { // check if it's negative?
        printf("%d is negative number \n", x);
        if( x%2 == 0) // check if it's even (divisible by 2)
            printf("%d is even number \n", x);
        else // not divisible by 2
            printf("%d is odd number \n", x);
    }
    else { // any other case .. which is not positive or negative .. obviously zero :)
        printf("Zero");
    }
    getch();
    return 0;
}