Thursday, 15 November 2018

Getting Started with C++ | Tony Gaddis | Chapter 4 | Problem Solutions | Asad Ali


All the solutions to the problems of the Chapter 2 out of book "Getting Started With C++ by Tony Gaddis"

Problem 1
/*
Minimum/Maximum
Write a program that asks the user to enter two numbers. The program should use the
conditional operator to determine which number is the smaller and which is the larger.
*/
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout << "Enter number one : ";
cin >> num1;
cout << "Enter number two : ";
cin >> num2;
if(num1 > num2)
{
cout << "Number one " << num1 << " is maximum and Number two " << num2 << " is minimum." << endl;
}
else if(num2 > num1)
{
cout << "Number one " << num2 << " is maximum and Number two " << num1 << " is minimum." << endl;
}
else
{
cout << "Number one " << num1 << " is equal to Number two " << num2 << endl;
}
return 0;
}
Problem 2
/*
Roman Numeral Converter
Write a program that asks the user to enter a number within the range of 1 through
10. Use a switch statement to display the Roman numeral version of that number.
Input Validation: Do not accept a number less than 1 or greater than 10.
*/
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter a number : ";
cin >> num;
if(num >= 1 && num <= 10)
{
switch(num)
{
case 1: cout << "1 in roman is I" << endl;
break;
case 2: cout << "2 in roman is II" << endl;
break;
case 3: cout << "3 in roman is III" << endl;
break;
case 4: cout << "4 in roman is IV" << endl;
break;
case 5: cout << "5 in roman is V" << endl;
break;
case 6: cout << "6 in roman is VI" << endl;
break;
case 7: cout << "7 in roman is VII" << endl;
break;
case 8: cout << "8 in roman is VIII" << endl;
break;
case 9: cout << "9 in roman is IX" << endl;
break;
case 10: cout << "10 in roman is X" << endl;
break;
}
}
else
{
cout << "ERROR: Invalid Entry!" << endl;
}
return 0;
}
Problem 3
/*
Magic Dates
The date June 10, 1960 is special because when we write it in the following format,
the month times the day equals the year.
6/10/60
Write a program that asks the user to enter a month (in numeric form), a day, and a
two-digit year. The program should then determine whether the month times the day
is equal to the year. If so, it should display a message saying the date is magic. Otherwise
it should display a message saying the date is not magic.
*/
#include <iostream>
using namespace std;
int main()
{
int day = 0, month = 0, year = 0, check = 0;

cout << "Please Enter the day number : " << endl;
cin >> day;
cout << "Please Enter the month number : " << endl;
cin >> month;
cout << "Please Enter the year number last two digits : " << endl;
cin >> year;

check = day * month;

if(check == year)
{
cout << "The date is magic" << endl;
}
else if (check != year)
{
cout << "The date is not a magic" << endl;
}
else
{
cout << "Ooo! Imposible." << endl;
}

return 0;
}
Problem 4
/*
Areas of Rectangles
The area of a rectangle is the rectangle’s length times its width. Write a program that
asks for the length and width of two rectangles. The program should tell the user
which rectangle has the greater area, or if the areas are the same.
*/
#include <iostream>
using namespace std;
int main()
{
int L1, L2, W1, W2, A1, A2;
cout << "Enter the length of Rectangle One : ";
cin >> L1;
cout << "Enter the Width of Rectangle One : ";
cin >> W1;
cout << "Enter the length of Rectangle two : ";
cin >> L2;
cout << "Enter the Width of Rectangle two : ";
cin >> W2;
A1 = L1 * W1;
A2 = L2 * W2;
if(A1 > A2)
{
cout << "Rectangle One with area " << A1 << " is greater than the Rectangle Two with area " << A2 << endl;
}
else if(A2 > A1)
{
cout << "Rectangle Two with area " << A2 << " is greater than the Rectangle One with area " << A1 << endl;
}
else
{
cout << "ERROR!" << endl;
}
return 0;
}
Problem 5
/*
Body Mass Index
Write a program that calculates and displays a person’s body mass index (BMI). The
BMI is often used to determine whether a person with a sedentary lifestyle is overweight
or underweight for his or her height. A person’s BMI is calculated with the following
formula:
BMI = weight × 703 / height2
where weight is measured in pounds and height is measured in inches. The program
should display a message indicating whether the person has optimal weight, is underweight,
or is overweight. A sedentary person’s weight is considered to be optimal if his
or her BMI is between 18.5 and 25. If the BMI is less than 18.5, the person is considered
to be underweight. If the BMI value is greater than 25, the person is considered
to be overweight.
*/
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int weight, height, BMI;
cout << "Enter your height : ";
cin >> height;
cout << "Enter your weight : ";
cin >> weight;
BMI = weight * 703 / pow(height, 2);
if(BMI < 18.5)
{
cout << "The person is underweight." << endl;
}
else if(BMI > 25)
{
cout << "The person is overweight." << endl;
}
else
{
cout << "The person is optimal weight." << endl;
}
return 0;
}
Problem 6
/*
Mass and Weight
Scientists measure an object’s mass in kilograms and its weight in newtons. If you
know the amount of mass that an object has, you can calculate its weight, in newtons,
with the following formula:
Weight = mass × 9.8
Write a program that asks the user to enter an object’s mass, and then calculates and
displays its weight. If the object weighs more than 1,000 newtons, display a message
indicating that it is too heavy. If the object weighs less than 10 newtons, display a
message indicating that the object is too light.
*/
#include <iostream>
using namespace std;
int main(){
double weight, mass;
cout << "Enter mass of the object in kilogram : ";
cin >> mass;
weight = mass * 9.8;
if(weight > 1000)
{
cout << "The object is too heavy." << endl;
}
else if(weight < 10)
{
cout << "The object is too light." << endl;
}
else
{
cout << "The object weight is optimal." << endl;
}
return 0;
}
Problem 7
/*
Time Calculator
Write a program that asks the user to enter a number of seconds.
• There are 60 seconds in a minute. If the number of seconds entered by the user is
greater than or equal to 60, the program should display the number of minutes in
that many seconds.
• There are 3,600 seconds in an hour. If the number of seconds entered by the user
is greater than or equal to 3,600, the program should display the number of hours
in that many seconds.
• There are 86,400 seconds in a day. If the number of seconds entered by the user is
greater than or equal to 86,400, the program should display the number of days
in that many seconds.
*/
#include <iostream>
using namespace std;
int main(){
float seconds, minuts, hours;
cout << "Enter the number of the seconds : ";
cin >> seconds;

if(seconds >= 60 && seconds < 3600)
{
minuts = seconds / 60;
cout << "The number of minuts is " << minuts << endl;
}
else if(seconds >= 3600)
{
hours = seconds / 3600;
cout << "The nubmer of hours is " << hours << endl;
}
else
{
cout << "Less than a minut." << endl;
}
return 0;
}
Problem 8
coming soon..
Problem 9
coming soon..

Problem 9
coming soon..

Problem 9
coming soon..

Share:

0 comments:

Post a Comment

Thanks for visiting my Blog.

Have any problem, feel free to contact us via Email: asadalipkp9@gmail.com

Have a nice day!

padacode.blogspot.com

Powered by Blogger.