The Program in C++ to read Seven Numbers,sort in descending order is given below:
#include <iostream>
using namespace std;
int main() {
int num[7]; // array to store the numbers
cout << "Enter 7 numbers: ";
for (int i = 0; i < 7; i++) {
cin >> num[i];
}
// Sorting the numbers in descending order
for (int i = 0; i < 6; i++) {
for (int j = i + 1; j < 7; j++) {
if (num[i] < num[j]) {
int temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
cout << "Sorted numbers in descending order: ";
for (int i = 0; i < 7; i++) {
cout << num[i] << " ";
}
cout << endl;
return 0;
}
Output:
Enter 7 numbers: 9
1
9
1
4
6
1
3
4
Sorted numbers in descending order: 9 9 6 4 4 1 1
Pro-Tips💡
This program uses an array ‘num’ to store the numbers entered by the user.
The first for loop reads seven numbers from the user and stores them in the array.
The second for loop uses the bubble sort algorithm, to sort the numbers in descending order.
It goes through the array and compare the current number with the next number,
if the current number is smaller than the next one, the program swap the two numbers, this loop goes through the array multiple times until the array is sorted.
Finally, the program prints the sorted numbers to the console.
Note that this program uses bubble sort algorithm, which is not the most efficient sorting algorithm for large arrays, but it is simple and easy to understand for beginners.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.