Skip to main content

ProwessApps

Learn · Practice · Excel

C Program to find greatest among three numbers using Nested If-else

C Code Example — Conditional Programs

ADVERTISEMENT

C Program to find greatest among three numbers using Nested If-else

Objective

Write a C program to find the greatest among three numbers using Nested if-else statements.

Algorithm / Approach

  1. Read three numbers a, b, and c.
  2. Check the first condition: if(a > b).
  3. If true, dive into an inner (nested) block and check if(a > c) to confirm a is the absolute greatest.
  4. If the first condition was false (meaning b is larger than a), drop to the outer else block.
  5. Inside that else block, check if(b > c) to see if b beats c.
main.c
#include<stdio.h>
int main( ){
int a, b,c;
printf("Enter Values for A,B & C : ");
scanf("%d%d%d",&a,&b,&c);
if(a>b) {
if(a>c)
printf("A is Greatest\n") ;
else
printf("C is Greatest\n") ;
}
else {
if(b>c)
printf("B is Greatest\n") ;
else
printf("C is Greatest\n") ;
}
return 0;
}

Expected Output

Enter Values for A,B & C :
52 43 66
C is Greatest

Explanation of the Program

  • A Nested If is simply an if statement placed inside the body of another if (or else) statement.
  • This approach avoids using the logical && operator. It branches the logic like a tree: first comparing two numbers, discarding the loser, and then comparing the winner against the third number.

Complexity

Time Complexity O(1)
Space Complexity O(1)
ADVERTISEMENT