C – Nested If Block

Nested If: Defining if block inside another if block.

Syntax:

if(condition){
	if(condition){
		if-if-Stat;
	}
	else{
		if-else-stat;
	}
}
else{
	if(condition){
		else-if-stat;
	}
	else{
		else-else-stat;
	}
}

Flow Chart:

Check the number is Even or not only if the number is positive:

#include<stdio.h>
int main()
{
            int n;
            printf(“Enter number : “);
            scanf(“%d”, &n);
            if(n>0)
            {
                        if(n%2==0)
                                    printf(“Even number\n”);
                        else
                                    printf(“Not even number\n”); 
            }
            else
                        printf(“Not a positive number\n”);                  
            return 0;
}

Check biggest of 2 numbers only if the numbers are equal:

#include<stdio.h>
int main()
{
            int a, b;
            printf(“Enter 2 numbers : “);
            scanf(“%d%d”, &a, &b);
            if(a!=b)
            {
                        if(a>b)
                                    printf(“a is big\n”);
                        else
                                    printf(“b is big\n”);      
            }
            else
                        printf(“Both are equal numbers\n”);                
            return 0;
}

Print student grade only if the student passed in all subjects:

  • Minimum pass marks must be >=35
  • Display the Grade only if the student passed in all subjects.
  • If the average >= 60 then print “A-Grade”
  • If the average >= 50 then print “B-Grade”
  • If the average >= 40 then print “C-Grade”
#include<stdio.h>
int main(){
            int m1, m2, m3;
            printf(“Enter 3 marks : “);
            scanf(“%d%d%d”, &m1, &m2, &m3);
            if(m1>=35 && m2>=35 && m3>=35)
            {
                        int avg = (m1+m2+m3)/3;
                        if(avg>=60)
                                    printf(“Grade-A\n”);
                        else if(avg>=50)
                                    printf(“Grade-B\n”);
                        else
                                    printf(“Grade-C\n”);    
            }
            else
                        printf(“Student failed\n”);                   
            return 0;
}

Read month number and display days:

  • If the month is invalid (not from 1 to 12) then display “Invalid month”
  • Month-2 has 28 or 29 days
  • Months-4,6,9,11 has 30 days
  • Remaining months has 31 days
#include<stdio.h>
int main()
{
            int month;
            printf(“Enter month : “);
            scanf(“%d”, &month);
            if(month>=1 && month<=12)
            {
                        if(month==2)
                                    printf(“28 days \n”);                
                        else if(month==4 || month==6 || month==9 || month==11)
                                    printf(“30 days \n”);    
                        else
                                    printf(“31 days \n”);    
            }
            else
                        printf(“Invalid month given \n”);         
            return 0;
}
Scroll to Top