Find the Greatest of the Three Numbers

Given Three integer inputs num1, num2 and num3, the objective is ti write a code to Find the Largest of the Three Numbers in C Language. In this article we will see a  C program to Find Greatest of three numbers in C. We will use if else conditions and ternary operator too to find the same. Here are some of the methods to solve the above mentioned problem,

  • Method 1: Using if-else Statements 2
  • Method 2: Using if-else Statements 2
  • Method 3: Using Ternary Operator

We’ll discuss the above mentioned methods in the sections below in depth.

Method 1: Using if-else Statements 1

				
					// C Code

#include <stdio.h> 
int main ()
{
    int num1, num2, num3;
    
    num1=12,num2=17,num3=19;

    //checking if num1 is greatest
    if (num1 >= num2 && num1 >= num3)
        printf("%d is the greatest", num1);
        
    //checking if num2 is greatest
    else if(num2 >= num1 && num2 >= num3)
         printf("%d is the greatest", num2);

    //checking if num2 is greatest
    else if(num3 >= num1 && num3 >= num2)
         printf("%d is the greatest", num3);
    
    return 0;
}
				
			
				
					// C++ Code

#include <iostream>
using namespace std;
int main ()
{
    int first, second, third;
    first=10,second=20,third=30;
    
    //comparing first with other numbers
    if ((first >= second) && (first >= third))
        cout << first << " is the greatest "; 

    //comparing Second with other numbers 
    else if ((second >= first) && (second >= third))
        cout << second << " is the greatest";
    
    else
        cout << third << " is the greatest";
 
    return 0;
}
// Time complexity : O(1)
// Space complexity : O(1)
				
			
				
					// Java Code

public class Main
{
  public static void main (String[]args)
  {

    int num1 = 10, num2 = 20, num3 = 30;

    //checking if num1 is greatest
    if (num1 >= num2 && num1 >= num3)
        System.out.println (num1 + " is the greatest");

    //checking if num2 is greatest
    else if (num2 >= num1 && num2 >= num3)
        System.out.println (num2 + " is the greatest");

    //checking if num2 is greatest
    else if (num3 >= num1 && num3 >= num2)
        System.out.println (num3 + " is the greatest");
  }
}
				
			

Method 2: Using Nested if-else Statements

				
					# Python Code

num1, num2, num3 = 10 , 30 , 20
max = 0
if num1 >= num2 and num1 >= num3:
  print(num1)
elif num2 >= num1 and num2 >= num3:
  print(num2)
else:
  print(num3)
				
			
				
					// C Code

#include <stdio.h>
int main ()
{
    int num1, num2, num3;
    
    num1=13,num2=45,num3=27;

    //comparing num1 with other numbers
    if ((num1 >= num2) && (num1 >= num3))
        printf("%d is the greatest", num1);
        
    //comparing num2 with other numbers
    else if ((num2 >= num1) && (num2 >= num3))
         printf("%d is the greatest", num2);
    
    // num3 has to be greatest then if not above
    else
         printf("%d is the greatest", num3);
    
    return 0;
}
				
			
				
					// C++ Code

#include <iostream>
using namespace std;
int main ()
{
    int first, second, third;
    
    first=10,second=20,third=30;
    
    int temp, result;

    // find the largest bw first and second and store in temp
    temp = first > second ? first:second;
    
    // find the largest bw temp and third and finally printing it
    result = temp > third ? temp:third;
        
    // the above two ternary statements can be condensed into one ternary statement
    //result = third > (first > second ? first : second) ? third : ((first > second) ? first : second);
    
    cout << result << " is the largest";

 
    return 0;
}
// Time complexity : O(1)
// Space complexity : O(1)
				
			
				
					// Java Code

public class Main
{
  public static void main (String[]args)
  {

    int num1 = 10, num2 = 20, num3 = 30;

    //comparing num1 with other numbers
    if ((num1 >= num2) && (num1 >= num3))
        System.out.println (num1 + " is the greatest");

    //checking if num2 is greatest
    else if (num2 >= num1 && num2 >= num3)
        System.out.println (num2 + " is the greatest");

    // num3 has to be greatest then if not above
    else
        System.out.println (num3 + " is the greatest");
  }
}
				
			
				
					# Python Code 

num1, num2, num3 = 10 , 30 , 20
max = 0
if num1 >= num2:
  if num1 >= num3:
    print(num1)
elif num2 >= num1:
  if num2 >= num3:
    print(num2)
else:
  print(num3)
				
			

Method 3: Using Ternary Operator

				
					// C Code

#include <stdio.h> 
int main ()
{
    int num1, num2, num3;
    
    num1=12,num2=98,num3=84;
    
    int temp, result;    
    
    // find the largest b/w num1 and num2 & store in temp
    temp = num1>num2 ? num1:num2;
    
    // find the largest b/w temp and num3 & finally printing it
    result = temp>num3 ? temp:num3;    
     
    printf(" %d is the largest", result);
    return 0;
}
				
			
				
					// Java Code

public class Main
{
  public static void main (String[]args)
  {

    int num1 = 10, num2 = 20, num3 = 30;

     int temp, result;    
    
    // find the largest b/w num1 and num2 & store in temp
    temp = num1>num2 ? num1:num2;
    
    // find the largest b/w temp and num3 & finally printing it
    result = temp>num3 ? temp:num3;  
    System.out.println (result + " is the greatest");
  }
}
				
			
				
					# Ternary operator using in Python

num1, num2, num3 = 10 , 30 , 20
max = num1 if num1>num2 else num2
max = num3 if num3>max else max
print(max)
				
			

Method 4 : Using inbuilt function

				
					// Using max function in C++

#include <iostream>
#include <algorithm>
using namespace std;

int main ()
{
    int first, second, third;
    
    first=10,second=20,third=30;
    
    int result;

    result =  max(first,max(second, third));
    
    cout << result << " is the largest";

 
    return 0;
}

// Time complexity : O(1)
// Space complexity : O(1)
				
			
				
					// Python program to find max of 3 numbers using inbuilt function

num1, num2, num3 = 10 , 30 , 20
print(max(num1,num2,num3))
				
			

0 Comments

Leave a Reply

Avatar placeholder

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