C PROGRAMMING PROGRAM • CONTROL FLOW & LOOP FORMS

Find the Larger Number Using the Conditional Operator

Learn how to find the larger number using the conditional operator using a clear C program.

Beginner?: operatorExpressionComparison

PROBLEM UNDERSTANDING

Input and expected output

Sample input
27 41
Sample output
Largest = 41

COMPLETE C PROGRAM

Complete C implementation

ternary-largest-two.c
Open in compiler
#include <stdio.h>

int main(void)
{
    int first, second;
    printf("Enter two integers: ");
    scanf("%d %d", &first, &second);
    int largest = first > second ? first : second;
    printf("Largest = %d\n", largest);
    return 0;
}

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

Largest = 41
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read the required input values.
  2. Evaluate one condition and select either the first or second expression as the result.
  3. Display the computed result.

Evaluate one condition and select either the first or second expression as the result.

EFFICIENCY

Time and space complexity

Time complexity

O(1)

Auxiliary space

O(1)

DEBUGGING CHECKLIST

Common mistakes

Check this

Use the correct format specifier for every variable.

Check this

Initialize variables before using their values.

Check this

Check braces, semicolons and input order carefully.

Try it yourself

Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.