Answer to Question #125071 in C++ for Zeeshan Ali

Question #125071
Write a program that sorts three integers. The integers are entered from the user input and stored in variables num1, num2, and num3, respectively. The program sorts the numbers so that num1<=num2<=num3.
1
Expert's answer
2020-07-07T09:54:54-0400
#include <iostream>
using namespace std;


int main()
{
    string enter1, enter2, enter3;
    int num1, num2, num3;
    cout << "Enter integer number 1: ";
    cin >> enter1;
    cout << "Enter integer number 2: ";
    cin >> enter2;
    cout << "Enter integer number 3: ";
    cin >> enter3;
    num1 = atoi(enter1.c_str());
    num2 = atoi(enter2.c_str());
    num3 = atoi(enter3.c_str());


    int temp;
    if (num2 <= num1)
    {
        temp = num2;
        num2 = num1;
        num1 = temp;
    }
    if (num3 <= num1)
    {
        temp = num3;
        num3 = num1;
        num1 = temp;
    }
    if (num3 <= num2)
    {
        temp = num3;
        num3 = num2;
        num2 = temp;
    }
    
    cout << "\nsorted numbers: "<<"\nnum1: " << num1 << "\nnum2: " << num2 << "\nnum3: " << num3;
}

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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS