C++ Examples

WAP in C++ to read Seven Numbers,sort in descending order

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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

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

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X