+5 votes

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);

}

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);

}

+2 votes

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
Rudra Sai
(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

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);

}

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);

}

0 votes

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

...