Programmin C & C++ Lab Report
Programmin C & C++ Lab Report
PROGRAM-01
OBJECTIVE : Write a c++ program to find simple interest and compound interest.
CODE :
#include <iostream.h>
#include <conio.h>
#include <math.h> // For pow function
// Function to calculate simple interest
double calculateSimpleInterest(double principal, double rate, double time)
{
return (principal * rate * time) / 100;
}
// Function to calculate compound interest
double calculateCompoundInterest(double principal, double rate, double time, int n)
{
return principal * pow((1 + rate / (100 * n)), n * time) - principal;
}
int main()
{
double principal, rate, time;
int n;
clrscr();
// Taking user input
cout << "\nEnter principal amount: ";
cin >> principal;
cout << "\nEnter annual interest rate (in percentage): ";
cin >> rate;
cout << "\nEnter time (in years): ";
cin >> time;
cout << "\nEnter number of times interest is compounded per year: ";
cin >> n;
// Calculating interests
Page | 1
Programming C & C++ PGDCA : 2024-25
double simpleInterest = calculateSimpleInterest(principal, rate, time);
double compoundInterest = calculateCompoundInterest(principal, rate, time, n);
// Displaying results
cout << "Simple Interest: " << simpleInterest << endl;
cout << "Compound Interest: " << compoundInterest << endl;
getch();
return 0;
}
OUTPUT :
Page | 2
Programming C & C++ PGDCA : 2024-25
PROGRAM-02
OBJECTIVE : Write a c++ program to convert temperature from Fahrenheit to Celsius.
CODE :
#include <iostream.h>
#include <conio.h>
// Function to convert Fahrenheit to Celsius
double fahrenheitToCelsius(double fahrenheit)
{
return (fahrenheit - 32) * 5 / 9;
}
int main()
{
double fahrenheit, celsius;
clrscr();
// Taking user input
cout << "\nEnter temperature in Fahrenheit: ";
cin >> fahrenheit;
// Converting to Celsius
celsius = fahrenheitToCelsius(fahrenheit);
// Displaying result
cout << "Temperature in Celsius: " << celsius << "°C" << endl;
getch();
return 0;
}
OUTPUT :
Page | 3
Programming C & C++ PGDCA : 2024-25
PROGRAM-03
OBJECTIVE : Write a c++ program to generate a multiplication table of given number using
for loop.
CODE :
#include
<iostream.h>
#include <conio.h>
int main()
{
int num;
clrscr();
// Taking user input
cout << "\nEnter a number: ";
cin >> num;
// Generating multiplication table using for loop
cout << "Multiplication Table of " << num << ":\
n"; for (int i = 1; i <= 10; i++)
{
cout << num << " x " << i << " = " << num * i << endl;
}
getch();
return 0;
}
OUTPUT :
Page | 4
Programming C & C++ PGDCA : 2024-25
PROGRAM-04
OBJECTIVE : Write a c++ program to find factorial of given number using a do-while loop.
CODE :
#include
<iostream.h>
#include <conio.h>
int main()
{
int num;
long long factorial = 1;
clrscr();
// Taking user input
cout << "\nEnter a number: ";
cin >> num;
int i = 1;
// Calculating factorial using do-while loop
do
{
factorial *= i;
i++;
} while (i <= num);
// Displaying result
cout << "Factorial of " << num << " is: " << factorial << endl;
getch();
return 0;
}
OUTPUT :
Page | 5
Programming C & C++ PGDCA : 2024-25
PROGRAM-05
OBJECTIVE : Write a c++ program to find Fibonacci series up to nth terms.
CODE :
#include
<iostream.h>
#include <conio.h>
int main()
{
int n, t1 = 0, t2 = 1, nextTerm;
clrscr();
// Taking user input
cout << "\nEnter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; i++) {
cout << t1 << " ";
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
cout <<
endl; getch();
return 0;
}
OUTPUT :
Page | 6
Programming C & C++ PGDCA : 2024-25
PROGRAM-06
OBJECTIVE : Write a c++ program to find whether inserted year is leap year or not.
CODE :
#include
<iostream.h>
#include <conio.h>
int main()
{
int year;
clrscr();
// Taking user input
cout << "\nEnter a year: ";
cin >> year;
// Checking leap year condition
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
cout << year << " is a leap year." << endl;
}
else
{
cout << year << " is not a leap year." << endl;
}
getch();
return 0;
}
OUTPUT :
Page | 7
Programming C & C++ PGDCA : 2024-25
PROGRAM-07
OBJECTIVE : Write a c++ program to find whether given number is prime or not.
CODE :
#include
<iostream.h>
#include <conio.h>
int isPrime(int num)
{
if (num <= 1) return 0;
for (int i = 2; i * i <= num; i++)
{
if (num % i == 0) return 0;
}
return 1;
}
void main()
{
clrscr();
int num;
cout << "\nEnter a number: ";
cin >> num;
if (isPrime(num))
{
cout << num << " is a prime number.\n";
}
else
{
cout << num << " is not a prime number.\n";
}
getch();
}
Page | 8
Programming C & C++ PGDCA : 2024-25
OUTPUT :
Page | 9
Programming C & C++ PGDCA : 2024-25
PROGRAM-08
OBJECTIVE : Write a c++ program to input three numbers and find the largest using
relational and logical operators.
CODE :
#include
<iostream.h>
#include <conio.h>
int main()
{
int num1, num2, num3;
clrscr();
// Taking user input
cout << "\nEnter three numbers: ";
cin >> num1 >> num2 >> num3;
// Finding the largest number using relational and logical
operators if (num1 >= num2 && num1 >= num3)
{
cout << "The largest number is: " << num1 << endl;
}
else if (num2 >= num1 && num2 >= num3)
{
cout << "The largest number is: " << num2 << endl;
}
else
{
cout << "The largest number is: " << num3 << endl;
}
getch();
return 0;
}
Page | 10
Programming C & C++ PGDCA : 2024-25
OUTPUT :
Page | 11
Programming C & C++ PGDCA : 2024-25
PROGRAM-09
OBJECTIVE : Write a c++ program to input marks and print the grade based on the following
conditions:
Marks >= 90: Grade A
Marks >= 75: Grade B
Marks >= 50: Grade C
Marks < 50: Fail
CODE :
#include
<iostream.h>
#include <conio.h>
int main()
{
int marks;
clrscr();
// Taking user input
cout << "\nEnter marks: ";
cin >> marks;
// Determining grade based on marks
if (marks >= 90)
{
cout << "Grade: A" << endl;
}
else if (marks >= 75)
{
cout << "Grade: B" << endl;
}
else if (marks >= 50)
{
cout << "Grade: C" << endl;
}
else
{
cout << "Fail" << endl;
Page | 12
Programming C & C++ PGDCA : 2024-25
}
getch();
return 0;
}
OUTPUT :
Page | 13
Programming C & C++ PGDCA : 2024-25
PROGRAM-10
OBJECTIVE : Create a menu-driven c++ program using the switch statement to perform
basic arithmetic operations (Addition, Subtraction, Multiplication, and
Division).
CODE :
#include
<iostream.h>
#include <conio.h>
int main()
{
int choice;
double num1, num2, result;
clrscr();
// Display menu
cout << "\nMenu:\n";
cout << "1. Addition\n";
cout << "2. Subtraction\n";
cout << "3. Multiplication\n";
cout << "4. Division\n";
cout << "\nEnter your choice (1-4): ";
cin >> choice;
// Taking input numbers
cout << "\nEnter two numbers: ";
cin >> num1 >> num2;
// Performing operations based on user choice
switch (choice)
{
case 1:
result = num1 + num2;
cout << "Result: " << result << endl;
break;
case 2:
result = num1 - num2;
cout << "Result: " << result << endl;
Page | 14
Programming C & C++ PGDCA : 2024-25
break;
case 3:
result = num1 * num2;
cout << "Result: " << result << endl;
break;
case 4:
if (num2 != 0)
{
result = num1 / num2;
cout << "Result: " << result << endl;
}
else
{
cout << "Error! Division by zero is not allowed." << endl;
}
break;
default:
cout << "Invalid choice! Please enter a number between 1 and 4." << endl;
}
getch();
return 0;
}
OUTPUT :
Page | 15
Programming C & C++ PGDCA : 2024-25
PROGRAM-11
OBJECTIVE : Write a c++ program to input a day number (1-7) and print the corresponding
day of the week using the switch statement.
CODE :
#include
<iostream.h>
#include <conio.h>
int main()
{
int day;
clrscr();
// Taking user input
cout << "\nEnter a day number (1-7): ";
cin >> day;
// Determining the day of the week
switch (day)
{
case 1:
cout << "Sunday" << endl;
break;
case 2:
cout << "Monday" << endl;
break;
case 3:
cout << "Tuesday" << endl;
break;
case 4:
cout << "Wednesday" << endl;
break;
case 5:
cout << "Thursday" << endl;
break;
case 6:
Page | 16
Programming C & C++ PGDCA : 2024-25
cout << "Friday" << endl;
break;
case 7:
cout << "Saturday" << endl;
break;
default:
cout << "Invalid input! Please enter a number between 1 and 7." << endl;
}
getch();
return 0;
}
OUTPUT :
Page | 17
Programming C & C++ PGDCA : 2024-25
PROGRAM-12
OBJECTIVE : Write a single c++ program to perform following tasks using if. Else, loop and
single dimension integer array:
a) Sort the elements.
b) Find greater number and average.
int main()
{
int n,i,j;
int
arr[20];
clrscr();
getch();
return 0;
}
OUTPUT :
Page | 19
Programming C & C++ PGDCA : 2024-25
CODE : For (b)
#include <iostream.h>
#include <conio.h>
Page | 20
Programming C & C++ PGDCA : 2024-25
void main()
{
clrscr();
int size;
cout << "\nEnter the number of elements: ";
cin >> size;
int arr[30];
cout << "Enter " << size << " elements: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
sortArray(arr, size);
Page | 21
Programming C & C++ PGDCA : 2024-25
cout << "Greatest number: " << findGreatest(arr, size) << "\n";
cout << "Average: " << findAverage(arr, size) << "\n";
getch();
}
OUTPUT :
Page | 22
Programming C & C++ PGDCA : 2024-25
PROGRAM-13
OBJECTIVE : Write a single c++ program to perform following tasks using if, Else, loop and
double dimension integer array of size 3x3:
a) Addition of two matrix.
b) Subtraction of two matrix.
c) Multiplication of two matrix.
CODE : For (a)
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int matrix1[3][3], matrix2[3][3], sum[3][3];
int i,j;
cout << "\nEnter elements of first 3x3 matrix:\n";
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
cin >> matrix1[i][j];
}
}
cout << "Enter elements of second 3x3 matrix:\n";
for ( i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
cin >> matrix2[i][j];
}
}
// Perform addition
for (i = 0; i < 3; i++)
{
Page | 23
Programming C & C++ PGDCA : 2024-25
for (j = 0; j < 3; j++)
{
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
cout << "Resultant Matrix after Addition:\n";
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
cout << sum[i][j] << " ";
}
cout << "\n";
}
getch();
}
OUTPUT :
Page | 24
Programming C & C++ PGDCA : 2024-25
CODE : For (b)
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int matrix1[3][3], matrix2[3][3], difference[3][3];
int i,j;
cout << "\nEnter elements of first 3x3 matrix:\n";
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
cin >> matrix1[i][j];
}
}
cout << "Enter elements of second 3x3 matrix:\n";
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
cin >> matrix2[i][j];
}
}
// Perform subtraction
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
difference[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
Page | 25
Programming C & C++ PGDCA : 2024-25
OUTPUT :
Page | 26
Programming C & C++ PGDCA : 2024-25
CODE : For (c)
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int matrix1[3][3], matrix2[3][3], product[3][3] = {0};
int i,j,k;
cout << "\nEnter elements of first 3x3 matrix:\n";
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
cin >> matrix1[i][j];
}
}
cout << "Enter elements of second 3x3 matrix:\n";
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
cin >> matrix2[i][j];
}
}
// Perform multiplication
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
product[i][j] = 0;
for (k = 0; k < 3; k++)
{
Page | 27
Programming C & C++ PGDCA : 2024-25
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
cout << "Resultant Matrix after Multiplication:\n";
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
cout << product[i][j] << " ";
}
cout << "\n";
}
getch();
}
OUTPUT :
Page | 28
Programming C & C++ PGDCA : 2024-25
PROGRAM-14
OBJECTIVE : Write a c++ program of swapping two numbers and demonstrates call by
value and call by reference.
CODE :
#include <iostream.h>
#include <conio.h>
// Function to swap numbers using Call by Value
void swapByValue(int a, int b)
{
int temp = a;
a = b;
b = temp;
cout << "Inside swapByValue: a = " << a << ", b = " << b << "\n";
}
// Function to swap numbers using Call by Reference
void swapByReference(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void main()
{
clrscr();
int x, y;
cout << "\nEnter two numbers: ";
cin >> x >> y;
cout << "Before swapping: x = " << x << ", y = " << y << "\n";
// Call by Value
swapByValue(x, y);
cout << "After swapByValue: x = " << x << ", y = " << y << " (No change)\n";
// Call by Reference
Page | 29
Programming C & C++ PGDCA : 2024-25
swapByReference(x, y);
cout << "After swapByReference: x = " << x << ", y = " << y << " (Values swapped)\n";
getch();
}
OUTPUT :
Page | 30
Programming C & C++ PGDCA : 2024-25
PROGRAM-15
OBJECTIVE : Write a c++ program to return the largest of three numbers using return by
reference.
CODE :
#include <iostream.h>
#include <conio.h>
// Function to return the largest number by reference
int &largest(int &a, int &b, int &c)
{
if (a > b && a > c)
return a;
else if (b > c)
return b;
else
return c;
}
void main()
{
clrscr();
int x, y, z;
cout << "\nEnter three numbers: ";
cin >> x >> y >> z;
int &largestNum = largest(x, y, z);
cout << "\nThe largest number is: " << largestNum << "\n";
getch();
}
OUTPUT :
Page | 31
Programming C & C++ PGDCA : 2024-25
PROGRAM-16
OBJECTIVE : Write a c++ program to use an inline function for calculating the area of a
circle and a rectangle.
CODE :
#include <iostream.h>
#include <conio.h>
Page | 32
Programming C & C++ PGDCA : 2024-25
OUTPUT :
Page | 33
Programming C & C++ PGDCA : 2024-25
PROGRAM-17
OBJECTIVE : Write a c++ program to calculate the perimeter of a rectangle, ensuring the
dimensions remain constant in the function.
CODE :
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
float length, width;
getch();
}
OUTPUT :
Page | 34
Programming C & C++ PGDCA : 2024-25
PROGRAM-18
OBJECTIVE : Write a c++ program overloaded functions to calculate the area of a circle,
rectangle, and triangle.
CODE :
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
float radius, length, width, base, height;
Page | 35
Programming C & C++ PGDCA : 2024-25
cout << "\nEnter radius of the circle: ";
cin >> radius;
cout << "Area of the circle: " << area(radius) << "\n";
getch();
}
OUTPUT :
Page | 36
Programming C & C++ PGDCA : 2024-25
PROGRAM-19
OBJECTIVE : Write a c++ program to create a class Rectangle with data members for
length and width, and member functions to calculate and display the area
and perimeter.
CODE :
#include <iostream.h>
#include <conio.h>
class Rectangle
{
private:
float length, width;
public:
// Constructor to initialize length and width
Rectangle(float l, float w)
{
length = l;
width = w;
}
Page | 37
Programming C & C++ PGDCA : 2024-25
// Function to display area and perimeter
void display()
{
cout << "Area of Rectangle: " << area() << "\n";
cout << "Perimeter of Rectangle: " << perimeter() << "\n";
}
};
void main()
{
clrscr();
float l, w;
getch();
}
OUTPUT :
Page | 38
Programming C & C++ PGDCA : 2024-25
PROGRAM-20
OBJECTIVE : Write a c++ program to store and display details of 5 employees (ID, name,
salary) using an array of objects.
CODE :
#include <iostream.h>
#include <conio.h>
class Employee
{
public:
int id;
char name[50];
float salary;
// Function to take employee details
void input()
{
cout << "Enter Employee ID: ";
cin >> id;
cout << "Enter Employee Name: ";
cin >> name;
cout << "Enter Employee Salary: ";
cin >> salary;
}
// Function to display employee details
void display()
{
cout << "Employee ID: " << id << "\n";
cout << "Employee Name: " << name << "\n";
cout << "Employee Salary: " << salary << "\n";
}
};
Page | 39
Programming C & C++ PGDCA : 2024-25
void main()
{
clrscr();
int i;
Employee employees[5];
// Input details for 5 employees
for (i = 0; i < 5; i++)
{
cout << "\nEnter details for Employee " << (i + 1) << ":\n";
employees[i].input();
}
// Display details of 5 employees
cout << "\nEmployee Details:\n";
for (i = 0; i < 5; i++)
{
cout << "\nEmployee " << (i + 1) << ":\n";
employees[i].display();
}
getch();
}
Page | 40
Programming C & C++ PGDCA : 2024-25
OUTPUT :
Page | 41
Programming C & C++ PGDCA : 2024-25
PROGRAM-21
OBJECTIVE : Write a c++ program where a friend function calculates the average of
marks of a student, accessing private members of a Student class.
CODE :
#include <iostream.h>
#include <conio.h>
class Student
{
private:
char name[50];
int marks[3];
public:
void input()
{
cout << "\nEnter student name: ";
cin >> name;
cout << "Enter marks in 3 subjects: ";
for (int i = 0; i < 3; i++)
{
cin >> marks[i];
}
}
void display()
{
cout << "Student Name: " << name << "\n";
cout << "Marks: ";
for (int i = 0; i < 3; i++)
{
cout << marks[i] << " ";
}
cout << "\n";
}
Page | 42
Programming C & C++ PGDCA : 2024-25
// Friend function declaration
friend float calculateAverage(Student s);
};
// Friend function definition
float calculateAverage(Student s)
{
int sum = 0;
for (int i = 0; i < 3; i++)
{
sum += s.marks[i];
}
return sum / 3.0;
}
void main()
{
clrscr();
Student s;
s.input();
s.display();
cout << "Average Marks: " << calculateAverage(s) << "\n";
getch();
}
OUTPUT :
Page | 43
Programming C & C++ PGDCA : 2024-25
PROGRAM-22
OBJECTIVE : Write a c++ program to create a class Circle with a parameterized
constructor to initialize the radius and calculate the area.
CODE :
#include <iostream.h>
#include <conio.h>
const float PI =
3.14159; class Circle
{
private:
float radius;
public:
// Parameterized constructor to initialize radius
Circle(float r)
{
radius = r;
}
// Function to calculate area
float area()
{
return PI * radius * radius;
}
// Function to display area
void display()
{
cout << "Radius: " << radius << "\n";
cout << "Area of Circle: " << area() << "\n";
}
};
Page | 44
Programming C & C++ PGDCA : 2024-25
void main()
{
clrscr();
float r;
OUTPUT :
Page | 45
Programming C & C++ PGDCA : 2024-25
PROGRAM-23
OBJECTIVE : Write a c++ program to copy the details of a student (roll number, name,
marks) from one object to another using a copy constructor.
CODE :
#include <iostream.h>
#include <conio.h>
#include <string.h>
class Student
{
private:
int rollNumber;
char name[50];
float marks;
public:
// Parameterized constructor to initialize student details
Student(int r, char n[], float m)
{
rollNumber = r;
strcpy(name, n);
marks = m;
}
// Copy constructor
Student(const Student &s)
{
rollNumber = s.rollNumber;
strcpy(name, s.name);
marks = s.marks;
}
// Function to display student details
void display()
{
cout << "Roll Number: " << rollNumber << "\n";
Page | 46
Programming C & C++ PGDCA : 2024-25
cout << "Name: " << name << "\n";
cout << "Marks: " << marks << "\n";
}
};
void main()
{
clrscr();
int roll;
char name[50];
float marks;
Page | 47
Programming C & C++ PGDCA : 2024-25
OUTPUT :
Page | 48
Programming C & C++ PGDCA : 2024-25
PROGRAM-24
OBJECTIVE : Write a c++ program to count the number of objects created and destroyed
using constructors and destructors.
CODE :
#include <iostream.h>
#include <conio.h>
class Counter
{
private:
static int count;
public:
// Constructor to increment count
Counter()
{
count++;
cout << "Object Created. Total Objects: " << count << "\n";
}
// Destructor to decrement count
~Counter()
{
cout << "Object Destroyed. Remaining Objects: " << --count << "\n";
}
// Function to display current object count
static void displayCount()
{
cout << "Current Object Count: " << count << "\n";
}
};
Page | 49
Programming C & C++ PGDCA : 2024-25
int Counter::count = 0;
void main()
{
clrscr();
cout << "Creating Objects...\n";
Counter c1, c2;
Counter::displayCount();
{
Counter c3;
Counter::displayCount();
} // c3 goes out of scope and gets destroyed
Counter::displayCount();
getch();
}
OUTPUT :
Page | 50
Programming C & C++ PGDCA : 2024-25
PROGRAM-25
OBJECTIVE : Write a c++ program to overload the unary ++ and -- operator to increment
and decrement the value of a private data member.
CODE :
#include <iostream.h>
#include <conio.h>
class Counter
{
private:
int value;
public:
// Constructor to initialize value
Counter(int v = 0)
{
value = v;
}
Page | 51
Programming C & C++ PGDCA : 2024-25
void display()
{
cout << "Value: " << value << "\n";
}
};
void main()
{
clrscr();
Counter c(5);
++c; // Increment
cout << "After Increment: ";
c.display();
--c; // Decrement
cout << "After Decrement: ";
c.display();
getch();
}
OUTPUT :
Page | 52
Programming C & C++ PGDCA : 2024-25
PROGRAM-26
OBJECTIVE : Write a c++ program to overload the + operator to add two complex numbers
using a class Complex.
CODE :
#include <iostream.h>
#include <conio.h>
class Complex
{
private:
float real, imag;
public:
// Constructor to initialize complex number
Complex(float r = 0, float i = 0)
{
real = r;
imag = i;
}
Page | 53
Programming C & C++ PGDCA : 2024-25
void main()
{
clrscr();
float r1, i1, r2, i2;
cout << "\nEnter real and imaginary parts of first complex number: ";
cin >> r1 >> i1;
cout << "\nEnter real and imaginary parts of second complex number: ";
cin >> r2 >> i2;
getch();
}
OUTPUT :
Page | 54
Programming C & C++ PGDCA : 2024-25
PROGRAM-27
OBJECTIVE : Write a c++ program to overload the * operator to multiply two matrices
using a class Matrix.
CODE :
#include <iostream.h>
#include <conio.h>
class Matrix
{
private:
int mat[3][3];
public:
// Function to input matrix elements
void input()
{
cout << "Enter elements of 3x3 matrix:\n";
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> mat[i][j];
}
}
}
// Overloading * operator to multiply two matrices
Matrix operator*(Matrix m)
{
Matrix temp;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Page | 55
Programming C & C++ PGDCA : 2024-25
temp.mat[i][j] = 0;
for (int k = 0; k < 3; k++)
{
temp.mat[i][j] += mat[i][k] * m.mat[k][j];
}
}
}
return temp;
}
// Function to display matrix
void display()
{
cout << "Matrix:\n";
for (int i = 0; i < 3; i+
+)
{
for (int j = 0; j < 3; j++)
{
cout << mat[i][j] << " ";
}
cout << "\n";
}
}
};
void main()
{
clrscr();
Matrix m1, m2, result;
Page | 56
Programming C & C++ PGDCA : 2024-25
m2.input();
result = m1 * m2; // Matrix multiplication using overloaded * operator
cout << "\nResultant Matrix after multiplication:\n";
result.display();
getch();
}
OUTPUT :
Page | 57
Programming C & C++ PGDCA : 2024-25
PROGRAM-28
OBJECTIVE : Write a c++ program to create a class String and overload the + operator to
concatenate two strings.
CODE :
#include <iostream.h>
#include <conio.h>
#include <string.h>
class String
{
private:
char str[100];
public:
// Constructor to initialize string
String()
{
str[0] = '\0';
}
Page | 58
Programming C & C++ PGDCA : 2024-25
strcat(temp.str, s.str);
return temp;
}
void main()
{
clrscr();
String s1, s2, result;
getch();
}
Page | 59
Programming C & C++ PGDCA : 2024-25
OUTPUT :
Page | 60
Programming C & C++ PGDCA : 2024-25
PROGRAM-29
OBJECTIVE : Write a c++ program to create a class Distance that converts a float value
representing kilometers into meters and centimeter.
CODE :
#include <iostream.h>
#include <conio.h>
class Distance
{
private:
float kilometers;
public:
// Constructor to initialize distance
Distance(float km)
{
kilometers = km;
}
Page | 61
Programming C & C++ PGDCA : 2024-25
void display()
{
cout << "Distance in kilometers: " << kilometers << " km\n";
cout << "Distance in meters: " << toMeters() << " m\n";
cout << "Distance in centimeters: " << toCentimeters() << " cm\n";
}
};
void main()
{
clrscr();
float km;
Distance d(km);
d.display();
getch();
}
OUTPUT :
Page | 62
Programming C & C++ PGDCA : 2024-25
PROGRAM-30
OBJECTIVE : Write a c++ program to convert objects of a Fahrenheit class to a Celsius class
using operator overloading.
CODE :
#include <iostream.h>
#include <conio.h>
class Celsius;
class Fahrenheit
{
private:
float fahrenheit;
public:
Fahrenheit(float f)
{
fahrenheit = f;
}
float getFahrenheit()
{
return fahrenheit;
}
void display()
{
cout << "Temperature in Fahrenheit: " << fahrenheit << " F\n";
}
};
class Celsius
Page | 63
Programming C & C++ PGDCA : 2024-25
{
private:
float celsius;
public:
Celsius() {}
Celsius(Fahrenheit f)
{
celsius = (f.getFahrenheit() - 32) * 5 / 9;
}
void display()
{
cout << "Temperature in Celsius: " << celsius << " C\n";
}
};
void main()
{
clrscr();
float tempF;
Fahrenheit f(tempF);
Celsius c = f; // Conversion using operator overloading
f.display();
c.display();
getch();
}
Page | 64
Programming C & C++ PGDCA : 2024-25
OUTPUT :
Page | 65
Programming C & C++ PGDCA : 2024-25
PROGRAM-31
OBJECTIVE : Write a c++ program to demonstrate multilevel inheritance with a base class
Shape (basic properties), derived class Rectangle (length and width), and
further derived class Cuboid (height).
CODE :
#include <iostream.h>
#include <conio.h>
class Shape
{
protected:
float area;
public:
void displayArea()
{
cout << "Area: " << area << "\n";
}
};
Page | 66
Programming C & C++ PGDCA : 2024-25
displayArea();
}
};
void main()
{
clrscr();
float l, w, h;
Page | 67
Programming C & C++ PGDCA : 2024-25
cout << "\nEnter height of the cuboid: ";
cin >> h;
Cuboid c(l, w, h);
c.display();
getch();
}
OUTPUT :
Page | 68
Programming C & C++ PGDCA : 2024-25
PROGRAM-32
OBJECTIVE : Write a c++ program to handle data from two base classes Person (personal
information) and Employee (job details) using a derived class Manager.
CODE :
#include <iostream.h>
#include <conio.h>
class Person
{
protected:
char name[50];
int age;
public:
void getPersonalInfo()
{
cout << "Enter Name: ";
cin >> name;
cout << "Enter Age: ";
cin >> age;
}
void displayPersonalInfo()
{
cout << "Name: " << name << "\nAge: " << age << "\n";
}
};
class Employee
{
protected:
int empID;
float salary;
public:
Page | 69
Programming C & C++ PGDCA : 2024-25
void getJobDetails()
{
cout << "Enter Employee ID: ";
cin >> empID;
cout << "Enter Salary: ";
cin >> salary;
}
void displayJobDetails()
{
cout << "Employee ID: " << empID << "\nSalary: " << salary << "\n";
}
};
Page | 70
Programming C & C++ PGDCA : 2024-25
void main()
{
clrscr();
Manager m;
cout << "\nEnter Manager Details:\n";
m.getManagerDetails();
getch();
}
OUTPUT :
Page | 71
Programming C & C++ PGDCA : 2024-25
PROGRAM-33
OBJECTIVE : Write a c++ program to demonstrate hierarchical inheritance where a base
class Animal has derived classes Dog, Cat, and Bird, each with specific
methods.
CODE :
#include <iostream.h>
#include <conio.h>
class Animal
{
public:
void makeSound()
{
cout << "Some generic animal sound\n";
}
};
class Dog : public Animal
{
public:
void makeSound()
{
cout << "Dog barks: Woof! Woof!\n";
}
};
class Cat : public Animal
{
public:
void makeSound()
{
cout << "Cat meows: Meow! Meow!\n";
}
};
Page | 72
Programming C & C++ PGDCA : 2024-25
class Bird : public Animal
{
public:
void makeSound()
{
cout << "Bird chirps: Chirp! Chirp!\n";
}
};
void main()
{
clrscr();
Dog d;
Cat c;
Bird b;
cout << "Dog Sound:\n";
d.makeSound();
cout << "Cat Sound:\n";
c.makeSound();
cout << "Bird Sound:\n";
b.makeSound();
getch();
}
OUTPUT :
Page | 73
Programming C & C++ PGDCA : 2024-25
PROGRAM-34
OBJECTIVE : Write a c++ program to create an abstract class Shape with a pure virtual
function calculateArea() and derived classes Circle and Rectangle that
implement the function.
CODE :
#include <iostream.h>
#include <conio.h>
class Shape
{
public:
virtual float calculateArea() = 0; // Pure virtual function
};
void main()
{
clrscr();
float r, l, w;
getch();
}
Page | 75
Programming C & C++ PGDCA : 2024-25
OUTPUT :
Page | 76
Programming C & C++ PGDCA : 2024-25
PROGRAM-35
OBJECTIVE : Write a c++ program to demonstrate the use of a pointer to an object by
creating a class Student with data members for roll number and name. Use a
pointer to access and modify object data.
CODE :
#include <iostream.h>
#include <conio.h>
class Student
{
private:
int rollNumber;
char name[50];
public:
void setDetails()
{
cout << "Enter Roll Number: ";
cin >> rollNumber;
cout << "Enter Name: ";
cin >> name;
}
void displayDetails()
{
cout << "Roll Number: " << rollNumber << "\n";
cout << "Name: " << name << "\n";
}
};
void main()
{
clrscr();
Student *ptr, s;
ptr = &s;
Page | 77
Programming C & C++ PGDCA : 2024-25
getch();
}
OUTPUT :
Page | 78
Programming C & C++ PGDCA : 2024-25
PROGRAM-36
OBJECTIVE : Write a c++ program to use the this pointer to return the current object from
member functions in a ChainedFunction class that supports method chaining.
CODE :
#include <iostream.h>
#include <conio.h>
class ChainedFunction
{
private:
int value;
public:
ChainedFunction() : value(0) {}
ChainedFunction& setValue(int v)
{
value = v;
return *this;
}
ChainedFunction& increment()
{
value++;
return *this;
}
Page | 79
Programming C & C++ PGDCA : 2024-25
void display()
{
cout << "\nValue is : " << value << "\n";
}
};
void main()
{
clrscr();
ChainedFunction obj;
obj.setValue(5).increment().multiply(3).display();
getch();
}
OUTPUT :
Page | 80