Solution for Exercise 8-22
ex0822
#include <stdio.h>
int main()
{
int a,b,larger;
printf("Enter value A: ");
scanf("%d",&a);
printf("Enter different value B: ");
scanf("%d",&b);
if( a > b )
larger = a;
else
larger = b;
printf("Value %d is larger.\n",larger);
return(0);
}
The following is also a valid solution:
#include <stdio.h>
int main()
{
int a,b;
printf("Enter value A: ");
scanf("%d",&a);
printf("Enter different value B: ");
scanf("%d",&b);
if( a > b )
printf("Value %d is larger.\n",a);
else
printf("Value %d is larger.\n",b);
return(0);
}
Output
Enter value a: 10
Enter different value B: -10
Value 10 is larger.
Notes
* The first solution better duplicates the ternary operator.
* This code is fine, but somehow it lacks the stark brevity that the ?: operator offers.
Copyright © 1997-2025 by QPBC.
All rights reserved
