C++ Practical List
C++ Practical List
1. Write a program in C++ to compute the sum of the first 𝑛 terms of the following series:
a. 𝑠𝑢𝑚 = 1 − 2 + 3 − 4 + 5 …
b. 𝑠𝑢𝑚 = 1 + 1/2 + 1/3 + 1/4 + ⋯
c. 𝑠𝑢𝑚 = 1 + 1/2! + 1/3! + 1/4! + ⋯
d. 𝑠𝑢𝑚 = 1 − 2𝑛 + 3𝑛 − 4𝑛 + 5𝑛 − ⋯
Prompt the user to enter the value of 𝑛.
2. Write a program in C++ to display the following pattern:
A
BA
CBA
DCBA
Prompt the user to enter the number of rows, 𝑛.
3. Write a function to find whether a given number is prime or not. Use the same to generate
a. first 𝑛 prime numbers
b. prime numbers up to 𝑛
c. 𝑛 prime numbers starting from 𝑚
4. Write a program in C++ to compute the factors and prime factors of a given number.
5. Write a program in C++ to calculate GCD of two numbers a. with recursion and b. without
recursion.
6. Write a function to generate an element of a Fibonacci series and call it from the main ()
function to generate the first 𝑛 elements of the series.
7. Write a program in C++ to compute the factors of a given number using the default
argument.
8. Write a menu driven program in C++ to perform the following operations on an array of
integers:
a) find the minimum, maximum and average of the array elements
b) search an element in the array using linear search
c) search an element in the array using binary search
d) display the address of every element of the array
e) remove the duplicates from an array
9. Write a program in C++ to merge two sorted integer arrays into a single sorted array.
10. Write a program in C++ to split an array of integers into two arrays, where one array stores
all even and the other stores all odd integers.
11. Write a program in C++ that prints a table indicating the number of occurrences of each
letter of the alphabet in the text entered as input.
12. Write a function chkPalin () that checks whether an input string is a palindrome or not and
returns true if it is and false if not. Use this function to find whether the string entered by
the user is a palindrome or not.
13. Write a menu driven program in C++ to perform string manipulation (without using built-
in string functions):
a. Show address of each character in string
b. Concatenate two strings
c. Compare two strings
d. Calculate length of the string (use pointers)
e. Convert all lowercase characters to uppercase
f. Reverse the string
g. Insert a string in another string at a user specified position
14. Write a program in C++ having a function to swap two numbers using pointers.
15. Define a class Circle with radius as a data member. Define default, parameterized and copy
constructors. Also define member functions to store radius, calculate area and
circumference, and to print area and circumference. Define the driver function main () to
create objects and call the above functions.
16. Define a Matrix class and write a menu-driven program in C++ to perform following
operations (exceptions should be thrown by the functions if matrices passed to them are
handled by the main () function):
a. sum
b. product
c. transpose
17. Define a class Person having name and age as data members. Define two classes Student
and Employee that publicly inherit from class Person. Class Student has additional
attributes— course, marks and year and class Employee has department and salary. Write
display () methods in all the three classes to display the corresponding attributes. Provide
the necessary methods to show runtime polymorphism.
18. Define a class Student containing fields for roll no, name, class, year and total marks.
Define the driver function main () to store 5 objects of the class Student in a file. Retrieve
these records from the file and display them.
19. Define a class Fraction with numer and denom as data members. Define default,
parameterized and copy constructors. Also define member functions to perform basic
operations on the fractions. Define the driver function main () to create objects and call the
above functions.
20. Define a class Complex to store complex numbers. Define default, parameterized and copy
constructors. Also define member functions to perform basic operations on the complex
numbers. Define the driver function main () to create objects and call the above functions.
21. Define a class Triangle. Add exception handling statements to ensure the following
conditions: all sides are greater than 0 and sum of any two sides is greater than the third
side. The class should also have overloaded functions for calculating the area of a right-
angled triangle as well as using Heron's formula to calculate the area of any type of triangle.
Define the driver function main () to create objects and call the above functions.
22. Create a template class TwoDim which contains 𝑥 and 𝑦 coordinates. Define default
constructor, parameterized constructor and void print () function to print the coordinates.
Now reuse this class in ThreeDim adding a new dimension as 𝑧. Define the constructors
and void print () in the subclass. Implement main () to show runtime polymorphism.
23. Write a program in C++ to copy the contents of one text file to another file and display the
total number of characters, words and lines copied.
24. Write a program in C++ to copy the contents of one text file to another file, after removing
all whitespaces.
25. Write a program in C++ to copy the contents of one text file to another file, while inserting
a new line after each string.