C++ #include <iostream> #include <cmath> #include <iomanip> using namespace std;
ID: 3765663 • Letter: C
Question
C++
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int num1;
int num2;
cout << fixed << showpoint << setprecision(2);
cout << "Enter two integers: ";
cin >> num1 >> num2;
cout <<end1;
if (num1 !=0 && num2 !=0)
cout << sqrt (abs(num1 + num2) + 0.5) << end1;
else if (num1 !=0)
cout < floor(num1 + 0.5) << end1;
else if (num2 !=0)
cout << ceil (num2 + 0.5) <<end1;
else
cout << 0 << end1;
return 0;
}
Explanation / Answer
This is how the steps are explained:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int num1;
int num2;
cout << fixed << showpoint << setprecision(2); //Sets the precision to 2 decimal points.
cout << "Enter two integers: "; //Reads 2 integers.
cin >> num1 >> num2;
cout <<endl;
if (num1 !=0 && num2 !=0) //If neither number is 0.
cout << sqrt (abs(num1 + num2) + 0.5) << endl; //Add both the numbers. Add 0.5 to the summation. Then find the squareroot of the result.
else if (num1 !=0) //If the second number is 0.
cout << floor(num1 + 0.5) << endl; //Add 0.5 to the first number. Calculate the floor value of the result.
else if (num2 !=0) //If first number is 0.
cout << ceil (num2 + 0.5) <<endl; //Add 0.5 to the second number. Calculate the ceil value of the result.
else
cout << 0 << endl; //If both the numbers are 0. Display 0.
return 0;
}
So, classifying the inputs into 4 options:
(i) Both inputs are non zeros.
(ii) First number is non-zero and second number is 0.
(iii) First number is 0, and second number is non-zero.
(iv) Both numbers are 0.
Now apply the inputs:
a. num1: -23 num2: 8
Add both the numbers (-15).
Add 0.5 to the absolute value. (15.5)
Calculate squareroot of 15.5. (3.94).
b. num1: 12 num2: 32
Add both the numbers (44).
Add 0.5 to the absolute value. (44.5)
Calculate squareroot of 15.5. (6.67).
c. num1: 15 num2: 0
Calculate the floor of num1 + 0.5 (Floor(15.5)) (15.00)
d. num1: 0 num2: -36
Calculate the ceil of num2 + 0.5 (Ceil(-35.5)) (-35.00)
e. num1: 0 num2: 0
Just displays 0.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.