## 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.

### C++ Basic Exercises

1. Write a program in C++ to print a welcome text in a separate line.

2.
Write a program in C++ to print the sum of two numbers.
Sample Output:

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

3. Write a program in C++ to find Size of fundamental data types.
Sample Output:
Find Size of fundamental data types :------------------------------------------The sizeof(char) is : 1 bytesThe sizeof(short) is : 2 bytesThe sizeof(int) is : 4 bytesThe sizeof(long) is : 8 bytesThe sizeof(long long) is : 8 bytesThe sizeof(float) is : 4 bytesThe sizeof(double) is : 8 bytesThe sizeof(long double) is : 16 bytesThe sizeof(bool) is : 1 bytes

4. Write a program in C++ to print the sum of two numbers using variables.
Print the sum of two numbers :
-----------------------------------
The sum of 29 and 30 is : 59

5. Write a program in C++ to check the upper and lower limits of integer.
Expected Output:
Check the upper and lower limits of integer :--------------------------------------------------The maximum limit of int data type : 2147483647The minimum limit of int data type : -2147483648The maximum limit of unsigned int data type : 4294967295The maximum limit of long long data type : 9223372036854775807The minimum limit of long long data type : -9223372036854775808The maximum limit of unsigned long long data type : 18446744073709551615The Bits contain in char data type : 8The maximum limit of char data type : 127The minimum limit of char data type : -128The maximum limit of signed char data type : 127The minimum limit of signed char data type : -128The maximum limit of unsigned char data type : 255The minimum limit of short data type : -32768The maximum limit of short data type : 32767The maximum limit of unsigned short data type : 65535

6. Write a program in C++ to check whether the primitive values crossing the limits or not.
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

7. Write a program in C++ to display various type or arithmetic operation using mixed data type.
Sample output:
Display arithmetic operations with mixed data type :---------------------------------------------------------5 + 7 = 123.7 + 8.0 = 11.75 + 8.0 = 13.05 - 7 = -23.7 - 8.0 = -4.35 - 8.0 = -3.05 * 7 = 353.7 * 8.0 = 29.65 * 8.0 = 40.05 / 7 = 03.7 / 8.0 = 0.55 / 8.0 = 0.6

8. Write a program in C++ to check overflow/underflow during various arithmetical operation.
Sample Output:
Check overflow/underflow during various arithmetical operation :Range of int is [-2147483648, 2147483647]---------------------------------------------------------------------Overflow the integer range and set in minimum range : -2147483648Increasing from its minimum range : -2147483647Product is :1Underflow the range and set in maximum range : 2147483647Decreasing from its maximum range : 2147483646Product is : 0

9. Write a program in C++ to display the operation of pre and post increment and decrement.
Sample Output:
Display the operation of pre and post increment and decrement :--------------------------------------------------------------------The number is : 57After post increment by 1 the number is : 58After pre increment by 1 the number is : 59After increasing by 1 the number is : 60After post decrement by 1 the number is : 59After pre decrement by 1 the number is : 58After decreasing by 1 the number is : 57

10. Write a program in C++ to formatting the output.
Sample Output:
Formatting the output :----------------------------The value of pi : 3.1416The 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+00Status
in number : 0Status in alphabet : false

11. Write a program in C++ to print the result of the specified operations.
Sample Output:
Print the result of some specific operation :--------------------------------------------------Result of 1st expression is : 23Result of 2nd expression is : 5Result of 3rd expression is : 12Result of 4th expression is : 3

12. Write a program in C++ to add two numbers accept through keyboard.
Sample Output:
Sum of two numbers :-------------------------Input 1st number : 25Input 2nd number : 39The sum of the numbers is : 64

13. Write a program in C++ to swap two numbers.
Sample Output:
Swap two numbers :-----------------------Input 1st number : 25Input 2nd number : 39After swapping the 1st number is : 39After swapping the 2nd number is : 25

14. Write a program in C++ to calculate the volume of a sphere.
Calculate the volume of a sphere :
---------------------------------------
Input the radius of a sphere : 6
The volume of a sphere is : 904.32

15. Write a program in C++ to calculate the volume of a cube.
Sample Output:
Calculate the volume of a cube :---------------------------------------Input the side of a cube : 5The volume of a cube is : 125

16. Write a program in C++ to calculate the volume of a cylinder.
Sample Output:
Calculate the volume of a cylinder :-----------------------------------------Input the radius of
the cylinder : 6Input the height of the cylinder : 8The volume of a cylinder is : 904.32

17. Write a program in C++ to find the Area and Perimeter of a Rectangle.
Sample Output:
Find the Area and Perimeter of a Rectangle :-------------------------------------------------Input the length of the rectangle : 10Input the width of the rectangle : 15The area of the rectangle is : 150The perimeter of the rectangle is : 50

