C if else Statement Examples | Learn C Programming

C if else Statement Examples

Learn how to use if, else, and else if in C with 15 real examples.

Example 1: Check if number is positive

#include <stdio.h>

int main() {
    int num = 5;
if(num > 0) {
    printf("Positive number");
}
    return 0;
}

This program checks: Check if number is positive.

Check if number is positive को जांचने के लिए यह प्रोग्राम है।

Output:
Positive number

Example 2: Check if number is negative

#include <stdio.h>

int main() {
    int num = -3;
if(num < 0) {
    printf("Negative number");
}
    return 0;
}

This program checks: Check if number is negative.

Check if number is negative को जांचने के लिए यह प्रोग्राम है।

Output:
Negative number

Example 3: Check if number is even

#include <stdio.h>

int main() {
    int num = 4;
if(num % 2 == 0) {
    printf("Even number");
}
    return 0;
}

This program checks: Check if number is even.

Check if number is even को जांचने के लिए यह प्रोग्राम है।

Output:
Even number

Example 4: Check if number is odd

#include <stdio.h>

int main() {
    int num = 7;
if(num % 2 != 0) {
    printf("Odd number");
}
    return 0;
}

This program checks: Check if number is odd.

Check if number is odd को जांचने के लिए यह प्रोग्राम है।

Output:
Odd number

Example 5: Check if number is zero

#include <stdio.h>

int main() {
    int num = 0;
if(num == 0) {
    printf("Zero");
}
    return 0;
}

This program checks: Check if number is zero.

Check if number is zero को जांचने के लिए यह प्रोग्राम है।

Output:
Zero

Example 6: Check greater of two numbers

#include <stdio.h>

int main() {
    int a = 10, b = 20;
if(a > b) {
    printf("A is greater");
} else {
    printf("B is greater");
}
    return 0;
}

This program checks: Check greater of two numbers.

Check greater of two numbers को जांचने के लिए यह प्रोग्राम है।

Output:
B is greater

Example 7: Check equality of two numbers

#include <stdio.h>

int main() {
    int a = 5, b = 5;
if(a == b) {
    printf("Equal");
}
    return 0;
}

This program checks: Check equality of two numbers.

Check equality of two numbers को जांचने के लिए यह प्रोग्राम है।

Output:
Equal

Example 8: Check divisible by 5

#include <stdio.h>

int main() {
    int num = 15;
if(num % 5 == 0) {
    printf("Divisible by 5");
}
    return 0;
}

This program checks: Check divisible by 5.

Check divisible by 5 को जांचने के लिए यह प्रोग्राम है।

Output:
Divisible by 5

Example 9: Nested if - positive and even

#include <stdio.h>

int main() {
    int num = 10;
if(num > 0) {
    if(num % 2 == 0) {
        printf("Positive and Even");
    }
}
    return 0;
}

This program checks: Nested if - positive and even.

Nested if - positive and even को जांचने के लिए यह प्रोग्राम है।

Output:
Positive and Even

Example 10: Check leap year

#include <stdio.h>

int main() {
    int year = 2024;
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
    printf("Leap year");
}
    return 0;
}

This program checks: Check leap year.

Check leap year को जांचने के लिए यह प्रोग्राम है।

Output:
Leap year

Example 11: Check voting eligibility

#include <stdio.h>

int main() {
    int age = 18;
if(age >= 18) {
    printf("Eligible to vote");
} else {
    printf("Not eligible");
}
    return 0;
}

This program checks: Check voting eligibility.

Check voting eligibility को जांचने के लिए यह प्रोग्राम है।

Output:
Eligible to vote

Example 12: Find largest of three

#include <stdio.h>

int main() {
    int a = 10, b = 15, c = 12;
if(a > b && a > c) {
    printf("A is largest");
} else if(b > c) {
    printf("B is largest");
} else {
    printf("C is largest");
}
    return 0;
}

This program checks: Find largest of three.

Find largest of three को जांचने के लिए यह प्रोग्राम है।

Output:
B is largest

Example 13: Check if character is vowel

#include <stdio.h>

int main() {
    char ch = 'e';
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') {
    printf("Vowel");
} else {
    printf("Consonant");
}
    return 0;
}

This program checks: Check if character is vowel.

Check if character is vowel को जांचने के लिए यह प्रोग्राम है।

Output:
Vowel

Example 14: Grade by marks

#include <stdio.h>

int main() {
    int marks = 85;
if(marks >= 90) printf("Grade A");
else if(marks >= 75) printf("Grade B");
else printf("Grade C");
    return 0;
}

This program checks: Grade by marks.

Grade by marks को जांचने के लिए यह प्रोग्राम है।

Output:
Grade B

Example 15: Check number sign

#include <stdio.h>

int main() {
    int num = -10;
if(num > 0) printf("Positive");
else if(num < 0) printf("Negative");
else printf("Zero");
    return 0;
}

This program checks: Check number sign.

Check number sign को जांचने के लिए यह प्रोग्राम है।

Output:
Negative