C Online Compiler
Example: Greatest of Three Numbers in C using Switch Case
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Greatest of Three Numbers in C using Switch Case #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); int largest = num1; // Step-2 Compare num2 & num3 to find the largest switch ((num1 > num2) * 1 + (num1 > num3) * 2 + (num2 > num3) * 4) { case 0: largest = num2 > num3 ? num2 : num3; // Choose the larger of num2 or num3 break; case 1: // num1 is greater than num2 and num3 largest = num1; break; case 2: // num1 is greater than num3 but less than num2 largest = num2; break; case 3: // num1 is greater than num2, num2 is greater than num3 largest = num1; break; case 4: // num1 is less than num2, num1 is greater than num3 largest = num3; break; case 5: // num1 is greater than num3, num2 is greater than num3 largest = num2; break; case 6: // num2 is greater than num1, num2 is greater than num3 largest = num2; break; case 7: // num2 is greater than num1 and num3 largest = num2; break; } // Step-3 Final output of the program printf("\nGreatest Number :: %d", largest); return 0; }
4 5 3
Output
Clear
ADVERTISEMENTS