C++: Write a program whose inputs are three integers, and whose output is both t
ID: 3843286 • Letter: C
Question
C++:
Write a program whose inputs are three integers, and whose output is both the largest of the three values and the smallest of the three values. If the input is 7 15 3, the output is: largest 15 smallest 3 Your program must define and call two functions int LargestNumber (int num1 int num2, int num3) and int smallest Number (int numl, int num2, int num3). The function LargestNumber should return the largest number of the three input values. The function SmallestNumber should return the smallest number of the three input values.Explanation / Answer
// C++ code 1
#include <iostream>
using namespace std;
int LargestNumber(int num1, int num2, int num3)
{
if(num1 > num3 && num1 > num2)
return num1;
else if(num2 > num3 && num2 > num1)
return num2;
else
return num3;
}
int SmallestNumber(int num1, int num2, int num3)
{
if(num1 < num3 && num1 < num2)
return num1;
else if(num2 < num3 && num2 < num1)
return num2;
else
return num3;
}
int main()
{
int num1, num2, num3;
cout << "Enter num1: ";
cin >> num1;
cout << "Enter num2: ";
cin >> num2;
cout << "Enter num3: ";
cin >> num3;
cout << "Largest: " << LargestNumber(num1, num2, num3) << endl;
cout << "Smallest: " << SmallestNumber(num1, num2, num3) << endl;
return 0;
}
/*
output:
Enter num1: 7
Enter num2: 15
Enter num3: 3
Largest: 15
Smallest: 3
*/
// C++ code 2
#include <iostream>
using namespace std;
string IntegerToBinary(int num1)
{
string s = "";
while(num1!=0)
{
int t = num1%2;
s = s + to_string(t);
num1 = num1/2;
}
return s;
}
string ReverseString(string userString)
{
int i=0;
int j= userString.size()-1;
while(i<j)
{
char temp=userString[i];
userString[i]=userString[j];
userString[j]=temp;
i++;
j--;
}
return userString;
}
int main()
{
int num1;
cout << "Enter num1: ";
cin >> num1;
cout << "Binary = " << ReverseString(IntegerToBinary(num1)) << endl;
return 0;
}
/*
output:
Enter num1: 6
Binary = 110
*/
// C++ code 3
#include <iostream>
using namespace std;
void SwapValues(int& userVal1, int& userVal2)
{
int t = userVal2;
userVal2 = userVal1;
userVal1 = t;
}
int main()
{
int userVal1, userVal2;
cout << "Enter userVal1: ";
cin >> userVal1;
cout << "Enter userVal2: ";
cin >> userVal2;
cout << "userVal1 = " << userVal1 << " and userVal2 = " << userVal2 << endl;
cout << "Swapping ";
SwapValues(userVal1, userVal2);
cout << "userVal1 = " << userVal1 << " and userVal2 = " << userVal2 << endl;
return 0;
}
/*
output:
Enter userVal1: 3
Enter userVal2: 8
userVal1 = 3 and userVal2 = 8
Swapping
userVal1 = 8 and userVal2 = 3
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.