## Basic C++ Exercises

Exercises 1 to 36 cover basic C++ concepts that you should be able to complete if you have worked through all of the materials in unit 1. Start with some early problems, then try a few from the middle, and then a few more later from the list. Do not attempt any of the problems after Exercise 36, as we haven't learned these concepts yet. Don't worry, we will come back to them.

### Solutions

##### Exercise 1 Solution

Sample C++ Code:

#include <iostream>
using namespace std;
int main()
{
cout << "\n\n Print a welcome text in a separate line :\n";
cout << "----------------------------------------------\n";
cout << " Welcome to \n" ;
cout << " w3resource.com "<<endl ;

}

Sample Output:

 Print a welcome text in a separate line :
----------------------------------------------
Welcome to
w3resource.com

##### Exercise 2 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
cout << "\n\n Print the sum of two numbers :\n";
cout << "-----------------------------------\n";
cout << " The sum of 29 and 30 is : "<< 29+30 <<"\n\n" ;
}

Sample Output:

Print the sum of two numbers :
-----------------------------------
The sum of 29 and 30 is : 59

##### Exercise 3 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
cout << "\n\n Find Size of fundamental data types :\n";
cout << "------------------------------------------\n";
cout << " The sizeof(char) is :          " << sizeof(char) << " bytes \n" ;
cout << " The sizeof(short) is :         " << sizeof(short) << " bytes \n" ;
cout << " The sizeof(int) is :           " << sizeof(int) << " bytes \n" ;
cout << " The sizeof(long) is :          " << sizeof(long) << " bytes \n" ;
cout << " The sizeof(long long) is :     " << sizeof(long long) << " bytes \n";
cout << " The sizeof(float) is :         " << sizeof(float) << " bytes \n" ;
cout << " The sizeof(double) is :        " << sizeof(double) << " bytes \n";
cout << " The sizeof(long double) is :   " << sizeof(long double) << " bytes \n";
cout << " The sizeof(bool) is :          " << sizeof(bool) << " bytes \n\n";
return 0;
}

Sample Output:

 Find Size of fundamental data types :
------------------------------------------
The sizeof(char) is :          1 bytes
The sizeof(short) is :         2 bytes
The sizeof(int) is :           4 bytes
The sizeof(long) is :          8 bytes
The sizeof(long long) is :     8 bytes
The sizeof(float) is :         4 bytes
The sizeof(double) is :        8 bytes
The sizeof(long double) is :   16 bytes
The sizeof(bool) is :          1 bytes

##### Exercise 4 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
cout << "\n\n Print the sum of two numbers :\n";
cout << "-----------------------------------\n";
int a;
int b;
int sum;
a=29;
b=30;
sum=a+b;
cout << " The sum of "<< a << " and "<<b <<" is : "<< sum <<"\n\n" ;
}

Sample Output:

 Print the sum of two numbers :
-----------------------------------
The sum of 29 and 30 is : 59

##### Exercise 5 Solution

Sample C++ Code:

#include <iostream>
#include <climits>   // integer limits in header file
using namespace std;

int main()
{
cout << "\n\n Check the upper and lower limits of integer :\n";
cout << "--------------------------------------------------\n";
cout << " The maximum limit of int data type :                  " << INT_MAX << endl;
cout << " The minimum limit of int data type :                  " << INT_MIN << endl;
cout << " The maximum limit of unsigned int data type :         " << UINT_MAX << endl;
cout << " The maximum limit of long long data type :            " << LLONG_MAX << endl;
cout << " The minimum limit of long long data type :             " << LLONG_MIN << endl;
cout << " The maximum limit of unsigned long long data type :   " << ULLONG_MAX << endl;
cout << " The Bits contain in char data type :                  " << CHAR_BIT << endl;
cout << " The maximum limit of char data type :                 " << CHAR_MAX << endl;
cout << " The minimum limit of char data type :                 " << CHAR_MIN << endl;
cout << " The maximum limit of signed char data type :          " << SCHAR_MAX << endl;
cout << " The minimum limit of signed char data type :          " << SCHAR_MIN << endl;
cout << " The maximum limit of unsigned char data type :        " << UCHAR_MAX << endl;
cout << " The minimum limit of short data type :                " << SHRT_MIN << endl;
cout << " The maximum limit of short data type :                " << SHRT_MAX << endl;
cout << " The maximum limit of unsigned short data type :       " << USHRT_MAX << endl;
cout << endl;
return 0;
}

