Q. Write An Algorithm And Program To Find Greatest of Three Numbers.

Here you will find an algorithm and program to find greatest of three numbers. Given three numbers and our task is to find the largest number among the three numbers.

Now let us understand what logic we need to use in this -
In the program three integer numbers are given num1, num2, num3, it would use "if condition" and will compare and display the greatest number as output.

For Example :-

Input : num1 = 2, num2 = 8, num3 = 1
Output : Largest number = 8
Explanation : As we can see that there are three integer number : num1 = 2, num2 = 8, num3 = 1. Output is the largest number amoung the three number i.e. Largest number = 8

Algorithm To Find Greatest of Three Numbers.

\\Algorithm To Find the Largest Number Among Three Numbers.
START
Step 1: [ Take Input ] Read: Number Num1, Num2, Num3
Step 2: If Num1 > Num2.
			If true, then check if Num1 > Num3.
				If true, print 'Num1' as the greatest number.
				If false, print 'Num3' as the greatest number.
			If false, then check if Num2 > Num3.
				If true, print 'Num2' as the greatest number.
    			If false, print 'Num3' as the greatest number.

END


Program To Find Greatest of Three Numbers

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • //C Program To Find the Largest Number Among Three Numbers
    #include <stdio.h> 
    
    int main() 
    { 
    	//initializing numbers to compare 
        int Num1=10, Num2=15, Num3=-10; 
      
        printf("The Three Number are Num1 = %d, Num2 = %d, Num3 = %d \n", Num1, Num2, Num3); 
        
        //comparing numbers, Num1 with Num2 and Num1 with Num3   
        //if both conditions are true, prints Num1  
        if (Num1 >= Num2 && Num1 >= Num3) 
            printf("Largest number: %d", Num1); 
    
        //comparing Num2 with Num1 and Num2 with Num3  
        //if both conditions are true, prints Num2 
        if (Num2 >= Num1 && Num2 >= Num3) 
            printf("Largest number: %d", Num2); 
    
        //comparing Num3 with Num1 and Num3 with Num2 
        //if both conditions are true, prints Num3  
        if (Num3 >= Num1 && Num3 >= Num2) 
            printf("Largest number: %d", Num3); 
      
        return 0; 
    } 
    
    //C++ Program To Find the Largest Number Among Three Numbers
    #include <iostream>
    using namespace std;
    
    int main() {   
    	//initializing numbers to compare 
        int Num1 = 10, Num2 = 15, Num3 = -10;
    
        cout <<"The Three Number are Num1 = "<<Num1 <<", Num2 = "<<Num2<<", Num3 = "<<Num3<<"\n";
    
        //comparing numbers, Num1 with Num2 and Num1 with Num3   
        //if both conditions are true, prints Num1
        if(Num1 >= Num2 && Num1 >= Num3)
            cout << "Largest number: " << Num1;
    
        //comparing Num2 with Num1 and Num2 with Num3  
        //if both conditions are true, prints Num2 
        if(Num2 >= Num1 && Num2 >= Num3)
            cout << "Largest number: " << Num2;
        
        //comparing Num3 with Num1 and Num3 with Num2 
        //if both conditions are true, prints Num3
        if(Num3 >= Num1 && Num3 >= Num2)
            cout << "Largest number: " << Num3;
      
        return 0;
    }
    
    //Java Program To Find the Largest Number Among Three Numbers
    
    import java.util.Scanner;
    public class LFC
    {
      public static void main (String[]args)
      {
        //initializing numbers to compare  
        int Num1 = 10, Num2 = 15, Num3 = -10;
        System.out.println ("The Three Numbers are Num1 = "+Num1+", Num2 = "+Num2+", Num3 = "+Num3);
        //comparing numbers, Num1 with Num2 and Num1 with Num3   
        //if both conditions are true, prints Num1  
        if (Num1 >= Num2 && Num1 >= Num3)
            System.out.println ("Largest Number: "+Num1);
        //comparing Num2 with Num1 and Num2 with Num3  
        //if both conditions are true, prints Num2  
        else if (Num2 >= Num1 && Num2 >= Num3)
            System.out.println ("Largest Number: "+Num2);
        else
          //prints Num3 if the above conditions are false  
            System.out.println ("Largest number: "+Num3);
      }
    }
    
    #Python Program To Find the Largest Number Among Three Numbers
    
    # initializing numbers to compare 
    Num1 = 10
    Num2 = 15
    Num3 = -10
    
    #comparing numbers, Num1 with Num2 and Num1 with Num3 
    if (Num1 >= Num2) and (Num1 >= Num3):
       largest = Num1
    #comparing numbers, Num2 with Num1 and Num2 with Num3 
    elif (Num2 >= Num1) and (Num2 >= Num3):
       largest = Num2
    #comparing numbers, Num3 with Num1 and Num3 with Num2
    else:
       largest = Num3
    
    print("Largest number: ", largest)
    
    //C# Program To Find the Largest Number Among Three Numbers
    
    using System;
    class LFC {
      static void Main() {
        //initializing numbers to compare 
        int Num1 = 10, Num2 = 15, Num3 = -10;
        
        //comparing numbers, Num1 with Num2 and Num1 with Num3   
        //if both conditions are true, prints Num1
        if(Num1 >= Num2 && Num1 >= Num3)
            Console.Write("Largest Number : "+Num1);
    
        //comparing Num2 with Num1 and Num2 with Num3  
        //if both conditions are true, prints Num2 
        if(Num2 >= Num1 && Num2 >= Num3)
            Console.Write("Largest Number : "+Num2);
        
        //comparing Num3 with Num1 and Num3 with Num2 
        //if both conditions are true, prints Num3
        if(Num3 >= Num1 && Num3 >= Num2)
            Console.Write("Largest Number : "+Num3);
        
      }
    }
    
    //PHP Program To Find the Largest Number Among Three Numbers
    
    //initializing numbers to compare 
    $Num1 = 40;
    $Num2 = 25;
    $Num3 = 7;
    
    echo "The three numbers are Num1 = $Num1, Num2 = $Num2, Num3 = $Num3 
    "; //comparing numbers, Num1 with Num2 and Num1 with Num3 //if both conditions are true, prints Num1 if(($Num1 > $Num2) && ($Num1 > $Num3)) echo "Largest Number: $Num1"; //comparing numbers, Num2 with Num3 else if($Num2 > $Num3) echo "Largest Number: $Num2"; else echo "Largest Number: $Num3";
    \\C Program To Find the Largest Number Among Three Numbers
    #include <stdio.h> 
    
    int main() 
    { 
    	//initializing numbers to compare 
        int Num1=10, Num2=15, Num3=-10; 
      
        printf("The Three Number are Num1 = %d, Num2 = %d, Num3 = %d \n", Num1, Num2, Num3); 
        
        //comparing numbers, Num1 with Num2 and Num1 with Num3   
        //if both conditions are true, prints Num1  
        if (Num1 >= Num2 && Num1 >= Num3) 
            printf("Largest number: %d", Num1); 
    
        //comparing Num2 with Num1 and Num2 with Num3  
        //if both conditions are true, prints Num2 
        if (Num2 >= Num1 && Num2 >= Num3) 
            printf("Largest number: %d", Num2); 
    
        //comparing Num3 with Num1 and Num3 with Num2 
        //if both conditions are true, prints Num3  
        if (Num3 >= Num1 && Num3 >= Num2) 
            printf("Largest number: %d", Num3); 
      
        return 0; 
    } 
    

    Output

    The Three Number are Num1 = 10, Num2 = 15, Num3 = -10
    Largest number: 15
    

    Recommended Programs

       Program to find factorial of a number
       Program to count number of digits in a number