Largest of three numbers using if else ladder – CSKC
Sign In

Search

Solved Problem #CPP#2946    siteicon   siteicon  

Problem Statement - Largest of three numbers using if else ladder

Write a c++ program to find the largest of three numbers using the if-else selection ladder. Please do report with an appropriate message if two large numbers or all numbers are same.

Solution

TC++ #2946

 

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

  Related ( ? ) :

int a,b,c; cout<<“Enter three integers  :”; cin>>a>>b>>c;
Three integer numbers are collected

if(a>b && a>c) cout<<a<<” is Largest”<<endl;
Test if first is greater than other two.

else if(b>a && b>c) cout<<b<<” is Largest”<<endl;
Test if second is greater than other two.

else if(c>a && c>b) cout<<c<<” is Largest”<<endl;
Test if third is greater than other two

else cout<<“Two large numbers or all numbers are same”<<endl;
If above three conditions fail then either two large numbers or all the numbers would be same

Notes

  • This logic works by comparing two conditions at a time. One can also write a logic by comparing one condition at a time and then subsequently comparing the other one. For e.g.
    int G;
    if(a>b) {  if(a>c) G=a; else G=c;}
    else if(b>c) G=b; else G=c;
    G would be largest.

Common Errors

  • Some students accidentally put or (||) operator instead of and(&&) operator while comparison.


Suggested Filename(s): largest3.cpp



Share

Created: 23-Jan-2018 03:15 | Updated: 18-Dec-2018 17:46|