Sample Output:

Check the upper and lower limits of integer :
--------------------------------------------------
The maximum limit of int data type :                  2147483647
The minimum limit of int data type :                  -2147483648
The maximum limit of unsigned int data type :         4294967295
The maximum limit of long long data type :            9223372036854775807
The minimum limit of long long data type :             -9223372036854775808
The maximum limit of unsigned long long data type :   18446744073709551615
The Bits contain in char data type :                  8
The maximum limit of char data type :                 127
The minimum limit of char data type :                 -128
The maximum limit of signed char data type :          127
The minimum limit of signed char data type :          -128
The maximum limit of unsigned char data type :        255
The minimum limit of short data type :                -32768
The maximum limit of short data type :                32767
The maximum limit of unsigned short data type :       65535

##### Exercise 6 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
cout << "\n\n Check whether the primitive values crossing the limits or not :\n";
cout << "--------------------------------------------------------------------\n";
char gender = 'F';             // char is single-quoted
bool isEmployed = true;         // true(non-zero) or false(0)
unsigned short numOfsons = 2; // [0, 255]
short yearOfAppt = 2009;      // [-32767, 32768]
unsigned int YearlyPackage = 1500000;   // [0, 4294967295]
double height = 79.48;       // With fractional part
float gpa = 4.69f;           // Need suffix 'f' for float
long totalDrawan = 12047235L;     // Suffix 'L' for long
long long balance = 995324987LL;  // Need suffix 'LL' for long long int

cout << " The Gender is : " << gender << endl;
cout << " Is she married? : " << isEmployed << endl;
cout << " Number of sons she has : " << numOfsons << endl;
cout << " Year of her appointment : " << yearOfAppt << endl;
cout << " Salary for a year : " << YearlyPackage << endl;
cout << " Height is : " << height << endl;
cout << " GPA is " << gpa << endl;
cout << " Salary drawn upto : " << totalDrawan << endl;
cout << " Balance till : " << balance << endl;
return 0;
}

Sample Output:

 Check whether the primitive values crossing the limits or not :
--------------------------------------------------------------------
The Gender is : F
Is she married? : 1
Number of sons she has : 2
Year of her appointment : 2009
Salary for a year : 1500000
Height is : 79.48
GPA is 4.69
Salary drawn upto : 12047235
Balance till : 995324987

##### Exercise 7 Solution

Sample C++ Code:

#include <iostream>
#include <iomanip>   // formatting floating-point numbers with 1 decimal place
using namespace std;

int main()
{
int m1 = 5, m2 = 7;
double d1 = 3.7, d2 = 8.0;

cout << "\n\n Display arithmetic operations with mixed data type :\n";
cout << "---------------------------------------------------------\n";
cout << fixed << setprecision(1);

cout <<" "<< m1 << " + " << m2 << " = " << m1+m2 << endl;
cout <<" "<< d1 << " + " << d2 << " = " << d1+d2 << endl;
cout <<" "<< m1 << " + " << d2 << " = " << m1+d2 << endl;

cout <<" "<< m1 << " - " << m2 << " = " << m1-m2 << endl;
cout <<" "<< d1 << " - " << d2 << " = " << d1-d2 << endl;
cout <<" "<< m1 << " - " << d2 << " = " << m1-d2 << endl;

cout <<" "<< m1 << " * " << m2 << " = " << m1*m2 << endl;
cout <<" "<< d1 << " * " << d2 << " = " << d1*d2 << endl;
cout <<" "<< m1 << " * " << d2 << " = " << m1*d2 << endl;

cout <<" "<< m1 << " / " << m2 << " = " << m1/m2 << endl;
cout <<" "<< d1 << " / " << d2 << " = " << d1/d2 << endl;
cout <<" "<< m1 << " / " << d2 << " = " << m1/d2 << endl;
cout << endl;
return 0;
}

