C – If Else If

If else If: The if-else-if ladder statement executes only block among multiple we defined based on valid condition

Program to check the number is positive or negative or zero:

#include<stdio.h>
int main()
{
            int n;
            printf(“Enter a number : “);
            scanf(“%d”, &n);
            if(n>0)
                        printf(“Positive number\n”);
            else if(n<0)
                        printf(“Negative number\n”);
            else
                        printf(“zero”);
            return 0;
}

Program to check the Character is Alphabet or Digit or Symbol:

#include<stdio.h>
int main()
{
            char ch;
            printf(“Enter character : “);
            scanf(“%c”, &ch);
            if(ch>=’A’ && ch<=’Z’)
                        printf(“Upper case alphabet\n”);
            else if(ch>=’a’ && ch<=’z’)
                        printf(“Lower case alphabet\n”);
            else if(ch>=’0′ && ch<=’9′)
                        printf(“Digit\n”);
            else
                        printf(“Symbol \n”);
            return 0;
}

Program to find the biggest of 3 numbers:

#include<stdio.h>
int main()
{
            int a, b, c;
            printf(“Enter 3 numbers : “);
            scanf(“%d%d%d”, &a, &b, &c);
            if(a>b && a>c)
                        printf(“A is big\n”);
            else if(b>c)
                        printf(“B is big\n”);
            else
                        printf(“C is big\n”);
            return 0;
}

Program to check the input year is Leap or not:

  • If the year divisible by 400 is called Leap year
  • If the year divisible by 4 is called Leap year.
  • If the year divisible by 100 is not leap year (except 400 multiples)
#include<stdio.h>
int main()
{
            int n;
            printf(“Enter year : “);
            scanf(“%d”, &n);
            if(n%400==0)
                        printf(“Lep year \n”);
            else if(n%4==0 && n%100!=0)
                        printf(“Lep year \n”);
            else
                        printf(“Not leap year \n”);
           
            return 0;
}

C program to calculate electricity bill based on units

  • 0-100 units -> 0.8 per unit;
  • 101-200 units -> 1.0 per unit;
  • 201-300 units -> 1.2 per unit;
  • Above 300 -> 1.5 per unit;
#include<stdio.h>
int main()
{
            int units;
            float bill;
            printf(“Enter units consumed : “);
            scanf(“%d”, &units);
            if(units>0 && units<=100)
                        bill = units*0.8;
            else if(units>100 && units<=200)
                        bill = (100*0.8) + (units-100)*1.0;
            else if(units>200 && units<=300)
                        bill = (100*0.8) + (100*1.0) + (units-200)*1.2;
            else
                        bill = (100*0.8) + (100*1.0) + (100*1.2) + (units-300)*1.5;     
            printf(“Bill to pay : %f \n” , bill);
            return 0;
}

Scroll to Top