|
CSIT 121 - Exercises |
Solution:
#include <iostream>
using namespace std;
int main()
{
float a, b;
cout << "Enter two real numbers: ";
cin >> a >> b;
if ( a < b )
cout << "The minimum is: " << a << endl;
else // means a >= b
cout << "The minimum is: " << b << endl;
return 0;
}
|
Solution:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float x, y;
cout << "Enter a real number: ";
cin >> x;
if ( x < 0 )
y = fabs(x);
else
y = log(fabs(x+1));
cout << "f("<< x <<") = " << y << endl;
return 0;
}
|
· It pays $6 per hour for the first 20 hours in the week.
· It pays $8.5 per hour for every additional hour.
Write a C++ program to compute the salaries of employees at that restaurant. The input to the program is the number of hours worked and the output is the salary of the employee. The input should be a nonnegative integer.
Solution:
#include <iostream>
using namespace std;
int main()
{
float hours, salary;
cout << "Enter the number of hours: ";
cin >> hours;
if ( hours <= 20 )
salary = 6 * hours;
else
salary = 6 * 20 + ( hours - 20) * 8.5;
cout << "The salary is: " << salary << endl;
return 0;
}
|
Solution:
#include <iostream>
using namespace std;
int main()
{
float a, b, c, min1 , min2;
cout << "Enter three real numbers: ";
cin >> a >> b >> c;
// First compare a and b.
if ( a < b )
min1 = a;
else
min1 = b;
// Now compare c with the minimum of a and b.
// Note that min1 is the minimum of a and b.
// min2 is the minimum of a, b, and c.
if ( min1 < c )
min2 = min1;
else
min2 = c;
cout << "The minimum is: " << min2 << endl;
return 0;
}
|
Solution:
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number: ";
cin >> number;
if ( (number >= 0) && (number <= 9) )
cout << "One Digit" << endl;
else if ( (number >=10) && (number <= 99) )
cout << "Two Digits" << endl;
else if ( (number >=100) && (number <= 999) )
cout << "Three Digits" << endl;
else if ( (number >= 1000) && (number <= 9999) )
cout << "Four Digits" << endl;
else
cout << "Out of Range" << endl;
return 0;
}
|
Solution:
#include <iostream>
using namespace std;
int main()
{
float Grade;
cout << "Enter a grade: ";
cin >> Grade;
if (Grade <= 59)
cout << "E" << endl;
else if (Grade >= 60 && Grade <= 69)
cout << "D" << endl;
else if (Grade >= 70 && Grade <= 79)
cout << "C" << endl;
else if (Grade >= 80 && Grade <= 89)
cout << "B" << endl;
else if (Grade >= 90 && Grade <= 100)
cout << "A" << endl;
else
cout << "Invalid Grade" << endl;
return 0;
}
|