In: Computer Science
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code should print the numbers in descending order.
CODE IN C++ :
#include <iostream>
using namespace std;
int main()
{
unsigned int arr[4]; // stores 4 whole numbers
cout << "Enter four whole numbers: ";
for(int i = 0; i < 4; i++)
{
cin >> arr[i]; // taking input from the user
}
// sorting in descending order
for(int i = 0; i < 4; i++)
{
for(int j = i+1; j < 4; j++)
{
if(arr[i] < arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
// printing the numbers in descending order
cout << "Numbers in descending order: ";
for(int i = 0; i < 4; i++)
{
cout << arr[i] << " ";
}
}
OUTPUT SNIPPET :
Hope this resolves your doubt. Please give an upvote if you liked my solution. Thank you :)