Program to find largest among three numbers in C

Program to find the largest among three numbers in the C

In this program, we will discuss a simple concept of the program to find the largest number among three numbers in the C programming language.

In this topic, we are going to learn how to find the biggest number from given three numbers. we can use the operator in C language to find the biggest number of this program.

 

find largest number among three numbers

Find largest among three numbers

Using if statements to find the largest number

Among the integer numbers

Here is a clear description with an example C program to find the largest among three integer numbers, written in a way that’s also suitable for a programming tutorial or blog post.

Program 1  

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1, num2,num3; //declare the variables
    printf("Enter the first number to compare: ");
    scanf("%d",&num1);//get input from user for num1
    printf("Enter the second number to compare: ");
    scanf("%d",&num2);//get input from user for num2
    printf("Enter the third number to compare: ");
    scanf("%d",&num3);//get input from user for num3

    if(num1>=num2 && num1>=num3){
         printf("\nLargest number is: %d\n",num1);
    }//num1 compare with num 2 and num 3
    if(num2>=num1 && num2>=num3){
         printf("\nLargest number is: %d\n",num2);
    }//num2 compare with num1 and num3
    if(num3>=num1 && num3>=num2){
         printf("\nLargest number is: %d\n",num2);
    }//num3 compare with num1 and num2

   getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter the first number to compare: 67
Enter the second number to compare: 98
Enter the third number to compare: 45

Largest number is: 98

This C program is used to find the largest number among three integer values entered by the user.
The program compares the three numbers using conditional statements (if–else) and determines which number is greater than the others.

 

Among the floating point numbers

Here is a clear description with an example C program to find the largest among three floating-point numbers, written in a way that’s also suitable for a programming tutorial or blog post.

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    double num1, num2,num3; //declare the variables
    printf("Enter the first number to compare: ");
    scanf("%lf",&num1);//get input from user for num1
    printf("Enter the second number to compare: ");
    scanf("%lf",&num2);//get input from user for num2
    printf("Enter the third number to compare: ");
    scanf("%lf",&num3);//get input from user for num3

    if(num1>=num2 && num1>=num3){
         printf("\nLargest number is: %.2f\n",num1);
    }//num1 compare num 2 and num 3
    if(num2>=num1 && num2>=num3){
         printf("\nLargest number is: %.2f\n",num2);
    }//num2 compare num1 and num3
    if(num3>=num1 && num3>=num2){
         printf("\nLargest number is: %.2f\n",num2);
    }//num3 compare num1 and num2

   getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter the first number to compare: 5.6
Enter the second number to compare: 8.1
Enter the third number to compare: 3.4

Largest number is: 8.10

This C program is used to find the largest number among three floating-point values entered by the user.
The program compares the three numbers using conditional statements (if–else) and determines which number is greater than the others.

 

Using Nested if-else statements for largest number

Program 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2,num3; //declare the variables
    printf("Enter the first number to compare: ");
    scanf("%d",&num1);//get input from user for num1
    printf("Enter the second number to compare: ");
    scanf("%d",&num2);//get input from user for num2
    printf("Enter the third number to compare: ");
    scanf("%d",&num3);//get input from user for num3

    if(num1>=num2){//compare num1 and num2
            if(num1>=num3){//compare num1 and num3
         printf("\nLargest number is: %d\n",num1);
            }
            else{
                printf("\nLargest number is: %d\n",num3);
            }
    }
    else{
        if(num2>=num1){//compare num2 and num1
            printf("\nLargest number is: %d\n",num2);
        }
        else{
            printf("\nLargest number is: %d\n",num3);
        }

    }
   getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter the first number to compare: 45
Enter the second number to compare: 87
Enter the third number to compare: 57

Largest number is: 87

 

Using Nested if statements for largest number

Program 4

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1, num2,num3; //declare the variables
    printf("Enter the first number to compare: ");
    scanf("%d",&num1);//get input from user for num1
    printf("Enter the second number to compare: ");
    scanf("%d",&num2);//get input from user for num2
    printf("Enter the third number to compare: ");
    scanf("%d",&num3);//get input from user for num3

    if(num1>=num2 && num1>=num3){
         printf("\nLargest number is: %d\n",num1);
    }//num1 compare num 2 and num 3
    else if(num2>=num1 && num2>=num3){
         printf("\nLargest number is: %d\n",num2);
    }//num2 compare num1 and num3
    else{
         printf("\nLargest number is: %d\n",num2);
    }

   getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter the first number to compare: 56
Enter the second number to compare: 89
Enter the third number to compare: 65

Largest number is: 89

 

Suggested for you

If statements in C language

Nested if statements in C language

Operator in C language

Data type in C language

Variable in C language

 

Similar post

 largest number of three number in C

 largest number of three number in C++

 largest number of three number in Python

 largest number of three number in C using function

 largest number of three number in C++ using function

 largest number of three number in Python using function

 largest number of three number in Java using method

All type of the operators in Java with example
Program to find largest number among three numbers in Cpp
C languageoperator