0
0
Cprogramming~10 mins

Ternary operator in C - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assign the smaller of two numbers to the variable min using the ternary operator.

C
int a = 5, b = 10;
int min = (a [1] b) ? a : b;
Drag options to blanks, or click blank then click option'
A<
B>
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' will assign the larger number.
Using '==' or '!=' will not correctly compare sizes.
2fill in blank
medium

Complete the code to assign the string "even" or "odd" to result based on whether num is even or odd using the ternary operator.

C
int num = 7;
char *result = (num % 2 [1] 0) ? "odd" : "even";
Drag options to blanks, or click blank then click option'
A>
B!=
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' will assign 'even' to odd numbers and vice versa.
Using '>' or '<' does not correctly check for evenness.
3fill in blank
hard

Fix the error in the ternary operator condition to correctly assign the absolute value of num to abs_val.

C
int num = -8;
int abs_val = (num [1] 0) ? -num : num;
Drag options to blanks, or click blank then click option'
A<
B!=
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' will incorrectly negate positive numbers.
Using '==' or '!=' does not check negativity.
4fill in blank
hard

Fill both blanks to create a ternary operator that assigns "positive" if n is greater than zero, otherwise "non-positive".

C
int n = 3;
char *sign = (n [1] 0) ? "positive" : [2];
Drag options to blanks, or click blank then click option'
A>
B"non-positive"
C"negative"
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' in the first blank reverses the logic.
Assigning 'negative' instead of 'non-positive' changes the meaning.
5fill in blank
hard

Fill all three blanks to create a ternary operator that assigns the maximum of x and y to max_val.

C
int x = 4, y = 9;
int max_val = (x [1] y) ? [2] : [3];
Drag options to blanks, or click blank then click option'
A>
Bx
Cy
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' reverses the logic and assigns the minimum.
Swapping 'x' and 'y' in the assignments changes the result.