Sample Output:

 Display arithmetic operations with mixed data type :
---------------------------------------------------------
5 + 7 = 12
3.7 + 8.0 = 11.7
5 + 8.0 = 13.0
5 - 7 = -2
3.7 - 8.0 = -4.3
5 - 8.0 = -3.0
5 * 7 = 35
3.7 * 8.0 = 29.6
5 * 8.0 = 40.0
5 / 7 = 0
3.7 / 8.0 = 0.5
5 / 8.0 = 0.6

##### Exercise 8 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
cout << "\n\n Check overflow/underflow during various arithmetical operation :\n";
cout << " Range of int is [-2147483648, 2147483647]"<< endl;
cout << "---------------------------------------------------------------------\n";
int n1 = 2147483647;      // maximum range of integer
cout <<" Overflow the integer range and set in minimum range : " << n1 + 1 << endl;
cout <<" Increasing from its minimum range : " << n1 + 2 << endl;
cout <<" Product is :" << n1 * n1 << endl;

int n2 = -2147483648;     // minimum range of integer
cout <<" Underflow the range and set in maximum range : " << n2 - 1 << endl;
cout <<" Decreasing from its maximum range : " << n2 - 2 << endl;
cout <<" Product is : " << n2 * n2 << endl;
cout << endl;
return 0;
}

Sample Output:

 Check overflow/underflow during various arithmetical operation :
Range of int is [-2147483648, 2147483647]
---------------------------------------------------------------------
Overflow the integer range and set in minimum range : -2147483648
Increasing from its minimum range : -2147483647
Product is :1
Underflow the range and set in maximum range : 2147483647
Decreasing from its maximum range : 2147483646
Product is : 0


##### Exercise 9 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
int num = 57;
cout << "\n\n Display the operation of pre and post increment and decrement :\n";
cout << "--------------------------------------------------------------------\n";
cout <<" The number is : " << num << endl;
num++;               // increase by 1 (post-increment)
cout <<" After post increment by 1 the number is : " << num << endl;
++num;               // increase by 1 (pre-increment)
cout <<" After pre increment by 1 the number is : " << num << endl;
num = num + 1;  // num is now increased by 1.
cout <<" After increasing by 1 the number is : " << num << endl; // 79
num--;               // decrease by 1 (post-decrement)
cout <<" After post decrement by 1 the number is : " << num << endl;
--num;               // decrease by 1 (pre-decrement)
cout <<" After pre decrement by 1 the number is : " << num << endl;
num = num - 1; // num is now decreased by 1.
cout <<" After decreasing by 1 the number is : " << num << endl;
cout << endl;
return 0;
}

Sample Output:

 Display the operation of pre and post increment and decrement :
--------------------------------------------------------------------
The number is : 57
After post increment by 1 the number is : 58
After pre increment by 1 the number is : 59
After increasing by 1 the number is : 60
After post decrement by 1 the number is : 59
After pre decrement by 1 the number is : 58
After decreasing by 1 the number is : 57 

##### Exercise 10 Solution

Sample C++ Code:

#include <iostream>
#include <iomanip>    // Needed to do formatted I/O
using namespace std;

int main()
{
cout << "\n\n Formatting the output :\n";
cout << "----------------------------\n";

double pi = 3.14159265; // this is floating point number
cout << fixed << setprecision(4); // number is set to display with 4 decimal places
cout <<" The value of pi : " << pi << endl;
cout << " The value of pi 4 decimal place of total width 8   : |" << setw(8) << pi << "|" << endl; // setw() sets the total width
cout << " The value of pi 4 decimal place of total width 10  : |" << setw(10) << pi << "|"<< endl;

cout << setfill('-'); // setfill() sets to fill the blanks with specified character
cout << " The value of pi 4 decimal place of total width 8   : |" << setw(8) << pi << "|" << endl;
cout << " The value of pi 4 decimal place of total width 10  : |" << setw(10) << pi << "|"<< endl;

cout << scientific;  // set value in scientific format with exponent
cout <<" The value of pi in scientific format is : " << pi << endl;

bool done = false; // this is boolean variable
cout <<" Status in number : " << done << endl;
cout << boolalpha;     // set output in alphabet true or false
cout <<" Status in alphabet : " << done << endl;
cout << endl;
return 0;
}

