0% found this document useful (0 votes)
35 views42 pages

C++ Practical Programs and Exercises

This document lists 50 practical programming problems in C++ that involve concepts like classes, functions, operators, inheritance, polymorphism, and more. The problems cover a wide range of fundamental programming tasks like number operations, string manipulation, arrays, searching, sorting, file handling and more.

Uploaded by

ecommercejvs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views42 pages

C++ Practical Programs and Exercises

This document lists 50 practical programming problems in C++ that involve concepts like classes, functions, operators, inheritance, polymorphism, and more. The problems cover a wide range of fundamental programming tasks like number operations, string manipulation, arrays, searching, sorting, file handling and more.

Uploaded by

ecommercejvs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

II bcom -C

PRACTICAL PROGRAMS

1. Write a C++ program to reverse a given number.

2. Write a C++ program to add two numbers using class.


3. Write a C++ program to check whether a given number is prime.
4. Write a C++ program to demonstrate the usage of scope resolution operator.
5. Write a C++ program to check whether a given year is leap year or not.
6. Write a C++ program to add two numbers using functions.

[Link] a C++ program to accept and display the details of a student using class.
[Link] a C++ program to accept and display the details of an employee using a class

Try by yourself

9. Write a C++ program to count the number of words and characters in a given text.
[Link] a C++ program to compare two strings using string functions.

[Link] a C++ program to find the GCD of two numbers.


[Link] a C++ program to calculate the area of rectangle, square using function overloading
13. Write a C++ program to add two numbers using pointers.

14. Write a C++ program to find the factorial of a given number.


15. Write a C++ program to search for an element using binary search.
16. Write a C++ program to sort an array in ascending order.
17. Write a C++ program to find the factorial of a given number using recursion.

18. Write a C++ program to check whether a given number is even or odd.
19. Write a C++ program to demonstrate the usage of Inline function.

20. Write a C++ program to demonstrate parameter passing mechanism using pass by value method.
21. Write a C++ program to demonstrate parameter passing mechanism using pass by address method.
[Link] a C++ program to demonstrate the usage of a constructor and destructor in a class.

23. Write a C++ program to demonstrate simple inheritance.


[Link] a C++ program to calculate volume of cube, cylinder and rectangle using function overloading

Try urself(DIY)
25. Write a C++ program to demonstrate the usage of friend function in a class.
26.. Write a C++ program to demonstrate the usage of endl and setw manipulators.

27. Write a C++ program to display employee information using multiple inheritance.
28. Write a C++ program to demonstrate multilevel inheritance.
29. Write a C++ program to create a file.

30. Write a C++ program to check whether a given number is a palindrome or not.
31. Write a C++ program to generate the Fibonacci series using while loop.

32. Write a C++ program to overload + operator to add two complex numbers.
33. Write a C++ program to search for a given element in an array using linear search.

34. Write a C++ program to read a text file.

use Class program


36. Write a C++ program to create a simple class named Account and write methods to deposit and
withdraw amount from the account.

Same as program 24
37. Write a C++ program to demonstrate dynamic memory allocation in c++.
38. Write a C++ program to demonstrate polymorphism by calculating area of rectangle and triangle
using a shape class.

Try by urself ( function overloading)


39. Write a C++ program using Switch case to add, subtract, multiply and divide two numbers.

40. Write a C++ program using class to implement basic operations on a stack using arrays.
41. Write a C++ program to display the sizes of various data types in c++ language.

42. Write a C++ program to accept and display employee details using structures.
43. Write a C++ program to find the length of a given string using string functions.

44. Write a C++ program to print the ASCII value of a user entered character.

45. Write a C++ program to add two dimensional matrices.


46. Write a C++ program to overload + (plus) operator to perform concatenation of two strings.
47. Write a C++ program to find the sum of elements in a given array.
48. Write a C++ program to find the largest of 3 numbers.
49. Write a C++ program to demonstrate exception handling by dividing a number with zero.

Use class program


50. Write a C++ program to convert a binary number to a decimal number

Common questions

Powered by AI

