w3resource

C++ Exercises: Read seven numbers and sorts them in descending order

C++ Basic: Exercise-68 with Solution

Write a C++ program that reads seven numbers and sorts them in descending order.

Pictorial Presentation:

C++ Exercises: Read seven numbers and sorts them in descending order

Sample Solution:

C++ Code :

#include <iostream>
using namespace std;

int main() {
    int num[7];
    for (int i = 0; i != 7; ++i) {
        cin >> num[i];
    }
    sort(num, num+7);
    cout << " " << num[6]  << " " << num[5]  << " " << num[4]  << " " << num[3]  << " " << num[2] << " " << num[1] << " " << num[0];
    return 0;
}

Sample Output:

Sample Input 
2 5 8 9 7 3 4 
sample Output
 9 8 7 5 4 3 2

Flowchart:

Flowchart:  Read seven numbers and sorts them in descending order

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C++ program to which prints the central coordinate and the radius of a circumscribed circle of a triangle which is created by three points on the plane surface.
Next: Write a C++ program to read an integer n and prints the factorial of n, assume that n ≤ 10.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.

C++ Programming: Tips of the Day

Why is there no std::stou?

The most pat answer would be that the C library has no corresponding "strtou", and the C++11 string functions are all just thinly veiled wrappers around the C library functions: The std::sto* functions mirror strto*, and the std::to_string functions use sprintf.

Ref: https://bit.ly/3wtz2qA

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook