Let's say we have three numbers and 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 .
This programming tutorial will teach us how to print three numbers in descending order in various programming languages. The program we 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>
intmain()
{
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
elseif(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);
}
}
return0;
}
Output
Enter 3 Numbers eg(13 4 14):13 4 14
Descending:14 13 4
C++ Program to Print Three Numbers in Descending Order
#include<iostream>
usingnamespace std;
intmain()
{
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
elseif(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;
}
}
return0;
}
Output
Enter 3 Numbers eg(13 4 14): 13 4 14
Descending: 14 13 4
Python Program to Print Three Numbers in Descending Order
# input3 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
Java Program to Print Three Numbers in Descending Order
import java.util.Arrays;
import java.util.Scanner;
public classDescendingOrder {
public static voidmain(String[] args) {
Scannerscanner = newScanner(System.in);
int[] nums = newint[3];
System.out.println("Enter three numbers:");
for (inti = 0; i < nums.length; i++) {
nums[i] = scanner.nextInt();
}
Arrays.sort(nums);
for (int i = nums.length - 1; i >= 0; i--) {
System.out.print(nums[i] + " ");
}
}
}
Output
Enter three numbers:
1
7
9
9 7 1
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.
Leave a Comment on this Post