Write a Program to Print Three Numbers in Descending Order

By | January 14, 2022
Write a Program to print three numbers in descending order

Let’s say that we have three numbers and we need to sort them in descending order. How would we do that? Well, the answer is that we will use the if-else statement.

Every high-level programming language supports if-else statements. In this programming tutorial, we will learn how to print three numbers in descending order.

The program that we will create needs to ask a user to enter 3 numbers. Then by using the if-else statement, we will sort the numbers in descending order.

Example

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

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

Wrapping Up

In this tutorial, we learned how to implement a program that prints three numbers in descending order in three different programming languages, namely C, C++, and Python. The algorithm of such a program is very simple. First, we check for the largest number among the three values, and 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:

Vamware

Leave a Reply

Your email address will not be published.