Skip to main content

ProwessApps

Learn · Practice · Excel

WAP to find greatest among three numbers using nested if else.

C++ Code Example — Conditional Programs

ADVERTISEMENT

WAP 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 blocks.

Algorithm / Approach

  1. Read three integers.
  2. Create an outer if (a > b) block.
  3. Inside that block, create an inner if (a > c) to confirm a is the greatest, or an else to declare c the greatest.
  4. Create an outer else block (meaning b > a).
  5. Inside that else block, create an inner if (b > c) to confirm b is the greatest.
main.cpp
#include<iostream>
using namespace std;
int main() {
 int a,b,c;
 cout<<"Enter Values for A,B & C ";
 cin>>a>>b>>c;
 if(a>b) {
  if(a>c)
   cout<<"A is greatest";
  else
   cout<<"C is greatest";
 }
 else{
  if(b>c)
   cout<<"B is greatest";
  else
   cout<<"C is greatest";
 }
return 0;
}

Expected Output

Enter Values for A,B & C 10 50 30
B is greatest

Explanation of the Program

  • Nested conditionals are if-else blocks placed entirely inside other if-else blocks.
  • They are often used to break down complex logical trees into simpler steps, though they can make the code harder to read if nested too deeply compared to using logical operators like &&.

Complexity

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