C Online Compiler
Example: Find Largest Among Three (Nested if) in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Find Largest Among Three (Nested if) #include <stdio.h> int main() { int num1, num2, num3, largest; // Step 1: Prompt user for input printf("Enter three numbers: "); // Step 2: Read the three numbers from user scanf("%d %d %d", &num1, &num2, &num3); // Step 3: Use nested if statements if (num1 >= num2) { // If num1 is greater than or equal to num2 if (num1 >= num3) { // If num1 is also greater than or equal to num3 largest = num1; } else { // Else, num3 must be greater than num1 (and num1 >= num2 is true) largest = num3; } } else { // If num2 is greater than num1 if (num2 >= num3) { // If num2 is also greater than or equal to num3 largest = num2; } else { // Else, num3 must be greater than num2 (and num2 > num1 is true) largest = num3; } } // Step 4: Print the largest number printf("%d is the largest number.\n", largest); return 0; }
Output
Clear
ADVERTISEMENTS