18. Write a program in C++ to find the area of any triangle using Heron's Formula.
Sample Output:
Find the area of any triangle using Heron's Formula :----------------------------------------------------------Input the length of 1st side of the triangle : 5Input the length of 2nd side of the triangle : 5Input the length of 3rd side of the triangle : 5The area of the triangle is : 10.8253

19. Write a program in C++ to find the area and circumference of a circle.
Sample Output:
Find the area and circumference of any circle :----------------------------------------------------Input the radius(1/2 of diameter) of a circle : 5The area of the circle is : 78.5397The circumference of the circle is : 31.4159

20. Write a program in C++ to convert temperature in Celsius to Fahrenheit.
Sample Output:
Convert temperature in Celsius to Fahrenheit :---------------------------------------------------Input the temperature in Celsius : 35The temperature in Celsius : 35The temperature in Fahrenheit : 95

21.
Write a program in C++ to convert temperature in Fahrenheit to Celsius.
Sample Output:
Convert temperature in Fahrenheit to Celsius :---------------------------------------------------Input the temperature in
Fahrenheit : 95The temperature in Fahrenheit : 95The temperature in Celsius : 35

22. Write a program in C++ to find the third angle of a triangle.
Sample Output:
Find the third angle of a triangle :-----------------------------------------Input
the 1st angle of the triangle : 30Input the 2nd angle of the triangle : 60The 3rd of the triangle is : 90

23. Write a program in C++ that converts kilometers per hour to miles per hour.
Sample Output:
Convert kilometers
per hour to miles per hour :----------------------------------------------------Input the distance in kilometer : 25The 25 Km./hr. means 15.5343 Miles/hr.

24. Write a program in C++ to convert temperature in Kelvin to Fahrenheit.
Sample Output:
Convert temperature in Kelvin to Fahrenheit :---------------------------------------------------Input the temperature in Kelvin : 300The temperature in Kelvin : 300The temperature in Fahrenheit: 80.33

25. Write a program in C++ to convert temperature in Kelvin to Celsius.
Sample Output:
Convert temperature in Kelvin to Celsius :------------------------------------------------Input the temperature in Kelvin : 300The temperature in Kelvin : 300The temperature in Celsius : 26.85

26. Write a program in C++ to convert temperature in Fahrenheit to Kelvin.
Sample Output:
Convert temperature in Fahrenheit to Kelvin :---------------------------------------------------Input the temperature in Fahrenheit : 80.33The temperature in Fahrenheit : 80.33The temperature in Kelvin : 300

27.
Write a program in C++ to convert temperature in Celsius to Kelvin.
Sample Output:
Convert temperature in Celsius to Kelvin :---------------------------------------------------Input the temperature in Celsius : 26.85The temperature in Celsius : 26.85The temperature in Kelvin : 300

28. Write a program in C++ to find the area of Scalene Triangle.
Sample Output:
Find the area of Scalene Triangle :----------------------------------------Input the length of a side of the triangle : 5Input the length of another side of the triangle : 6Input the angle between these sides of the triangle : 6The area of the Scalene Triangle is : 1.56793

29. Write a program in C++ to compute quotient and remainder.
Sample Output:
Compute quotient and remainder :-------------------------------------Input the dividend : 25Input the divisor : 3The quotient of the division is : 8The remainder of the division is : 1

30. Write a program in C++ to compute the total and average of four numbers.
Sample Output:
Compute the total and average of four numbers :----------------------------------------------------Input 1st two numbers (separated by space) : 25 20Input last two numbers (separated by space) : 15 25The total of four numbers is : 85The average of four numbers is : 21.25

31. Write a program in C++ to input a single digit number and print a rectangular form of 4 columns and 6 rows.
Sample Output:
Make a rectangular shape by a single digit number :--------------------------------------------------------Input the number : 555555  55  55  55  55555

32. Write a program in C++ to check whether a number is positive, negative or zero.
Sample Output:Check whether a number is positive, negative or zero :-----------------------------------------------------------Input a number : 8The entered number is positive.

33.
Write a program in C++ to divide two numbers and print on the screen.
Sample Output:
Divide two numbers and print:----------------------------------The quotient of 30 and 10 is : 3

34. Write a C++ program to display the current date and time.
Sample Output:
Display the Current Date and Time :----------------------------------------seconds = 57minutes = 33hours = 12day of month = 6month of year = 7year = 2017weekday = 4day of year = 186daylight savings = 0Current Date: 6/7/2017Current Time: 12:33:57

35. Write a program in C++ to compute the specified expressions and print the output.
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

36. Write a program in C++ to test the Type Casting.
Sample Output:
Formatting the output using type casting:----------------------------------------------Print floating-point number in fixed format with 1 decimal place:Test explicit type casting:00.50.50.0Test implicit type casting:00int implicitly casts to double:4.0double truncates to int!:6

Source: W3resource, https://www.w3resource.com/cpp-exercises/basic/index.php