Sample Output:

 Formatting the output :
----------------------------
The value of pi : 3.1416
The value of pi 4 decimal place of total width 8   : |  3.1416|
The value of pi 4 decimal place of total width 10  : |    3.1416|
The value of pi 4 decimal place of total width 8   : |--3.1416|
The value of pi 4 decimal place of total width 10  : |----3.1416|
The value of pi in scientific format is : 3.1416e+00
Status in mumber : 0
Status in alphabet : false 

##### Exercise 11 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
cout << "\n\n Print the result of some specific operation :\n";
cout << "--------------------------------------------------\n";
cout << " Result of 1st expression is : "<< (-1+4*6) <<"\n" ; //-1 + 24 = 23
cout << " Result of 2nd expression is : "<< ((35+5)%7) <<"\n" ; //40 % 7 = 5 (remainder of 40/7)
cout << " Result of 3rd expression is : "<< (14+-4*6/11) <<"\n" ;  //14 - (24/11)= 14 - 2 = 12
cout << " Result of 4th expression is : "<< (2+15/6*1-7%2) <<"\n\n" ; //2 + (15/6) - remainder of (7/2) = 2 + 2 - 1 = 4 - 1 = 3

}

Sample Output:

 Print the result of some specific operation :
--------------------------------------------------
Result of 1st expression is : 23
Result of 2nd expression is : 5
Result of 3rd expression is : 12
Result of 4th expression is : 3 

##### Exercise 12 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
int num1, num2, sum;
cout << "\n Sum of two numbers :\n";
cout << "-------------------------\n";
cout << " Input 1st number : ";
cin >> num1 ;
cout << " Input 2nd number : ";
cin >> num2;
sum = num1 + num2;
cout <<" The sum of the numbers is : " << sum << endl;
cout << endl;
return 0;
}

Sample Output:

 Sum of two numbers :
-------------------------
Input 1st number : 25
Input 2nd number : 39
The sum of the numbers is : 64

##### Exercise 13 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
cout << "\n\n Swap two numbers :\n";
cout << "-----------------------\n";
int num1, num2, temp;
cout << " Input 1st number : ";
cin >> num1 ;
cout << " Input 2nd number : ";
cin >> num2;
temp=num2;
num2=num1;
num1=temp;
cout << " After swapping the 1st number is : "<< num1 <<"\n" ;
cout << " After swapping the 2nd number is : "<< num2 <<"\n\n" ;
}

Sample Output:

 Swap two numbers :
-----------------------
Input 1st number : 25
Input 2nd number : 39
After swapping the 1st number is : 39
After swapping the 2nd number is : 25

##### Exercise 14 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
float volsp;
cout << "\n\n Calculate the volume of a sphere :\n";
cout << "---------------------------------------\n";
cout<<" Input the radius of a sphere : ";
cout<<" The volume of a sphere is : "<< volsp << endl;
cout << endl;
return 0;
}

Sample Output:

 Calculate the volume of a sphere :
---------------------------------------
Input the radius of a sphere : 6
The volume of a sphere is : 904.32

##### Exercise 15 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
int sid1;
float volcu;
cout << "\n\n Calculate the volume of a cube :\n";
cout << "---------------------------------------\n";
cout<<" Input the side of a cube : ";
cin>>sid1;
volcu=(sid1*sid1*sid1);
cout<<" The volume of a cube is : "<< volcu << endl;
cout << endl;
return 0;
}

Sample Output:

 Calculate the volume of a cube :
---------------------------------------
Input the side of a cube : 5
The volume of a cube is : 125

