C Online Compiler
Example: Greatest of Three Numbers in C using Ternary Operator
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Greatest of Three Numbers in C using Ternary Operator #include <stdio.h> // It's the main function of the program int main() { int num1, num2, num3; // Step-1 Enter three integer values printf("INPUT-1: "); scanf("%d", &num1); printf("INPUT-2: "); scanf("%d", &num2); printf("INPUT-3: "); scanf("%d", &num3); // Step-2 Compare each value to find the greatest // If num1 is greater than both num2 and num3, num1 is the greatest num1 >= num2 &&num1 >= num3 ? printf("\nGreatest Number :: %d", num1) : ( // If num2 is greater than both num1 and num3, num2 is the greatest num2 >= num1 && num2 >= num3 ? printf("\nGreatest Number :: %d", num2) // If the above conditions are false then, num3 is the greatest : printf("\nGreatest Number :: %d", num3)); return 0; }
90 25 110
Output
Clear
ADVERTISEMENTS