C Language

C program to Find Largest of three numbers

C program to Find Largest of three numbers

In this article, we will discuss the concept of C program to Find Largest of three numbers

In this post, we are going to learn how to write a program to find largest number out of three numbers using different methods in C program.

Code to find largest numbers

Code to find largest numbers  using if statements

In this code, we will find largest number out of three numbers using if statements in C language

Program 1

#include <stdio.h>
#include <stdlib.h>
 //int biggestNum(int a, int b,int c);
int main()
{

    int num1,num2,num3; //declare the variables
    printf("Please Enter the first number: ");
    scanf("%d",&num1);//get input from user for num1
    printf("Please Enter the second number: ");
    scanf("%d",&num2);//get input from user for num2
    printf("Please Enter the third number: ");
    scanf("%d",&num3);//get input from user for num3
if(num1>=num2 && num1>=num3){
    printf("\n The largest number is: %d",num1);
  //Checking the num1 is largest
}
if(num2>=num1 && num2>=num3){
    printf("\n The largest number is: %d ",num2);
  //Checking the num2 is largest
}
if(num3>=num1 && num3>=num2){
    printf("\n The largest number is:%d ",num3);
  //Checking the num3 is largest
}
   getch();
    return 0;
}

When the above code is executed , it produces the following result

Please Enter the first number: 365
Please Enter the second number: 987
Please Enter the third number: 654

Largest number is: 987

 

Code to find largest numbers  using if-else if statements

In this code, we will find largest number out of three numbers using if else if statements in C language

Program 2

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

int main()
{
    int num1, num2,num3; //variables declaration
    printf("Enter the first number: ");
    //Ask input from the user
    scanf("%d",&num1);//Reading the input in num1
    printf("Enter the second number: ");
    scanf("%d",&num2);//Reading the input in num2
    printf("Enter the third number: ");
    scanf("%d",&num3);//REading the input in 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",num3);
    }

   getch();
    return 0;
}

When the above code is executed , it produces the following result

Please Enter the first number: 6785
Please Enter the second number: 9834
Please Enter the third number: 1233

Largest number is: 9834

 

Code to find largest numbers  using nested if statements

In this code, we will find largest number out of three numbers using Nested if statements in C language

Program 3

#include <stdio.h>
#include <stdlib.h>
 //int biggestNum(int a, int b,int c);
int main()
{

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

    if(num1>=num2){//checking num1 and num2
            if(num1>=num3){//checking num1 and num3
         printf("\nLargest number is: %d\n",num1);
            }
            else{
                printf("\nLargest number is: %d\n",num3);
            }
    }
    else{
        if(num2>=num3){//checking 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 executed , it produces the following result

Please Enter the first number: 786
Please Enter the second number: 432
Please Enter the third number: 987

Largest number is: 987

 

Code to find largest numbers  using function

In this code, we will find largest number out of three numbers using function in C language

Program 4

#include <stdio.h>
#include <stdlib.h>
biggestNumber(int,int,int);//function prototype
int main()
{

    int a,b,c;
    printf("Enter the three numbers\n");
    scanf("%d%d%d",&a,&b,&c);
    int result=biggestNumber(a,b,c);//Calling the function
    printf("Biggest number is: %d\n",result);
    //display the result
    getch();
    return 0;
}
int biggestNumber(int a,int b,int c){//function definition
if(a>b)
{
    if(a>c)
    return a;
    else
        return c;
}
else
{
    if(b>c)
        return b;
    else
        return c;
}
}

When the above code is executed , it produces the following result

Enter the three numbers 765
123
567

Biggest number is: 765

Code to find largest numbers  using ternary operator

In this code, we will find largest number out of three numbers using ternary operator in C language

Program 5

#include <stdio.h>
#include <stdlib.h>
 //int biggestNum(int a, int b,int c);
int main()
{

    int num1,num2,num3; //declare the variables
    printf("Please Enter three numbers: ");
    //Ask input from the user
    scanf("%d %d %d",&num1,&num2,&num3);
    //get input from user and store the variables

int result=num3>(num1>num2?num1:num2)?num3:((num1>num2)? num1:num2);
printf("Largwst number is: %d",result);
getch();
    return 0;
}

When the above code is executed , it produces the following result

Please Enter three numbers: 567
432
879
Largest number is:  879

 

Program 6

#include <stdio.h>
#include <stdlib.h>
 //int biggestNum(int a, int b,int c);
int main()
{

    int num1,num2,num3; //declare the variables
    printf("Enter three numbers: ");
    //Ask input from the user
    scanf("%d %d %d",&num1,&num2,&num3);
    //get input from user and store the variables

int temp=(num1>num2)? num1:num2;
int largest= num3>temp?num3:temp;
printf("Largest number is: %d",largest);
getch();
    return 0;
}

When the above code is executed , it produces the following result

Please Enter three numbers: 1004
2045
5460
Largest number is:  5460

 

Code to find largest numbers  using Array in C language

In this code, we will find largest number out of three numbers using Array in C language

Program 7

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

int main()
{
    int arr[10], size,i;
    printf("Enter the number of elements in an array\n");
    scanf("%d",&size);//takes input from user for array length
    printf("Enter %d integers \n",size);

    for(i=0; i<size; i++){
   printf("Enter the element %d: ",(i+1));
   scanf("%d",&arr[i]);//takes input from user for array
   }
int max=arr[0];
   for(i=0; i<size; i++){
     if(max<arr[i]){
     max=arr[i];
   }
   }
   printf("\nThe largest value is:%d ",max);
   getch();
    return 0;
}

 

Enter the number of elements in an array
3
Enter 3 integers
Enter the element 1: 45
Enter the element 1: 56
Enter the element 1: 67
The largest value is : 67

 

Suggested post

Operator in C language

if statements in C language

Nested if statement in C language

Array in C language

Function in C language

 

Similar post

Java code to find middle of three

C code to find middle of three

C++ code to find middle of three

Python code to find middle of three

 

Java program to Find largest of three numbers

C program to Find largest of three numbers

C++ program to Find largest of three numbers

Python program to Find largest of three numbers

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.