4.3k views
3 votes
C++ program: Write a program that prompts the user to input three numbers. The program should then output the numbers in ascending order, separated by a single space.

User Hakunin
by
5.2k points

1 Answer

5 votes

Answer:

// here is code in C++.

#include<iostream>

using namespace std;

int main()

{

// declare variables

double num1, num2, mun3, t;

cout << "Enter three numbers: ";

// read the three numbers

cin >> num1 >> num2 >> mun3;

//sort the three numbers

if( num1>num2 )

{

t = num1;

num1 = num2;

num2 = t;}

if( num2>mun3 ){

t = num2;

num2 = mun3;

mun3 = t;}

if( num1>num2 ){

t = num1;

num1 = num2;

num2 = t;

}

// print the output

cout<<"numbers in ascending order:";

cout << num1 << " " << num2 << " " << mun3 << endl;

return 0 ;

}

Step-by-step explanation:

Declare three variables to store the input. Read the value of all three variables. If first number is greater than second, then with the help of variable "t" swap the value of first and second.after that if second is greater than third number, swap them.In last if first number is greater than second one then swap them. This will sort the three numbers in ascending order.

Output:

Enter three numbers: 3 7 2

numbers in ascending order:2 3 7

User Andrea Di Giorgi
by
4.8k points