+ 1
How can write program to calculate greatest and smallest between 5 number
7 Antworten
+ 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;
}
+ 2
We need at least one initial value for further comparison, that's why.
+ 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.
+ 1
why do cin before use for
+ 1
but when I do this program the greatest number was 0 and the smallest number was 0
+ 1
can you explain the program
+ 1
thanks for you 😉