Using classes for arithmetic operations in C++ encapsulates data and functions into a single entity, enhancing code organization and reusability. It allows for abstraction by hiding the complexities involved in performing the operations and easing maintenance as changes need only be made within the class implementation. Moreover, classes can provide access control to certain operations, ensuring better data integrity and security. For instance, arithmetic operations like adding two numbers can be wrapped in a class, making them reusable across different programs .

Inheritance in C++ allows one class (child class) to inherit the properties and behavior of another class (parent class), enabling reusability of code and reduction of duplication. The child class can extend or override these functionalities, providing flexibility and promoting polymorphic behavior. This is particularly beneficial when creating hierarchies of classes that share a base set of behaviors but have additional specific functionalities. For example, using inheritance, a 'Vehicle' base class can be extended by 'Car' and 'Bike' classes, inheriting the common vehicle attributes while adding their unique features .

C++ exception handling can prevent division by zero errors using try-catch blocks. In the 'try' block, perform the division operation, and check if the denominator is zero before dividing. If it is zero, throw an exception using the 'throw' keyword. In the 'catch' block, catch the exception and handle it appropriately, such as by displaying an error message to the user or taking corrective measures to prevent program crashes. This provides a robust mechanism for dealing with runtime errors .

Polymorphism in C++ is achieved through method overriding and function overloading. For calculating areas of different shapes, a base 'Shape' class can define a virtual function for area computation. Derived classes like 'Rectangle' and 'Triangle' can override this function to provide specific implementations for area calculations. Alternatively, function overloading can be used where area functions are defined with different parameters for each shape type, enabling the same function name to be called with different arguments for varied results. This allows for flexible and interchangeable use of shape types at runtime .

Using a class to implement a stack with arrays in C++ provides encapsulation, managing stack operations such as push, pop, and check for empty or full states within a controlled scope. This encapsulation hides the underlining details from the user and ensures better data integrity and abstraction. It facilitates maintenance and scalability as the implementation details can be modified without impacting other parts of the program relying on the stack class. Such an approach is crucial in real-time systems where stack is a prevalent data structure, like syntax parsing, memory management, etc. .

The scope resolution operator (::) in C++ is used to define the context in which a variable, function, or type name is defined, typically to resolve naming conflicts or to specify the class to which a method or attribute belongs. It is particularly useful when a global function or variable has the same name as a local one, or when a class function's definition is written outside the class body. This operator can also access static members of a class or namespace's entities .

To reverse a number in C++, you must repeatedly extract the last digit of the number and build a new number with these digits in reverse order. Initialize an integer variable, say 'reversed', to 0. Use a loop to iterate over the input number, in each iteration, multiply 'reversed' by 10 and add to it the last digit of the number obtained by 'number % 10'. Then, remove the last digit from the number by dividing it by 10 ('number /= 10'). This process continues until the number becomes 0 .

Friend functions in C++ are necessary for certain situations where non-member functions need access to the private or protected data of a class. They allow external functions to interact efficiently with class internals without being a class member, providing flexibility in code structure. However, they break the fundamental encapsulation principle by exposing class internals, which might lead to integrity issues if not used judiciously. While they offer utility in operator overloading or when multiple classes must interact, their impact on encapsulation requires careful design consideration to maintain the class interface's abstraction .

Function overloading in C++ allows the creation of multiple functions with the same name but different signatures (parameter lists), enabling similar operations to be performed on different data types or different numbers of arguments. This enhances functionality by providing a more intuitive and versatile use of functions, allowing generalized methods to handle various input types with the same name, improving code readability and reducing the need for extensive documentation or unique function names for similar operations. It streamlines similar tasks under a unified function signature .

Dynamic memory allocation in C++ is demonstrated through the use of pointers and operators like 'new' and 'delete'. The 'new' operator is used to allocate memory on the heap, where variables can be dynamically created during runtime. For instance, 'int* ptr = new int;' allocates memory for an integer. To avoid memory leaks, the memory must be deallocated using 'delete ptr;' once it is no longer needed. Dynamic memory allocation is useful for data structures whose size cannot be predetermined, such as linked lists or dynamic arrays .

You might also like