1.
A B C D E
ABCD
ABC
AB
A
Aim:
To write a C++ program to print the Given pattern.
Algorithm:
1. Start the program.
2. Loop through 5 lines, decreasing the number of characters to print each time.
3. For each line, loop through characters starting from 'A' up to the required letter.
4. Print the characters followed by a space.
5. Move to the next line.
6. End the program.
Program:
#include<iostream>
using namespace std;
int main()
{
char a[5]={'A','B','C','D','E'},i,j;
for(i=5;i>=1;i--){
for(j=0;j<i;j++){
cout<<a[j]<<"\t";
}
cout<<"\n";
}
return 0;
}
Result:
Thus, the above C++ program to print the Given pattern has been successfully
executed.
[Link] a C++ program to print the Given pattern.
Aim:
To write a C++ program to check if the given number is prime or not.
Algorithm:
1. Start the program.
2. Input a number from the user.
3. Check if the number is less than or equal to 1; if so, it is not prime.
4. For numbers greater than 1, loop through numbers from 2 to the square root of the
number.
5. If the number is divisible by any of these numbers, it is not prime.
6. If no divisors are found, the number is prime.
7. Print whether the number is prime or not.
8. End the program.
Program:
#include <iostream>
using namespace std;
int main() {
int num, primeFlag = 0;
cout << "Enter a positive integer: ";
cin >> num;
if (num <= 1) {
primeFlag = 1;
} else {
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
primeFlag = 1; // If num is divisible by i, it's not prime
break;
}
}
}
if (primeFlag == 0)
cout << num << " is a prime number." << endl;
else
cout << num << " is not a prime number." << endl;
return 0;
}
Output:
Enter a positive integer: 7
7 is a prime number.
Enter a positive integer: 12
12 is not a prime number.
Result:
Thus, the above C++ program to check whether the given number is prime or
not has been successfully executed.
[Link] a C++ program to input and output data for finding area of rectangle using
functions.
Aim:
To create a C++ program that inputs the length and width of a rectangle and
calculates the area using a function.
Algorithm:
1. Start the program.
2. Declare variables for length, width, and area.
3. Create a function to calculate the area.
4. Input length and width from the user.
5. Call the function to compute the area.
6. Display the area.
7. End the program.
Program:
#include <iostream>
using namespace std;
double calculateArea(double length, double width) {
return length * width;
}
int main() {
double length, width, area;
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
area = calculateArea(length, width);
cout << "The area of the rectangle is: " << area << endl;
return 0;
}
Output:
Enter the length of the rectangle: 5
Enter the width of the rectangle: 3
The area of the rectangle is: 15
Result:
Thus, the above C++ program for calculating the area of the rectangle using a
function has been successfully executed.
[Link] a C++ program using functions to perform matrix addition & subtraction.
Aim:
To write a C++ program that performs matrix addition and subtraction using
functions.
Algorithm:
1. Startthe program.
2. Input the dimensions and elements of two matrices.
3. Create functions for matrix addition and subtraction.
4. Perform the operations by calling the respective functions.
5. Display the results.
6. End the program.
Program:
#include <iostream>
using namespace std;
const int MAX = 3;
void addMatrix(int matrix1[MAX][MAX], int matrix2[MAX][MAX], int
result[MAX][MAX]) {
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}
void subtractMatrix(int matrix1[MAX][MAX], int matrix2[MAX][MAX], int
result[MAX][MAX]) {
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
}
void displayMatrix(int matrix[MAX][MAX]) {
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main() {
int matrix1[MAX][MAX], matrix2[MAX][MAX], result[MAX][MAX];
cout << "Enter elements of matrix 1 (3x3):" << endl;
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
cin >> matrix1[i][j];
}
}
cout << "Enter elements of matrix 2 (3x3):" << endl;
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
cin >> matrix2[i][j];
}
}
addMatrix(matrix1, matrix2, result);
cout << "Matrix after addition:" << endl;
displayMatrix(result);
subtractMatrix(matrix1, matrix2, result);
cout << "Matrix after subtraction:" << endl;
displayMatrix(result);
return 0;
}
Output:
Enter elements of matrix 1 (3x3):
123
456
789
Enter elements of matrix 2 (3x3):
987
654
321
Matrix after addition:
10 10 10
10 10 10
10 10 10
Matrix after subtraction:
-8 -6 -4
-2 0 2
468
Result:
Thus, the above C++ program for performing matrix addition and subtraction using
functions has been successfully executed.
[Link] a C++ program to find and print the volume of a cube using inline
functions.
Aim:
To .
Algorithm:
1. Startthe program.
2. Input the side length of the cube.
3. Define an inline function to calculate the volume.
4. Output the volume.
5. End the program.
Program:
#include <iostream>
using namespace std;
inline double volumeOfCube(double side) {
return side * side * side;
}
int main() {
double side;
cout << "Enter the side length of the cube: ";
cin >> side;
cout << "The volume of the cube is: " << volumeOfCube(side) << endl;
return 0;
}
Output:
Enter the side length of the cube: 4
The volume of the cube is: 64
Result:
Thus, the above C++ program for calculating the volume of a cube using inline
functions has been successfully executed.
[Link] a C++ program to find the area of a square and rectangle using function
overloading.
Aim:
To write a C++ program that calculates the area of a square and a rectangle using
function overloading.
Algorithm:
1. Define a class Shape.
2. Overload the function area() to compute the area of a square and a rectangle.
3. Call the appropriate function based on the number of arguments passed.
Program:
#include <iostream>
using namespace std;
class Shape {
public:
int area(int side) {
return side * side;
}int area(int length, int breadth) {
return length * breadth;
}
};
int main() {
Shape shape;
int side = 5;
int length = 10;
int breadth = 7;
cout << "Area of square: " << [Link](side) << endl;
cout << "Area of rectangle: " << [Link](length, breadth) << endl;
return 0;
}
[Link] C++ program to find the volume of a cube, cone and a rectangle using
function overloading.
Aim:
To write a C++ program to find the volume of a cube, cone, and a rectangular
prism using function overloading.
Algorithm:
1. Define a class Volume.
2. Overload the function volume() to compute the volume of a cube, cone, and
rectangular prism.
3. Call the appropriate function based on the number of arguments passed.
Program:
#include <iostream>
#include <cmath>
using namespace std;
class Volume {
public:
int volume(int side) {
return side * side * side;
}
double volume(double radius, double height) {
return (M_PI * radius * radius * height) / 3;
}
int volume(int length, int breadth, int height) {
return length * breadth * height;
}
};
int main() {
Volume vol;
int side = 4;
double radius = 3.0;
double height_cone = 5.0;
int length = 7, breadth = 3, height_prism = 6
cout << "Volume of cube: " << [Link](side) << endl;
cout << "Volume of cone: " << [Link](radius, height_cone) << endl;
cout << "Volume of rectangular prism: " << [Link](length,
breadth, height_prism) << endl;
return 0;
}
[Link] a C++ program to swap two integers, floats, characters and two strings suing
function overloading concept.
Aim:
To write a C++ program that swaps two integers, floats, characters, and two strings
using function overloading.
Algorithm:
1. Define a class Swapper.
2. Overload the function swap() to handle different data types (integers, floats,
characters, and strings).
3. Use the function to swap two values of each type.
Program:
#include <iostream>
#include <string>
using namespace std;
class Swapper {
public:
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
void swap(float &a, float &b) {
float temp = a;
a = b;
b = temp;
}
void swap(char &a, char &b) {
char temp = a;
a = b;
b = temp;
}
void swap(char a[], char b[]) {
char temp[50];
temp = a;
a = b;
b = temp;
}
};
int main() {
Swapper swapper;
int int1 = 10, int2 = 20;
float float1 = 3.14f, float2 = 1.59f;
char char1 = 'A', char2 = 'B';
char str1[50] = "Hello", str2[50]= "World";
cout << "Before swapping: " << endl;
cout << "Integers: " << int1 << ", " << int2 << endl;
cout << "Floats: " << float1 << ", " << float2 << endl;
cout << "Characters: " << char1 << ", " << char2 << endl;
cout << "Strings: " << str1 << ", " << str2 << endl;
[Link](int1, int2);
[Link](float1, float2);
[Link](char1, char2);
[Link](str1, str2);
cout << "\nAfter swapping: " << endl;
cout << "Integers: " << int1 << ", " << int2 << endl;
cout << "Floats: " << float1 << ", " << float2 << endl;
cout << "Characters: " << char1 << ", " << char2 << endl;
cout << "Strings: " << str1 << ", " << str2 << endl;
return 0;
}
[Link] three classes Student, Test and Result classes. The student class contains
student relevant information. Test class contains marks for five subjects. The result
class contains Total and average of the marks obtained in five subjects. Inherit the
properties of Student and Test class details in Result class through multilevel
inheritance.
Aim:
To demonstrate multilevel inheritance using Student, Test, and Result classes
where a Student class holds student information, Test holds marks of five subjects,
and Result holds the total and average of marks.
Algorithm:
1. Create a Student class to store student information.
2. Create a Test class that inherits from Student and stores marks of five subjects.
3. Create a Result class that inherits from Test and calculates total and average marks.
4. In the main() function, create an object of Result and call functions to get and
display data.
Program:
#include <iostream>
using namespace std;
class Student {
protected:
char name[50];
int roll_number;
public:
void get_student_data() {
cout << "Enter name: ";
cin >> name;
cout << "Enter roll number: ";
cin >> roll_number;
}
void put_student_data() {
cout << "Name: " << name << endl;
cout << "Roll Number: " << roll_number << endl;
}
};
class Test : public Student {
protected:
float marks[5];
public:
void get_marks() {
cout << "Enter marks for 5 subjects: ";
for(int i = 0; i < 5; i++)
cin >> marks[i];
}
void put_marks() {
cout << "Marks in 5 subjects: ";
for(int i = 0; i < 5; i++)
cout << marks[i] << " ";
cout << endl;
}
};
class Result : public Test {
private:
float total, average;
public:
void calculate_result() {
total = 0;
for(int i = 0; i < 5; i++)
total += marks[i];
average = total / 5;
}
void display_result() {
put_student_data();
put_marks();
cout << "Total Marks: " << total << endl;
cout << "Average Marks: " << average << endl;
}
};
int main() {
Result student;
student.get_student_data();
student.get_marks();
student.calculate_result();
student.display_result();
return 0;
}
[Link] three classes Student, Test and Result classes. The student class
contains student relevant information. Test class contains marks for five subjects.
The result class contains Total and average of the marks obtained in five subjects.
Inherit the properties of Student and Test class details in Result class through
multiple inheritance.
Aim:
To create a system where Student contains basic student information, Test contains
marks of five subjects, and Result calculates the total and average marks using
multiple inheritance.
Algorithm:
1. Create a Student class that stores student information (name, roll number).
2. Create a Test class that stores marks of five subjects.
3. Create a Result class that inherits from both Student and Test classes.
4. The Result class will calculate the total and average of marks.
5. Implement getdata() and putdata() functions in each class for input and output.
6. In
the main() function, create an object of Result and use it to test functionality.
Program:
#include <iostream>
using namespace std;
class Student {
protected:
char name[50];
int roll_number;
public:
void get_student_data() {
cout << "Enter student name: ";
cin >> name;
cout << "Enter roll number: ";
cin >> roll_number;
}
void put_student_data() {
cout << "Name: " << name << endl;
cout << "Roll Number: " << roll_number << endl;
}
};
class Test {
protected:
float marks[5];
public:
void get_marks() {
cout << "Enter marks for 5 subjects: ";
for (int i = 0; i < 5; i++)
cin >> marks[i];
}
void put_marks() {
cout << "Marks: ";
for (int i = 0; i < 5; i++)
cout << marks[i] << " ";
cout << endl;
}
};
class Result : public Student, public Test {
private:
float total, average;
public:
void calculate_result() {
total = 0;
for (int i = 0; i < 5; i++)
total += marks[i];
average = total / 5;
}
void display_result() {
put_student_data();
put_marks();
cout << "Total Marks: " << total << endl;
cout << "Average Marks: " << average << endl;
}
};
int main() {
Result student;
student.get_student_data();
student.get_marks();
student.calculate_result();
student.display_result();
return 0;
}
[Link] three classes Student, Test and Result classes. The student class
contains student relevant information. Test class contains marks for five subjects.
The result class contains Total and average of the marks obtained in five subjects.
Inherit the properties of Student and Test class details in Result class through
multiple inheritance
Aim:
To implement a system with multiple levels of inheritance where the CEO class
inherits from Boss, and Boss inherits from Employee. The CEO class will use
scope resolution to access functions from both Employee and Boss.
Algorithm:
1. Create an Employee class that stores basic employee information (name, employee
number, department, salary).
2. Create a Boss class that inherits from Employee and adds bonus information.
3. Create a CEO class that inherits from Boss and adds share ownership information.
4. Implement AskInfo() and writeInfo() functions in all classes.
5. In the main() function, create a CEO object and call the relevant functions, using
the scope resolution operator to access base class methods.
Program:
#include <iostream>
using namespace std;
class Employee {
protected:
char name[50];
int employee_no;
string department;
float salary;
public:
void AskInfo() {
cout << "Enter employee name: ";
cin >> name;
cout << "Enter employee number: ";
cin >> employee_no;
cout << "Enter department: ";
cin >> department;
cout << "Enter salary: ";
cin >> salary;
}
void writeInfo() {
cout << "Name: " << name << endl;
cout << "Employee No: " << employee_no << endl;
cout << "Department: " << department << endl;
cout << "Salary: $" << salary << endl;
}
};
class Boss : public Employee {
protected:
float bonus;
public:
void AskInfo() {
Employee::AskInfo();
cout << "Enter bonus: ";
cin >> bonus;
}
void writeInfo() {
Employee::writeInfo();
cout << "Bonus: $" << bonus << endl;
}
};
class CEO : public Boss {
private:
int shares;
public:
void AskInfo() {
Boss::AskInfo(); // Call base class function
cout << "Enter number of shares: ";
cin >> shares;
}
void writeInfo() {
Boss::writeInfo(); // Call base class function
cout << "Shares: " << shares << endl;
}
};
int main() {
CEO ceo;
[Link]();
cout << "\nCEO Details:\n";
[Link]();
return 0;
}
[Link] a function template for finding the minimum value contained in an array.
Aim:
To create a function template in C++ that can find the minimum value from an
array of any data type (integers, floats, doubles, etc.).
Algorithm:
1. Define a template function findMin() that accepts an array and its size as
arguments.
2. Initialize a variable minValue with the first element of the array.
3. Iterate through the array and compare each element with minValue. If a smaller
value is found, update minValue.
4. Return the minimum value.
Program:
#include <iostream>
using namespace std;
template <typename T>
T findMin(T arr[], int size) {
T minValue = arr[0]; // Initialize minValue with the first element
for (int i = 1; i < size; i++) {
if (arr[i] < minValue) {
minValue = arr[i];
}
}
return minValue;
}
int main() {
int intArray[] = {3, 5, 2, 9, 1};
float floatArray[] = {5.6, 3.2, 9.8, 1.1, 2.3};
cout << "Minimum in integer array: " << findMin(intArray, 5) << endl;
cout << "Minimum in float array: " << findMin(floatArray, 5) << endl;
return 0;
}
[Link] C++ program to design a simple calculator using class and function
template
Aim:
To design a simple calculator in C++ using a class and function templates for basic
arithmetic operations (addition, subtraction, multiplication, and division).
Algorithm:
1. Create a class Calculator with a function template calculate().
2. The calculate() function will perform operations based on a specified operator (+, -,
*, /).
3. In the main() function, create an object of the Calculator class and call the template
function to perform calculations on different data types.
Program:
#include <iostream>
using namespace std;
template < class t>
class calculator {
private:
t num1, num2;
public:
calculator(t n1, t n2){
num1=n1;
num2=n2;
}
void display (){
cout<<"Values : "<<num1<<" and "<<num2<<endl;
cout<<num1<<" + "<<num2<<" : "<<add()<<endl;
cout<<num1<<" - "<<num2<<" : "<<sub()<<endl;
cout<<num1<<" * "<<num2<<" : "<<mul()<<endl;
cout<<num1<<" / "<<num2<<" : "<<div()<<endl;
}
t add(){
return num1+num2;
}
t sub(){
return num1-num2;
}
t mul(){
return num1*num2;
}
t div(){
return num1/num2;
}
};
int main(){
calculator<int> ical(5,3);
calculator<float> fcal(5.3,3.4);
[Link]();
[Link]();
return 0;
}
[Link] two functions with an exception-specification lists as follows: A) A
function which can throw only an integer exception B)A function which can throw
only a string exception Design a main( ) function with a try block that is used to
test and handle exceptions thrown by these two functions.
Aim:
To write a C++ program for designing two functions with an exception-
specification.
Algorithm:
1. Definetwo functions:
Function A that throws an integer exception.
Function B that throws a string exception.
2. In
the main() function:
Use try-catch blocks to handle exceptions thrown by these functions.
Call Function A and Function B inside the try block.
Catch the exceptions separately for integer and string types.
Program:
#include <iostream>
#include <string>
using namespace std;
void functionA() {
throw 10; // Throwing an integer exception
}
void functionB() {
throw string("String exception thrown");
}
int main() {
try {
functionA();
}
catch (int e) {
cout << "Caught an integer exception: " << e << endl;
}
try {
functionB();
}
catch (string& e) {
cout << "Caught a string exception: " << e << endl;
}
return 0;
}