##### Exercise 16 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
float volcy;
cout << "\n\n Calculate the volume of a cylinder :\n";
cout << "-----------------------------------------\n";
cout<<" Input the radius of the cylinder : ";
cout<<" Input the height of the cylinder : ";
cin>>hgt;
cout<<" The volume of a cylinder is : "<< volcy << endl;
cout << endl;
return 0;
}

Sample Output:

 Calculate the volume of a cylinder :
-----------------------------------------
Input the radius of the cylinder : 6
Input the height of the cylinder : 8
The volume of a cylinder is : 904.32


##### Exercise 17 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
int width, lngth, area, peri;
cout << "\n\n Find the Area and Perimeter of a Rectangle :\n";
cout << "-------------------------------------------------\n";
cout<<" Input the length of the rectangle : ";
cin>>lngth;
cout<<" Input the width of the rectangle : ";
cin>>width;
area=(lngth*width);
peri=2*(lngth+width);
cout<<" The area of the rectangle is : "<< area << endl;
cout<<" The perimeter of the rectangle is : "<< peri << endl;
cout << endl;
return 0;
}

Sample Output:

 Find the Area and Perimeter of a Rectangle :
-------------------------------------------------
Input the length of the rectangle : 10
Input the width of the rectangle : 15
The area of the rectangle is : 150
The perimeter of the rectangle is : 50 

##### Exercise 18 Solution

Sample C++ Code:

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float side1, side2, side3, area, s;
cout << "\n\n Find the area of any triangle using Heron's Formula :\n";
cout << "----------------------------------------------------------\n";
cout<<" Input the length of 1st side  of the triangle : ";
cin>>side1;
cout<<" Input the length of 2nd side  of the triangle : ";
cin>>side2;
cout<<" Input the length of 3rd side  of the triangle : ";
cin>>side3;
s = (side1+side2+side3)/2;
area = sqrt(s*(s-side1)*(s-side2)*(s-side3));
cout<<" The area of the triangle is : "<< area << endl;
cout << endl;
return 0;
}

Sample Output:

 Find the area of any triangle using Heron's Formula :
----------------------------------------------------------
Input the length of 1st side  of the triangle : 5
Input the length of 2nd side  of the triangle : 5
Input the length of 3rd side  of the triangle : 5
The area of the triangle is : 10.8253

##### Exercise 19 Solution

Sample C++ Code:

#include <iostream>
#define PI 3.14159
using namespace std;

int main()
{
cout << "\n\n Find the area and circumference of any circle :\n";
cout << "----------------------------------------------------\n";
cout<<" Input the radius(1/2 of diameter) of a circle : ";

cout<<" The area of the circle is : "<< area << endl;
cout<<" The circumference of the circle is : "<< circum << endl;

cout << endl;
return 0;
}

Sample Output:

 Find the area and circumference of any circle :
----------------------------------------------------
Input the radius(1/2 of diameter) of a circle : 5
The area of the circle is : 78.5397
The circumference of the circle is : 31.4159 

##### Exercise 20 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
float frh, cel;
cout << "\n\n Convert temperature in Celsius to Fahrenheit :\n";
cout << "---------------------------------------------------\n";
cout << " Input the temperature in Celsius : ";
cin >> cel;
frh = (cel * 9.0) / 5.0 + 32;
cout << " The temperature in Celsius    : " << cel << endl;
cout << " The temperature in Fahrenheit : " << frh << endl;
cout << endl;
return 0;
}

Sample Output:

 Convert temperature in Celsius to Fahrenheit :
---------------------------------------------------
Input the temperature in Celsius : 35
The temperature in Celsius    : 35
The temperature in Fahrenheit : 95

##### Exercise 21 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
float frh, cel;
cout << "\n\n Convert temperature in Fahrenheit to Celsius :\n";
cout << "---------------------------------------------------\n";
cout << " Input the temperature in Fahrenheit : ";
cin >> frh;
cel = ((frh * 5.0)-(5.0 * 32))/9;
cout << " The temperature in Fahrenheit : " << frh << endl;
cout << " The temperature in Celsius : " << cel << endl;
cout << endl;
return 0;
}

