If Else Statement (C)

In C, the if and else statements are as in many other languages, control flow statements that are used to make decisions. The if keyword is followed by a pair of parentheses containing an expression. If the expression evaluates to a non-zero quantity, the follwing statement or block is executed, and the block or statement following the else keyword (if it appears) is not executed. Otherwise, control flow jumps to the block or statement following the else keyword, or simply to after the entire if statement. The following code identifies even or odd numbers using an if else statement and the modulus operator.

#include "stdio.h"
#include "stdlib.h"
int main(void){
    int i;
    char str[100];
    puts("Enter a number.");
    fgets(str,100,stdin);
    if(atoi(str) % 2){
        puts("It is odd.");
    }else{
        puts("It is even.");
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License