Mohamad Hanif - 8 months ago 260

C Question

Write a program that would take 5 integers and prints the largest number and the smallest number

i tried this code

`#include <stdio.h>`

#include <stdlib.h>

int main()

{

int num1, num2, num3, num4, num5;

int largest;

int smallest;

printf ( "\nEnter five integers." );

printf ( "\nAnd I will give you the smallest and the largest." );

scanf( "%d%d%d%d%d", &num1, &num2, &num3, &num4, &num5 );

if ( num1 >= num2, num3, num4, num5 )

{

( "num1 = largest" );

}

if ( num1 <= num2, num3, num4, num5 )

{

( "num1 = smallest" );

}

if ( num2 >= num1, num3, num4, num5 )

{

( "num2 = largest" );

}

if ( num2 <= num1, num3, num4, num5 )

{

( "num2 = smallest" );

}

if ( num3 >= num1, num2, num4, num5 )

{

( "num3 = largest" );

}

if ( num3 <= num1, num2, num4, num5 )

{

( "num3 = smallest" );

}

if ( num4 >= num1, num2, num3, num5 )

{

( "num4 = largest" );

}

if ( num4 <= num1, num2, num3, num5 )

{

( "num4 = smallest" );

}

if ( num5 >= num1, num2, num3, num4 )

{

( "num5 = largest" );

}

if ( num5 <= num1, num2, num3, num4 )

{

( "num5 = smallest" );

}

printf ( "The largest integer is %d.", largest);

printf ( "The smallest integer is %d.", smallest);

return 0;

}

but there's somthing wrong!

please can any one help to solve this ?

and if we can do this with while loop please explaine

thanks

Answer Source

In expressions like this

```
if ( num1 >= num2, num3, num4, num5 )
```

there is used so-called comma operator and the result of the whole expression corresponds to the value `num5 != 0`

.

As you asked in fact two questions: the one is about how to write a program for this assignment

take 30 integers and prints the largest number and the smallest number

and other one is about how to write a program for this assignment

take 5 integers and prints the largest number and the smallest number

Then I will show a demonstrative program that performs the both assignments.:)

```
#include <stdio.h>
int main(void)
{
{
const size_t N = 30;
int value;
int smallest, largest;
printf ( "\nEnter %zu integers.", N );
printf ( "\nAnd I will give you the smallest and the largest.\n" );
size_t i = 0;
while ( i < N && scanf( "%d", &value ) == 1 )
{
if ( i++ == 0 )
{
smallest = largest = value;
}
else
{
if ( value < smallest )
{
smallest = value;
}
else if ( largest < value )
{
largest = value;
}
}
}
if ( i != 0 )
{
printf( "\nAmong %zu entered values "
"the smallest is %d and the largest is %d\n",
i, smallest, largest );
}
}
{
int num1, num2, num3, num4, num5;
printf ( "\nEnter five integers." );
printf ( "\nAnd I will give you the smallest and the largest.\n" );
scanf( "%d%d%d%d%d", &num1, &num2, &num3, &num4, &num5 );
if ( !( num2 < num1 ) && !( num3 < num1 ) && !( num4 < num1 ) && !( num5 < num1 ) )
{
printf( "The smallest number is the first number with value %d\n", num1 );
}
else if ( !( num3 < num2 ) && !( num4 < num2 ) && !( num5 < num2 ) )
{
printf( "The smallest number is the second number with value %d\n", num2 );
}
else if ( !( num4 < num3 ) && !( num5 < num3 ) )
{
printf( "The smallest number is the third number with value %d\n", num3 );
}
else if ( !( num5 < num4 ) )
{
printf( "The smallest number is the fouth number with value %d\n", num4 );
}
else
{
printf( "The smallest number is the fifth number with value %d\n", num5 );
}
if ( !( num1 < num2 ) && !( num1 < num3 ) && !( num1 < num4 ) && !( num1 < num5 ) )
{
printf( "The largest number is the first number with value %d\n", num1 );
}
else if ( !( num2 < num3 ) && !( num2 < num4 ) && !( num2 < num5 ) )
{
printf( "The largest number is the second number with value %d\n", num2 );
}
else if ( !( num3 < num4 ) && !( num3 < num5 ) )
{
printf( "The largest number is the third number with value %d\n", num3 );
}
else if ( !( num4 < num5 ) )
{
printf( "The largest number is the fouth number with value %d\n", num4 );
}
else
{
printf( "The largest number is the fifth number with value %d\n", num5 );
}
}
return 0;
}
```

Its output might look like

```
Enter 30 integers.
And I will give you the smallest and the largest.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Among 30 entered values the smallest is 1 and the largest is 30
Enter five integers.
And I will give you the smallest and the largest.
1 2 3 4 5
The smallest number is the first number with value 1
The largest number is the fifth number with value 5
```

You can yourself correct any typo in the program that you'll find.:)