+ 1

How can write program to calculate greatest and smallest between 5 number

29th Mar 2017, 1:23 PM
Bts...
Bts... - avatar
7 Réponses
+ 5
#include <iostream > using namespace std; int main (){ int n, b, s; cin>>n; //first input b=s=n; //first input is biggest & smallest till now // remaining inputs for ( int i=2;i <=5;i++){ cin>>n; if (n>b) b=n; if (s>n) s=n; } cout <<"Biggest value : "<<b<<endl; cout <<"Smallest value : "<<s<<endl; }
29th Mar 2017, 3:05 PM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
+ 2
We need at least one initial value for further comparison, that's why.
29th Mar 2017, 4:29 PM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
+ 2
If you set b =0 and s=0. It may be okay for finding out the biggest value, but won't get any positive smallest value.
29th Mar 2017, 4:51 PM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
+ 1
why do cin before use for
29th Mar 2017, 4:24 PM
Bts...
Bts... - avatar
+ 1
but when I do this program the greatest number was 0 and the smallest number was 0
29th Mar 2017, 4:30 PM
Bts...
Bts... - avatar
+ 1
can you explain the program
29th Mar 2017, 4:45 PM
Bts...
Bts... - avatar
+ 1
thanks for you 😉
30th Mar 2017, 6:20 PM
Bts...
Bts... - avatar