In this example, you will learn to find the largest number among the three numbers entered by the user.

This is a C program to calculate the biggest of 3 numbers

Using Nested if else

/*
 * C program to find the biggest of three numbers
 */
#include <stdio.h>
 
void main()
{
    int num1, num2, num3;
 
    printf("Enter the values of num1, num2 and num3\n");
    scanf("%d %d %d", &num1, &num2, &num3);
    printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", num1, num2, num3);
    if (num1 > num2)
    {
        if (num1 > num3)
        {
            printf("num1 is the greatest among three \n");
        }
        else
        {
            printf("num3 is the greatest among three \n");
        }
    }
    else if (num2 > num3)
        printf("num2 is the greatest among three \n");
    else
        printf("num3 is the greatest among three \n");
}

Using if…else

#include <stdio.h>

int main() {

  double n1, n2, n3;

  printf("Enter three numbers: ");
  scanf("%lf %lf %lf", &n1, &n2, &n3);

  // if n1 is greater than both n2 and n3, n1 is the largest
  if (n1 >= n2 && n1 >= n3)
    printf("%.2lf is the largest number.", n1);

  // if n2 is greater than both n1 and n3, n2 is the largest
  else if (n2 >= n1 && n2 >= n3)
    printf("%.2lf is the largest number.", n2);

  // if both above conditions are false, n3 is the largest
  else
    printf("%.2lf is the largest number.", n3);

  return 0;
}

Leave Your Comment