Question #14466

Write a C++ program that declares three variables minNum, midNum and maxNum to receive three integers read from the keyboard. The inputs are unsorted and can be read into the three variables in any order, for example, the first read number in minNum may not be the smallest. However, your program should then sort the three numbers to ensure minNum ≤ midNum ≤ maxNum and display them in this order.

Expert's answer

#include<stdio.h>

#include <conio.h>

#include <math.h>

int main()

{

int minNum, midNum,maxNum;

int arrayofnumbers[3];

int max,min;

printf("Enter the number 1: ");

scanf("%d",&minNum);

printf("Enter the number 2: ");

scanf("%d",&midNum);

printf("Enter the number 3: ");

scanf("%d",&maxNum);

arrayofnumbers[0]=minNum;

arrayofnumbers[1]=midNum;

arrayofnumbers[2]=maxNum;

max=arrayofnumbers[0];

min=arrayofnumbers[0];

for(int i=0;i<3;i++){

if(max<arrayofnumbers[i]){

max=arrayofnumbers[i];

}

}

for(int i=0;i<3;i++){

if(min>arrayofnumbers[i]){

min=arrayofnumbers[i];

}

}

maxNum=max;

minNum=min;

printf("minNum <= midNum <= maxNum:

");

printf("%d <= %d <= %d",minNum,midNum,maxNum);

getch();

return 0;

}

#include <conio.h>

#include <math.h>

int main()

{

int minNum, midNum,maxNum;

int arrayofnumbers[3];

int max,min;

printf("Enter the number 1: ");

scanf("%d",&minNum);

printf("Enter the number 2: ");

scanf("%d",&midNum);

printf("Enter the number 3: ");

scanf("%d",&maxNum);

arrayofnumbers[0]=minNum;

arrayofnumbers[1]=midNum;

arrayofnumbers[2]=maxNum;

max=arrayofnumbers[0];

min=arrayofnumbers[0];

for(int i=0;i<3;i++){

if(max<arrayofnumbers[i]){

max=arrayofnumbers[i];

}

}

for(int i=0;i<3;i++){

if(min>arrayofnumbers[i]){

min=arrayofnumbers[i];

}

}

maxNum=max;

minNum=min;

printf("minNum <= midNum <= maxNum:

");

printf("%d <= %d <= %d",minNum,midNum,maxNum);

getch();

return 0;

}

## Comments

## Leave a comment