BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to find greatest of three numbers

Last Updated: May 19, 2024 by Chaitanya Singh | Filed Under: C Programs

In this tutorial, you will learn how to write a C program to find greatest of three numbers. We will see three programs: In the first program, we will use if statement, second program if..else statement and in third program we will use nested if..else statement to find the greatest number.

Example 1: Program to find largest number using if statement

#include <stdio.h>

int main() {

  double num1, num2, num3;

  printf("Enter first number: ");
  scanf("%lf", &num1);
  printf("Enter second number: ");
  scanf("%lf", &num2);
  printf("Enter third number: ");
  scanf("%lf", &num3);

  // if num1 is greater than num2 & num3, num1 is the largest
  if (num1 >= num2 && num1 >= num3)
    printf("%lf is the largest number.", num1);

  // if num2 is greater than num1 & num3, num2 is the largest
  if (num2 >= num1 && num2 >= num3)
    printf("%lf is the largest number.", num2);

  // if num3 is greater than num1 & num2, num3 is the largest
  if (num3 >= num1 && num3 >= num2)
    printf("%lf is the largest number.", num3);

  return 0;
}

In this program, we are using if statement. The steps followed in this program are:

1. Prompts the user to enter three numbers one by one. Entered numbers are stored in three variables num1, num2 and num3 using scanf() function.
2. It compares num1 with other two variables num2 & num3 and if num1 is grater than both of these numbers then print num1 is the largest number.
3. Similarly, it compares num2 with num1 & num3 and if num2 is greater than both of these numbers, then it prints num2 as the output.
4. Similar to step 2 and 3, it compares num3 with num1 and num2 and if it is greater than both the numbers, prints num3 is the largest number.

Output:

C Program to find greatest of three numbers

Example 2: Program to find largest number using if..else statement

In this program, we are using if-else statement. The logic of the program is quite similar to the Example 1, however here, if any of the condition is true, the program prints the largest number and doesn’t execute other blocks thus, provide efficient execution.

#include <stdio.h>
int main() {

  double num1, num2, num3;

  printf("Enter first number: ");
  scanf("%lf", &num1);
  printf("Enter second number: ");
  scanf("%lf", &num2);
  printf("Enter third number: ");
  scanf("%lf", &num3);

  if (num1 >= num2 && num1 >= num3)
    printf("%lf is the largest number.", num1);

  else if (num2 >= num1 && num2 >= num3)
    printf("%lf is the largest number.", num2);

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

  return 0;
}

Output:

Enter first number: 10
Enter second number: 50
Enter third number: 1
50.000000 is the largest number.

Example 3: Program to find largest number using nested if..else

Here, we are using nested if-else statement for the comparison. To read more about nested if-else statement refer this article.

#include <stdio.h>

int main() {

  double num1, num2, num3;

  printf("Enter first number: ");
  scanf("%lf", &num1);
  printf("Enter second number: ");
  scanf("%lf", &num2);
  printf("Enter third number: ");
  scanf("%lf", &num3);

  if (num1 >= num2) {
    if (num1 >= num3)
      printf("%.2lf is the largest number.", num1);
    else
      printf("%.2lf is the largest number.", num3);
  }
  else {
    if (num2 >= num3)
      printf("%.2lf is the largest number.", num2);
    else
      printf("%.2lf is the largest number.", num3);
  }

  return 0;
}

Output:

Enter first number: 100
Enter second number: 999
Enter third number: 1202
1202.00 is the largest number.
❮ C TutorialC Programs ❯

Top Related Articles:

  1. C Program to find the Size of int, float, double and char
  2. C Program to Generate Multiplication Table
  3. C Program to Find GCD of two Numbers
  4. C Program to Count Vowels and Consonants in a String using Pointer
  5. Hello World Program in C

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap