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

+5 votes
asked Dec 28, 2020
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);

}

## 4 Answers

+2 votes
answered Dec 31, 2020 by (7,630 points)
selected Jan 2 by Wryyy!!

Best answer

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 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)
+3 votes
answered Jan 1 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 by (7,630 points)
Pratical & out of the box thinking, :)
commented Jan 2 by (320 points)
I can't agree more !
0 votes
answered Jan 2 by (320 points)
Hello and thank you for your answers ! :D
0 votes
answered Jan 2 by (140 points)

your logic is absolutely correct and astonishing buddy but the problem is that you have set the min value to 0. so there's ought to be an error because no positive value is less than 0. had it been you have set it to the greatest possible value than it would definitely workout.

or else you can also use the following logic:-

assign the value of first entered number to the min value. and then compare the other values by for loop with it