Sample Output:

 Convert temperature in Fahrenheit to Celsius :
---------------------------------------------------
Input the temperature in Fahrenheit : 95
The temperature in Fahrenheit : 95
The temperature in Celsius : 35

##### Exercise 22 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
float ang1, ang2, ang3;
cout << "\n\n Find the third angle of a triangle :\n";
cout << "-----------------------------------------\n";
cout<<" Input the 1st angle of the triangle : ";
cin>>ang1;
cout<<" Input the 2nd angle of the triangle : ";
cin>>ang2;
ang3=180-(ang1+ang2);
cout << " The 3rd of the triangle is : " << ang3 << endl;
cout << endl;
return 0;
}	

Sample Output:

 Find the third angle of a triangle :
-----------------------------------------
Input the 1st angle of the triangle : 30
Input the 2nd angle of the triangle : 60


##### Exercise 23 Solution

Sample C++ Code :

#include <iostream>
using namespace std;

int main()
{
float kmph, miph;
cout << "\n\n Convert kilometers per hour to miles per hour :\n";
cout << "----------------------------------------------------\n";
cout << " Input the distance in kilometer : ";
cin >> kmph;
miph = (kmph * 0.6213712);
cout << " The "<< kmph <<" Km./hr. means "<< miph << " Miles/hr." << endl;
cout << endl;
return 0;
}	

Sample Output:

 Convert kilometers per hour to miles per hour :
----------------------------------------------------
Input the distance in kilometer : 25
The 25 Km./hr. means 15.5343 Miles/hr.

##### Exercise 24 Solutions

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
float kel, frh;
cout << "\n\n Convert temperature in Kelvin to Fahrenheit  :\n";
cout << "---------------------------------------------------\n";
cout << " Input the temperature in Kelvin : ";
cin >> kel;
frh = (9.0 / 5) * (kel - 273.15) + 32;
cout << " The temperature in Kelvin    : " << kel << endl;
cout << " The temperature in Fahrenheit : " << frh << endl;
cout << endl;
return 0;
}

Sample Output:

 Convert temperature in Kelvin to Fahrenheit  :
---------------------------------------------------
Input the temperature in Kelvin : 300
The temperature in Kelvin    : 300
The temperature in Fahrenheit : 80.33

##### Exercise 25 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
float kel, cel;
cout << "\n\n Convert temperature in Kelvin to Celsius  :\n";
cout << "------------------------------------------------\n";
cout << " Input the temperature in Kelvin : ";
cin >> kel;
cel = kel - 273.15;
cout << " The temperature in Kelvin    : " << kel << endl;
cout << " The temperature in Celsius : " << cel << endl;
cout << endl;
return 0;
}

Sample Output:

 Convert temperature in Kelvin to Celsius  :
------------------------------------------------
Input the temperature in Kelvin : 300
The temperature in Kelvin    : 300
The temperature in Celsius : 26.85 

##### Exercise 26 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
float kel, frh;
cout << "\n\n Convert temperature in Fahrenheit to Kelvin  :\n";
cout << "---------------------------------------------------\n";
cout << " Input the temperature in Fahrenheit : ";
cin >> frh;
kel = (5.0 / 9) * (frh - 32) + 273.15;
cout << " The temperature in Fahrenheit : " << frh << endl;
cout << " The temperature in Kelvin : " << kel << endl;
cout << endl;
return 0;
}

Sample Output:

 Convert temperature in Kelvin to Celsius  :
------------------------------------------------
Input the temperature in Kelvin : 300
The temperature in Kelvin    : 300
The temperature in Celsius : 26.85

##### Exercise 27 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
float kel, cel;
cout << "\n\n Convert temperature in Celsius to Kelvin  :\n";
cout << "---------------------------------------------------\n";
cout << " Input the temperature in Celsius : ";
cin >> cel;
kel = cel + 273.15;
cout << " The temperature in Celsius : " << cel << endl;
cout << " The temperature in Kelvin : " << kel << endl;
cout << endl;
return 0;
}

