Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
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.
Related codes are those codes where similar programming concepts are in use. They are different from alternate codes in the sense that they may not have same purpose and results.
:
Run Output
Enter three integers: 1 2 3
3 is largest.
-OR-
Enter three integers: 43 12 32
43 is largest.
-OR-
Enter three integers: 103 104 101
104 is largest.
-OR-
Enter three integers: 103 103 103
Two large numbers or all numbers are same
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.