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
- Read two integers
aandb. - Use an
if (a > b)statement. - If true, print "A is Greater".
- If false, use an
elseblock 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-elsestatement 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 theifblock and executes theelseblock instead.
Complexity
Time Complexity
O(1)
Space Complexity
O(1)