Sample Output:

 Convert temperature in Celsius to Kelvin  :
---------------------------------------------------
Input the temperature in Celsius : 26.85
The temperature in Celsius : 26.85
The temperature in Kelvin : 300  

##### Exercise 28 Solution

Sample C++ Code:

#include <iostream>
#include <math.h>
#define PI 3.14159
using namespace std;

int main()
{
float side1, side2, ang1, area;
cout << "\n\n Find the area of Scalene Triangle :\n";
cout << "----------------------------------------\n";
cout<<" Input the length of a side  of the triangle : ";
cin>>side1;
cout<<" Input the length of another side  of the triangle : ";
cin>>side2;
cout<<" Input the angle between these sides of the triangle : ";
cin>>ang1;
area = (side1 * side2 * sin((PI/180)*ang1))/2;
cout<<" The area of the Scalene Triangle is : "<< area << endl;
cout << endl;
return 0;
}

Sample Output:

 Find the area of Scalene Triangle :
----------------------------------------
Input the length of a side  of the triangle : 5
Input the length of another side  of the triangle : 6
Input the angle between these sides of the triangle : 6
The area of the Scalene Triangle is : 1.56793 

##### Exercise 29 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
int dividend, divisor, quotient,remainder;
cout << "\n\n Compute quotient and remainder :\n";
cout << "-------------------------------------\n";
cout<<" Input the dividend : ";
cin>>dividend;
cout<<" Input the divisor : ";
cin>>divisor;
quotient=dividend / divisor;
remainder=dividend % divisor;
cout<<" The quotient of the division is : "<< quotient << endl;
cout<<" The remainder of the division is : "<< remainder << endl;
cout << endl;
return 0;
}

Sample Output:

 Compute quotient and remainder :
-------------------------------------
Input the dividend : 25
Input the divisor : 3
The quotient of the division is : 8
The remainder of the division is : 1 

##### Exercise 30 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
float n1,n2,n3,n4,tot,avrg;
cout << "\n\n Compute the total and average of four numbers :\n";
cout << "----------------------------------------------------\n";
cout<<" Input 1st two numbers (separated by space) : ";
cin>> n1 >> n2;
cout<<" Input last two numbers (separated by space) : ";
cin>> n3 >> n4;
tot=n1+n2+n3+n4;
avrg=tot/4;
cout<<" The total of four numbers is : "<< tot << endl;
cout<<" The average of four numbers is : "<< avrg << endl;
cout << endl;
return 0;
}

Sample Output:

 Compute the total and average of four numbers :
----------------------------------------------------
Input 1st two numbers (separated by space) : 25 20
Input last two numbers (separated by space) : 15 25
The total of four numbers is : 85
The average of four numbers is : 21.25  

##### Exercise 31 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
int x;
cout << "\n\n Make a rectangular shape by a single digit number :\n";
cout << "--------------------------------------------------------\n";
cout<<" Input the number : ";
cin>> x;
cout<<" "<<x<<x<<x<<x<<endl;
cout<<" "<<x<<" "<<" "<<x<<endl;
cout<<" "<<x<<" "<<" "<<x<<endl;
cout<<" "<<x<<" "<<" "<<x<<endl;
cout<<" "<<x<<" "<<" "<<x<<endl;
cout<<" "<<x<<x<<x<<x<<endl;
cout << endl;
return 0;
}

Sample Output:

 Make a rectangular shape by a single digit number :
--------------------------------------------------------
Input the number : 5
5555
5  5
5  5
5  5
5  5
5555     

##### Exercise 32 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
signed long num1 = 0;
cout << "\n\n Check whether a number is positive, negative or zero :\n";
cout << "-----------------------------------------------------------\n";
cout << " Input a number : ";
cin >> num1;
if(num1 > 0)
{
cout << " The entered number is positive.\n\n";
}
else if(num1 < 0)
{
cout << " The entered number is negative.\n\n";
}
else
{
std::cout << " The number is zero.\n\n";
}
return 0;
}

