Skip to main content

ProwessApps

Learn · Practice · Excel

WAP to find the greatest among two numbers

C++ Code Example — Conditional Programs

ADVERTISEMENT

WAP to find the greatest among two numbers

Objective

Write a C++ program to find the greatest among two numbers using if-else.

Algorithm / Approach

  1. Read two integers a and b.
  2. Use an if (a > b) statement.
  3. If true, print "A is Greater".
  4. If false, use an else block to print "B is Greater".
main.cpp
#include<iostream>
using namespace std;
int main() {
 int a,b;
 cout<<"Enter value for A: ";
 cin>>a;
 cout<<"Enter value for B: ";
 cin>>b;
 if(a>b) {
 cout<<"A is Greater";
 }
 else {
 cout<<"B is Greater";
 }
return 0;
}

Expected Output

Enter value for A: 12
Enter value for B: 14
B is Greater

Explanation of the Program

  • The if-else statement is the most basic form of conditional branching in C++.
  • It evaluates a boolean expression (a condition that is either true or false). If the condition is true, it executes the code block immediately following the if. If it is false, it completely skips the if block and executes the else block instead.

Complexity

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