Table of Contents
Introduction
This program of C++ programming will find the largest number among the three numbers entered by the user. There are different methods to perform this code.
Here we will use the if statement only.
Moreover, to understand this program, you should know about
1. Operators
2. if statement
Program
#include<iostream> using namespace std; int main() { float x,y,z; cout<<"Enter three values\n"; cin>>x>>y>>z; // if statements to check the largest number if(x>=y && x>=z) cout<<x<<" is largest among all."<<endl; if(y>=x && y>=z) cout<<y<<" is largest among all."<<endl; if(z>=y && z>=x) cout<<z<<" is largest among all."<<endl; return 0; }
Output
Explanation
The program asks the user to enter three values, and that values get stored in ‘x’, ‘y’, and ‘z’.
The values entered by the user are x=25.5, y=99, and z=99.8.
if(x>=y && x>=z)
The first if statement gives a false result since (25.5>=99 && 25.5>=99.8) is not true.
if(y>=x && y>=z)
The second if statement also gives a false result since (99>=25.5 && 99>=99.8) is not true.
if(z>=y && z>=x)
The third if statement gives a true result since (99.8>=25.5 && 99.8>=99) is true.
Thus, 99.8 is the greatest among all that gets printed.
0 Comments