Sample Output:

 Check whether a number is positive, negative or zero :
-----------------------------------------------------------
Input a number : 8
The entered number is positive.

##### Exercise 33 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
cout << "\n\n Divide two numbers and print:\n";
cout << "----------------------------------\n";
int a;
int b;
int resdiv;
a=30;
b=10;
resdiv=a/b;
cout << " The quotient of "<< a << " and "<<b <<" is : "<< resdiv <<"\n\n" ;
}

Sample Output:

Divide two numbers and print:
----------------------------------
The quotient of 30 and 10 is : 3

##### Exercise 34 Solution

Sample C++ Code:

#include<iostream>
#include<cmath>
#include <ctime>
using namespace std;

int main()
{

time_t t = time(NULL);
tm* tPtr = localtime(&t);
cout << "\n\n Display the Current Date and Time :\n";
cout << "----------------------------------------\n";
cout << " seconds = " << (tPtr->tm_sec) << endl;
cout << " minutes = " << (tPtr->tm_min) << endl;
cout << " hours = " << (tPtr->tm_hour) << endl;
cout << " day of month = " << (tPtr->tm_mday) << endl;
cout << " month of year = " << (tPtr->tm_mon)+1 << endl;
cout << " year = " << (tPtr->tm_year)+1900 << endl;
cout << " weekday = " << (tPtr->tm_wday )<< endl;
cout << " day of year = " << (tPtr->tm_yday )<< endl;
cout << " daylight savings = " <<(tPtr->tm_isdst )<< endl;
cout << endl;
cout << endl;

cout << " Current Date: " <<(tPtr->tm_mday)<<"/"<< (tPtr->tm_mon)+1 <<"/"<< (tPtr->tm_year)+1900<< endl;
cout << " Current Time: " << (tPtr->tm_hour)<<":"<< (tPtr->tm_min)<<":"<< (tPtr->tm_sec) << endl;
cout << endl;
return 0;
}

Sample Output:

 Display the Current Date and Time :
----------------------------------------
seconds = 57
minutes = 33
hours = 12
day of month = 6
month of year = 7
year = 2017
weekday = 4
day of year = 186
daylight savings = 0

Current Date: 6/7/2017
Current Time: 12:33:57  

##### Exercise 35 Solution

Sample C++ Code:

#include <iostream>
using namespace std;

int main()
{
cout << "\n\n Compute the specified expressions and print the output:\n";
cout << "------------------------------------------------------------\n";
cout << " Result of the expression "<<"(25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5) is : "<< (25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5) <<"\n" ;
}

Sample Output:

 Compute the specified expressions and print the output:
------------------------------------------------------------
Result of the expression (25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5) is : 2.13889 

##### Exercise 36 Solution

Sample C++ Code:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

cout << "\n\n Formatting the output using type casting:\n";
cout << "----------------------------------------------\n";
cout<<"Print floating-point number in fixed format with 1 decimal place: ";
cout << fixed << setprecision(1);
cout<<"\nTest explicit type casting :\n";
int i1 = 4, i2 = 8;
cout << i1 / i2 << endl;
cout << (double)i1 / i2 << endl;
cout << i1 / (double)i2 << endl;
cout << (double)(i1 / i2) << endl;

double d1 = 5.5, d2 = 6.6;
cout<<"\nTest implicit type casting :\n" ;
cout << (int)d1 / i2 << endl;
cout << (int)(d1 / i2) << endl;
cout <<"\nint implicitly casts to double: \n";
d1 = i1;
cout << d1 << endl;  // 4.0
cout<<"double truncates to int!: \n";
i2 = d2;
cout << i2 << endl;  // 6
}

Sample Output:

Formatting the output using type casting:
----------------------------------------------
Print floating-point number in fixed format with 1 decimal place:
Test explicit type casting :
0
0.5
0.5
0.0

Test implict type casting :
0
0

int implicitly casts to double:
4.0
double truncates to int!:
6