Write a Program to find the greatest number among the three numbers

Posted in

Write a Program to find the greatest number among the three numbers
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    Problem Statement

    We have given 3 numbers num_1 , num_2 and num_3 , as input from the user and we need to write a script or program that print the greatest one.

    For Example

    Input

    num_1 = 20
    num_2 = 30
    num_3 = 40

    Output

    The greatest number is: 40

    Solution

    The problem is very simple and beginner level. To solve the problem we just need to check if the num_1 is greater or equal to than num_2 and num_3 . If yes we will print the num_1 is the greatest, if not we will check if num_2 is greater than or equal num_1 and num_3 if yes we will print that num_2 is the greatest. Even if num_2 is not the greatest we will simply print that num_3 is the greatest. To solve this problem we only require of...else statement.

    C Program to Find the Greatest Number among the three numbers

    #include <stdio.h>
    
    int main()
    {	
    	int num_1,num_2,num_3;
    	//input the first number
    	printf("Enter the first number: ");
    	scanf("%d", &num_1);
    	
    	//input the second number
    	printf("Enter the second number: ");
    	scanf("%d", &num_2);
    	
    	// input the third number 
    	printf("Enter the third number: ");
    	scanf("%d",&num_3);
    	
    	//if the number 1 is the greatest
    	if(num_1 >= num_2 && num_1>=num_3)
    		printf("The greatest number is: %d",num_1);
    	
    	// if teh number 2 is the greatest
    	else if(num_2 >=num_1 && num_2>=num_3)
    		printf("The greatest number is: %d",num_2);
    	
    	// if the number 3 is the greatest
    	else
    		printf("The greatest number is: %d" ,num_3);
    	return 0;
    }

    Output

    Enter the first number: 12
    Enter the second number: 45
    Enter the third number: 15
    The greatest number is: 45

    C++ Program to find the greatest number among the three numbers

    #include<iostream>
    using namespace std;
    int main()
    {
    	int num_1,num_2,num_3;
    	
    	//input the first number
    	cout<<"Enter the first number: "; cin>>num_1;
    	
    	//input the second number
    	cout<<"Enter the second number: "; cin>>num_2;
    	
    	//input the third number
    	cout<<"Enter the third number: "; cin>>num_3;
    	
    	//check if number 1 is the greatest
    	if(num_1 >= num_2 && num_1>=num_3)
    		cout<<"The greatest number is: "<<num_1; 
           
           //check if number 2 is the greatest 
           else if(num_2 >=num_1 && num_2>=num_3)
    		cout<<"The greatest number is: "<<num_2;
    	
    	//if the number 3 is the greatest
    	else
    		cout<<"The greatest number is: " <<num_3;
    	return 0; 
    }

    Output:

    Enter the first number: 12
    Enter the second number: 23
    Enter the third number: 54
    The greatest number is: 54

    Python Program to find the greatest number among the three numbers

    # input the first number
    num_1= int(input("Enter the first number: "))
    
    # input the second number
    num_2= int(input("Enter the second number: "))
    
    # input the third number
    num_3= int(input("Enter the third number: "))
    
    # if the number 1 is the greatest
    if num_1>=num_2  and num_1 >= num_3:
        print("The greatest number is:", num_1)
    
    # if the numer 2 is the greatest
    elif num_2>=num_1  and num_2 >= num_2:
        print("The greatest number is:", num_2)
    
    # if the number 3 is the greatest
    else:
        print("The greatest number is:", num_3)

    Output:

    Enter the first number: 45
    Enter the second number: 78
    Enter the third number: 23
    The greatest number is: 78

    Wrapping Up!

    In this programming tutorial, we learned how to find out the greatest number among 3 numbers. The trick to solve this problem is lies behind the use of if....else statements with proper conditions. Here we only have 3 numbers which mean we can easily use the if....else statement and check for every number if the number is greater than or equal to the other two numbers.

    People are also reading:

    Leave a Comment on this Post

    0 Comments