Selected-Programming-Exercises-Ch4
Selected-Programming-Exercises-Ch4
ExProg1: In a right triangle, the square of the length of one side is equal
to the sum of the squares of the lengths of the other two sides. Write a
program that prompts the user to enter the lengths of three sides of a
triangle and then outputs a message indicating whether the triangle is a
right triangle.
Solution:
#include <iostream>
#include <iomanip>
int main()
{
double side1, side2, side3;
return 0;
}
Sample Run:
#include <iostream>
#include <iomanip>
int main()
{
int num1, num2;
char opr;
cout << num1 << " " << opr << " " << num2 << " = ";
switch (opr)
{
case '+':
cout << num1 + num2 << endl;
break;
case '-':
cout << num1 - num2 << endl;
break;
case '*':
cout << num1 * num2 << endl;
break;
case '/':
if (num2 != 0)
cout << num1 / num2 << endl;
else
cout << "ERROR \nCannot divide by zero" << endl;
break;
default:
cout << "Illegal operation" << endl;
}
return 0;
}
Sample Run:
2 + 3 = 5
----
6 - 9 = -3
*********************************************
Solution:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double length;
double width;
double leftMargin;
double rightMargin;
double topMargin;
double bottomMargin;
int charPointSize;
char lineSpacing;
double lengthOfALine;
int numberOfCharactersInALine = 0;
int numberOfLines = 0;
cout << "The number of lines that can be printed: " << numberOfLines <<
endl;
cout << "The number of characters that can be printed in a line: "
<< numberOfCharactersInALine << endl;
cin.ignore();
cin.get();
return 0;
}
Sample Run: