Examples of Conditional statement

Conditional  statement Definition :

Conditional statement is used to carry out operations depending on a particular situation. If and only if the provided condition is true, the operation described in the one block are carried out .

If else In C

If statement :

Problem 1 :

Write a basic C program using conditional statement to check number is valid or not. If number is divisible by 2 then valid print on screen otherwise nothing will print.

Run
#include <stdio.h>

int main() {
    int n=68;

    if(n%2==0){
        printf("valid number");
    }

   return 0;
}

Output:

 valid number 

In the above problem, the value of n is 68, which is divisible by 2 and makes the condition True and if block will execute and “valid number” will print on screen.

If...else statement:

Problem Statement 2 :

Write a basic C program using if else statement to check if a number is even or odd.
Run
#include<stdio.h> 

    int main() {
    int n=68;

    if(n%2==0){ // checking the condition
        printf("given number is even");
    }
    else{
        printf("given number is odd");
    }

   return 0;
}

Output:

given number is even

In the above problem, the value of integer is 68, which is even number since it is divisible by 2.

Therefore condition of if(68%2==0) is true and “given number is even” will print on screen.

If...else ladder statement:

Problem statement 3:

Write a basic C program using if else ladder to check following condition.

  • If a number is less than 10 print “one digit” on screen.
  • If a number is greater than or equal to 10 but less than 100 print “two digit” on screen.
  • Otherwise print “Integer” on screen.
Run
#include<stdio.h> 

int main() {
    int n=6778;

    if(n<10){ // checking the condition 
        printf("one digit");
    } 
    else if(n>=10 && n<100) {
        printf("two digit");
    }
    else{
        printf("an integer");
    }
    return 0;
}

Output:

two digit

In the above problem, the value of n is 68, which is greater than or equal to 10 but less than 100 therefore “two digit ” will print on screen.

Nested If else statement:

Problem statement 4:

Write a basic C program using nested if else statement to check the following conditions.
  • If number is even and also divisible by 3 print “number is even and also divisible by 3”.
  • If number is even but not divisible by 3 print “number is even but not divisible by 3”.
  • Otherwise print “odd number”.
Run
#include <stdio.h>

int main() {
    int n=66;

    if(n%2==0){ // checking the condition

        if(n%3==0){
            printf("number is even and also divisible by 3");
        }
        else{
            printf("number is even but not divisible by 3");
        }
    }

    else{
        printf("odd number");
    }
return 0;
}

Output:

number is even and also divisible by 3

In the above problem, the value of n is 66 , which is even , first if block will execute and 66 is also divisible by 3 . therefore, “number is even and also divisible by 3” will print.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription