Answer to Question #191834 in C++ for Rahel Aschalew

Question #191834

Receive 3 numbers and display them in ascending order from smallest to largest



1
Expert's answer
2021-05-11T07:47:48-0400
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int x, y, z;

    cout << "Enter the first number: ";
    cin >> x;

    cout << "Enter the second number: ";
    cin >> y;

    cout << "Enter the third number: ";
    cin >> z;

    if (x > y) {
        swap(x, y);
    }
    
    if (y > z) {
        swap(y, z);
    }
    
    if (x > y) {
        swap(x, y);
    } 
    
    cout << "\nYour numbers in ascending order from smallest to largest: " << endl;
    cout << x << ' ' << y << ' ' << z << endl;
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment