# " minimum" part of the program does not work c program loop

Hello ,

so i wanna make a program which gives the maximum and minimum number of 10 number entered by the user

the program gives me the maximum number but not the minimum,
the program does not give me the minimum number unless among the numbers that the user has entered a negative number or equal to 0 (because min = 0)
what should I do so that the program gives me the minimum number even though it is greater than 0?

sorry for my english and hope my problem is clear

#include<stdio.h>

int main()

{ int a=0,max=0,min=0,compteur;

printf ("entrez 10 nombres : \n");

for (compteur=0;compteur<=9;compteur++)

{

scanf("%d",&a);

if(a > max){

max = a;

}

if(a < min){

min = a;

}

}

printf("le nombre maximal est : %d \n",max);

printf("le nombre minimal est : %d\n",min);

}

answered Dec 31, 2020 by (10,500 points)
selected Jan 2, 2021 by Wryyy!!

The code is almost correct ,but just has the prob with "min" variables initial value.

To identify the "max" value you initialised it to least value possible.The same logic can be worked backwards to conclude that - to identify the "min" value you should initialise it to the greatest value possible.

Ie code should be like --->

```int min = 999999;//Greatest value possible hence
//all other values in the range
//are lesser than min
int max =-999999;//Least value possible hence
//all other values in the range
//are greater than max

```
commented Jan 2, 2021 by (100 points)
ur code is perfect fine but initialized min value is wrong bcoz u need to initialize min the any num except negative or zero(ex-min=10)
answered Jan 1, 2021 by (210 points)
Your code is almost correct and so is the answer given by xDELLx

I just have a different version of code which makes the first value entered as max and min, so that it can be used for comparison with other 9 values. Below is my code:

#include <stdio.h>

int main()
{
int max,min,i,num;

printf("Program to find min and max from ten given numbers");

printf("\nEnter the first number");
scanf("%d",&i);
max=i;
min=i;

for(i=2;i<=11;i++)
{
printf("Enter the next number");
scanf("%d",&num);

if(num>max)
max=num;

if (num<min)
min=num;

}
printf("\nThe greatest number is: %d",max);
printf("\nThe smallest number is: %d",min);

}
commented Jan 1, 2021 by (10,500 points)
Pratical & out of the box thinking, :)
commented Jan 2, 2021 by (630 points)
I can't agree more !