Write a program that reads in three integers and determines and prints the small
ID: 3557444 • Letter: W
Question
Write a program that reads in three integers and determines and prints the smallest number numerically in the group. The values are NOT necessarily entered in numeric order. The program must prompt the user and read the three values from the console. It should then print the message "The smallest value among , , and is " (where , , , and are replaced by the actual values.). Use only techniques covered in Chapters 1, 2, 3, and 4. You may use the comparison operators "==", "!=", ">", ">=", "<", and "<=", and also the and ("&&") and or ("||") logical operators. You may also use "else" and nesting. The most direct solution to this assignment uses exactly 3 "if" statements, and no nesting or else statements. However, there are many ways to solve it.
Explanation / Answer
#include <iostream> //cout
using namespace std;
//function prototype
int Min3(int, int, int);
int main()
{
//variable declaration
int val1,val2,val3;
//get input from console
cout<<"First Int:";
cin>>val1;
cout<<" Second Int:";
cin>>val2;
cout<<" Third Int:";
cin>>val3;
cout<<endl;
//Display result
cout<<" The smallest value among "<<val1<<" , "<<val2<<" and "<<val3<<" is "<<Min3(val1,val2,val3)<<endl;
return 0;
}
//Min3: return the smallest integers from 3 integers
int Min3(int v1, int v2, int v3)
{
if (v1 <= v2 && v1 <= v3)
return v1;
if (v2 <= v1 && v2 <= v3)
return v2;
if (v3 <= v1 && v3 <= v2)
return v3;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.