Write a Program to print three numbers in descending order

By | November 13, 2021
Write a Program to print three numbers in descending order

Let’s say we have three numbers and we need to sort them in descending order, how would we do that? The answer is using the if-else statement. Every high-level programming language supports if..else statements. And in this programming tutorial, we will learn how to print three numbers in descending order.

First, we will ask the user to enter 3 numbers, then using the if-else statement sort them in descending order.

Vamware

Example 1

Input: 11 13 10
Output: 10 11 13

C Program to print three numbers in descending order

#include <stdio.h>

int main() 
{
   int num1, num2, num3, small, smaller, smallest;
   
   //input 3 numbers from user
   printf("Enter 3 Numbers eg(13 4 14): ");
   scanf("%d %d %d", &num1, &num2, &num3);
   
   //for num1 is the greatest
   if(num1>=num2 && num1>=num3)
   {
	   	if(num2>=num3)
	   	{
	   		printf("Descending: %d %d %d", num1, num2, num3);
		}
		else
		{
			printf("Descending: %d %d %d", num1, num3, num2);
	}
   } 

   //for num2 is the greatest
   else if(num2>=num1 && num2>=num3)
   {
	   	if(num1>=num3)
	   	{
	   		printf("Descending: %d %d %d", num2, num1, num3);
		}
		else
		{
			printf("Descending: %d %d %d", num2, num3, num1);
	}
   } 
   //for num3 is the greatest
   else
   {
   		if(num2>=num1)
	   	{
	   		printf("Descending: %d %d %d", num3, num2, num1);
		}
		else
		{
			printf("Descending: %d %d %d", num3, num1, num2);
		}
   }
   return 0;
}

Output

Enter 3 Numbers eg(13 4 14): 12 3 123
Descending: 123 12 3

C++ Program to print three numbers in descending order

#include <iostream>
using namespace std;
 
int main() 
{	
  int num1, num2, num3;
   
   //input 3 numbers from user
   cout<<"Enter 3 Numbers eg(13 4 14): "; cin>>num1>>num2>>num3;
   
   //for num1 is the greatest
   if(num1>=num2 && num1>=num3)
   {
	   	if(num2>=num3)
	   	{
	   		cout<<"Descending: " <<num1<<" "<<num2<<" "<<num3;
		}
		else
		{
			cout<<"Descending: "<<num1<<" "<<num3<<" "<<num2; 
   } 
                } 
   //for num2 is the greatest
   else if(num2>=num1 && num2>=num3)
   {
	   	if(num1>=num3)
	   	{
	   		cout<<"Descending: "<<num2<<" "<<num1<<" "<<num3;
		}
		else
		{
			cout<<"Descending: "<<num2<<" "<<num3<<" "<<num1; 
                } 
   } 
   //for num3 is the greatest
   else 
   {
               if(num2>=num1)
	   	{
	   		cout<<"Descending: "<<num3<<" "<<num2<<" "<<num1;
		}
		else
		{
			cout<<"Descending: "<<num3<<" "<<num1<<" "<<num2;
		}
   }
   return 0;
}

Output

Enter 3 Numbers eg(13 4 14): 14 15 2
Descending: 15 14 2

Python Program to print three numbers in descending order

# input 3 numbers from user
num1, num2, num3 = map(int, input("Enter 3 Numbers eg(13 4 14): ").split())

# for num 1 is the greatest
if(num1>=num2 and num1>= num3):
    if(num2>=num3):
        print(f"Descending: {num1} {num2} {num3}")
    else:
        print(f"Descending: {num1} {num3} {num2}")

# for num 2 is the greatest
elif(num2>=num1 and num2>= num3):
    if(num1>=num3):
        print(f"Descending: {num2} {num1} {num3}")
    else:
        print(f"Descending: {num2} {num3} {num1}")

# for num 3 is the greatest
else:
    if(num2>=num1):
        print(f"Descending: {num3} {num2} {num1}")
    else:
        print(f"Descending: {num3} {num1} {num2}")

Output

Enter 3 Numbers eg(13 4 14): 13 4 14
Descending: 14 13 4

Wrapping Up!

In this tutorial, we learned how to implement the Program “to print three numbers in descending order” in three different programming languages. The algorithm of the above program is very simple, first, we check for the individual largest number then in the nested if-else statement we look for the second-largest number. The time and space complexity of the above program is constant because there are only three elements to sort.

People are also reading:

Leave a Reply

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