Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

The wrong with if funtion

1 réponse
Avatar
767204882
Hi,I am a new C++ learner.The follow is my program to compare the
largest and the smallest number in five numbers.However ,it always
produces wrong results.More strangely ,the largest output right,while
the smallest wrong.I don't known why


#include<stdio.h>

int main()
{
int num1,num2,num3,num4,num5;
int max=num1;
int min=num5;

printf("Enter the five numbers:\n");
scanf("%d %d %d %d %d",&num1,&num2,&num3,&num4,&num5);

if(num2>max)
max=num2;
if(num3>max)
max=num3;
if(num4>max)
max=num4;
if(num5>max)
max=num5;
printf("The largest number is %d",max);

if(num1<min)
min=num1;
if(num2<min)
min=num2;
if(num3<min)
min=num3;
if(num4<min)
min=num4;
printf("The smallest number is %d",min);
return 0;
}

1 réponse

Avatar
xylo
Le Thu, 30 Oct 2008 23:27:09 -0700, 767204882 a écrit:

int num1,num2,num3,num4,num5;
int max=num1;
int min=num5;



/*
wrong, you must assign min and max after you get your five numbers:
int max,
min;
*/


printf("Enter the five numbers:n");
scanf("%d %d %d %d %d",&num1,&num2,&num3,&num4,&num5);



/* assign min an max here */
max = num1;
min = num5;

if(num2>max)
max=num2;
if(num3>max)
max=num3;
if(num4>max)
max=num4;
if(num5>max)
max=num5;
printf("The largest number is %d",max);

if(num1<min)
min=num1;
if(num2<min)
min=num2;
if(num3<min)
min=num3;
if(num4<min)
min=num4;
printf("The smallest number is %d",min);





--
Apply rot13 to this e-mail address before using it.
JM Marino
http://jm.marino.free.fr