To find the max min for n no

why in max no we are taking very small no
nd in min very large no please give me another example for It I won’t be able to understand it

hello @tarshid7

we want to initilaise the variables with value that are easily replacable.

in case of max variable our objective is to find max so we intilaise the variable with very small number so that it get replaced by anybigger number easily while taking max.

similar logic for computing min

please explain me the whole program why for loop is using and no is greater smaller why its used

we have n numberrs so to read n number we need to use loop.
and whanever we read a new number we check with current maxium and minimum variable and update if needed

why we are inputing cin no. after loop please explain me

we are not taking any input after the loop. can u please shar3e the code that u are talking about

#include #include using namespace std; int main() { int n; cin>>n; int min=INT_MAX; int max=INT_MIN; int no; for(int i=0;i<n;i++){ cin>>no; if(no<min){ min=no; } if(no>max){ max=no; } } cout<<“max”<<max<<endl; cout<<“min”<<min<<endl; return 0; }

before for loop there inputing no and after for loop we are doing cin>>no why so that’s the q

int no; for(int i=0;i<n;i++){ cin>>no; if(no<min){ min=no;

before for loop we are reading n , that is number of elements in array.

here we are iterating n times and in each iteration we are reading new number so for that we are using cin>>no;

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

why for loop is using here

why cannot we can use while loop here and give me the program for while loop and explain me

Hi Tarshid. Its all up to u. U can use for loop or while loop. Both does the same thing. its only that both have a different syntax.

please show me the use of while loop in this program

why I is used here in for loop

#include
using namespace std;
int main() {
int n;
cin>>n;
int min=INT_MAX;
int max=INT_MIN;
int no;
int i = 0;
while(i < n){
cin>>no;
if(no<min){
min=no;
}
if(no>max){
max=no;
}
i++;
}
cout<<“max”<<max<<endl;
cout<<“min”<<min<<endl;
return 0;
}

Hope u understood the syntax of while loop and for loop now

what is the use of I why we are using it

we could have easily don n>0 and in last n=n-1; like we do in many program why we are not using it