Skip to main content

ProwessApps

Learn · Practice · Excel

WAP to find greatest among three numbers

C++ Code Example — Conditional Programs

ADVERTISEMENT

WAP to find greatest among three numbers

Objective

Write a C++ program to find the greatest among three numbers using the logical AND operator.

Algorithm / Approach

  1. Read three integers: a, b, and c.
  2. Check if a is the greatest using if (a > b && a > c).
  3. Check if b is the greatest using else if (b > c) (since a is already eliminated).
  4. If both are false, c must be the greatest, so use a final else.
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 && a>c) {
  cout<<"A 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 29 20
B is greatest

Explanation of the Program

  • The Logical AND operator (&&) allows you to combine multiple conditions together.
  • For an && expression to be considered true, BOTH conditions on its left and right sides must be individually true. By chaining else if statements, C++ evaluates them sequentially until it finds the first true condition, and then skips the rest.

Complexity

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