Hawassa University – IoT
Faculty of Informatics
Department of Information System
Fundamentals of Programming in C++
Lab Exercise 1 - Basics of C++
1. Write a program in C++ to print welcome text on 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 in C++ program to find the size of fundamental data types.
Sample Output:
Find Size of fundamental data types:
------------------------------------------
The sizeof(char) is: 1 byte
The sizeof(short) is: 2 bytes
The sizeof(int) is: 4 bytes
The sizeof(long) is: 8 bytes
The sizeof(long long) is : 8 byte
The sizeof(float) is: 4 bytes
The sizeof(double) is: 8 bytes
The sizeof(long double) is : 16 bytes
The sizeof(bool) is: 1 byte
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 C++ program to display the operation of pre and post increment and decrement.
1
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
6. Write a C++ program to add two numbers and accept them from the keyboard.
Sample Output:
Sum of two numbers:
-------------------------
Input 1st number: 25
Input 2nd number: 39
The sum of the numbers is: 64
7. Write a C++ program that swaps two numbers.
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
8. Write a C++ program that calculates the volume of a sphere, cube and cylinder. (hint: volsp =
4/3r3, Volcube = a*a*a=a3, Volcy = π*r2*height)
Sample Output:
Calculate the volume of a sphere, cube and cylinder:
---------------------------------------
Input the radius of a sphere: 6
2
Input the side of a cube: 5
Input the radius of the cylinder: 6
Input the height of the cylinder: 8
The volume of a sphere is: 904.32
The volume of a cube is: 125
The volume of a cylinder is: 904.32
9. Write a C++ program 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 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
10. Write a C++ program that accepts the user's first and last name and prints them in reverse order
with a space between them.
Sample Output:
Print the name in reverse where last name comes first:
-----------------------------------------------------------
Input First Name: Haileselassie
Input Last Name: Abdissa
Name in reverse is: Abdissa Haileselassie
11. Write a program to check whether the given number is positive or negative (using? : ternary
operator )?
12. Write a program to check whether the given number is even or odd (using ternary operator)?
13. Write a program to display the following output using a single cout statement.
Subject Marks
Mathematics 90
Computer 77
Chemistry 69