0

Finding second smallest number out of 3 unknown numbers in c++

my code- (please explain where am i wrong) #include<iostream> using namespace std; int main() { int a,b,c; cin>>a>>b>>c; if(a<=b && b<=c && a<=c) cout<<b; else if (b<=c && c<=a) cout<<c; else cout<<a; return 0; }

10th Aug 2020, 9:02 AM
Navneet Chauhan
Navneet Chauhan - avatar
8 Respuestas
+ 7
float n1, n2, n3; cout << "Enter three numbers: "; cin >> n1 >> n2 >> n3; if(n1 >= n2 && n1 >= n3) { cout << "Largest number: " << n1; } if(n2 >= n1 && n2 >= n3) { cout << "Largest number: " << n2; } if(n3 >= n1 && n3 >= n2) { cout << "Largest number: " << n3; }
11th Aug 2020, 6:37 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 6
Bartas Dausynas U can use equality operator for this situation yes it should be print two number but u can use exception handling in this case
11th Aug 2020, 6:56 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
If you add a small if before the first if. Like if(c<a) { int tmp = c; c = a; a = tmp; } The problem you have that if c is smaller than a you first if dosnt worl correctly
10th Aug 2020, 9:11 AM
Dragonxiv
Dragonxiv - avatar
0
Navneet Chauhan think, if (c<=b && b<=a) what should be the answer?🤔🤔
10th Aug 2020, 9:50 AM
The future is now thanks to science
The future is now thanks to science - avatar
0
For kth smallest using std::priority_queue https://code.sololearn.com/cXtA80Pq8xXK/#cpp For smallest using std::min_element https://code.sololearn.com/cVljOO5Y38lC/#
10th Aug 2020, 10:34 AM
Ockert van Schalkwyk
Ockert van Schalkwyk - avatar