CPP Lab Ramamoorthy
CPP Lab Ramamoorthy
PRACTICAL RECORD
Name: Ramamoorthy.K
Reg.No: 2403727714921075
Class/Section: CYBER - B
BONAFIDE CERTIFICATE
2025(ODD SEM)
2.
Programs using arrays and strings
3.
Programs using functions
4.
Programs using Structures and pointers
5.
Programs using classes and objects
6.
Programs using constructors and destructors
8.
Programs using friend class
9.
Programs using virtual function and abstract class
10.
Programs using inheritance concepts
11.
Programs using exception handling concepts
12.
Programs using files
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Rubrics for Evaluating Laboratory
Subject Code : 23CS101
a) Graduates will demonstrate knowledge of mathematical, scientific and multidisciplinary approach for
problem solving.
b) Graduates will be able to apply their knowledge in various programming skills to create solutions for
product based and application based software.
c) Graduates will possess the ability to create real time solutions for different projects by using modern tools
prevailing in the current trends.
e) Graduates attain advanced knowledge in the stream of Computer Science and Engineering to develop and
maintain the simple and complex information systems.
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
Reg No:
Ex-1 Ex-2 Ex- Ex- Ex- Ex- Ex- Ex- Ex- Ex-
3 4 5 6 7 8 9 10
Coding
30 Marks
Compilation
&
Debuggin
g 30
Marks
Execution &
Results
10 Marks
Documentation &
Viva
10 Marks
Total
Staff In-charge
Ex No: 1 Practice of C programming using Branching and iterative constructs
AIM:
To write a C++ program that prints the given number pattern using nested loops .
ALGORITHM:
1. Start.
2. Initialize a variable num = 1 to hold the current number to be printed.
3. Loop through the rows:
● Use a for loop to iterate over rows, where i represents the current row number.
● The number of elements in each row is equal to the current row number i.
4. Loop through the columns within each row:
● Use another for loop inside the first loop to control the number of columns (elements)
printed in each row. The loop runs from 1 to i.
● In each iteration of the inner loop, print the current value of num, then increment num
by 1.
5. After printing each row, move to the next line.
6. End.
PROGRAM:
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int k=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
cout<<k<<" ";
k++;
}
cout<<"\n";
}
return 0;
}
Sample 1 Input
4
Sample 1 Output
1
23
456
7 8 9 10
RESULT:
Thus, the program prints the number pattern in Right angled triangle pattern using nested
loops.
Ex No: 2 Programs using arrays and strings
AIM:
To find the value that occurs the most in a given array filled with values 1, 2, or 3. In the case of a tie,
the smallest value among the most frequent ones is selected.
ALGORITHM:
1. Input: A list (array) consisting of numbers where each element is either 1, 2, or 3.
2. Initialize Counters:
● Set up three counters: count_1, count_2, and count_3 to track occurrences of the values 1, 2,
and 3, respectively.
3. Count the Occurrences:
● Traverse the array using a loop.
● For each element, increase the respective counter based on whether the element is 1, 2, or
3.
4. Compare the Counts:
● Compare the values of count_1, count_2, and count_3.
● If one count is greater than the others, that value is the result.
● If there is a tie (two or more counts are equal), choose the smallest value among them.
5. Output: Print the value that occurs the most or the smallest value in case of a tie.
PROGRAM:
#include <iostream>
using namespace std;
int main() {
int size;
cin>>size;
int arr[size],c1=0,c2=0,c3=0;
for(int ind=0;ind<size;ind++)
{
cin>>arr[ind];
if (arr[ind]==1)
c1+=1;
else if (arr[ind]==2)
c2+=1;
else if (arr[ind]==3)
c3+=1;
}
if (c1>=c2&&c1>=c3)
cout<<1;
else if(c2>=c3)
cout<<2;
else
cout<<3;
return 0;
}
Sample 1 Input
3
121
Sample 1 Output
1
RESULT:
The program successfully identifies the value that occurs the most in the array, and in the case of a tie,
it prints the smaller value.
AIM:
To reverse the digits of a given number using user-defined functions in C++.
ALGORITHM:
1. Input: Read a number from the user.
2. Define a Function:
● Create a user-defined function reverse(int n) to reverse the number.
3. Reverse Logic:
● Initialize a variable reversed to 0.
PROGRAM:
#include<iostream>
using namespace std;
int reverse (int n)
{
int reversed=0;
while(n!=0){
reversed=(reversed*10)+n%10;
n=n/10;
}
return reversed;
}
int main() {
int n;
cin>>n;
cout<<reverse(n);
}
Sample 1 Input
1234
Sample 1 Output
4321
RESULT:
The C++ program successfully reverses the digits of a given number using a user-defined function.
EX NO: 4 Programs using Structures and Pointers
AIM:
To write a C++ program to find the maximum element in an array using pointers.
ALGORITHM:
● Input: Read the size of the array and the array elements from the user.
● Initialize Pointers:
● Declare a pointer ptr to point to the first element of the array.
● Initialize a variable max to store the maximum value (initially assign the value of the first
element).
● Pointer Traversal:
● Use the pointer ptr to traverse the array elements.
● Compare the current element (*ptr) with max:
o If *ptr is greater than max, update max.
PROGRAM:
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int *ptr,max=0;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
*ptr=arr[i];
if(*ptr>max)
max=*ptr;}
cout<<max;
}
Sample 1 Input
5
2
3
6
8
1
Sample 1 Output
8 is the maximum element
RESULT:
The C++ program successfully finds the maximum element in an array using pointers.
AIM:
To write a C++ program that sorts objects (representing employees) based on their experience and
displays their details.
ALGORITHM:
● Define a Class:
Create a class Employee with attributes: name, experience, and salary.
● Input:
Read the details (name, experience, salary) of multiple employees from the user.
● Sorting:
Use a sorting algorithm to sort the employees based on their experience in descending
order.
● Display:
After sorting, display the details of the employees in the sorted order.
PROGRAM:
#include <iostream>
using namespace std;
class Employee
{
public:
string name;
string qualification;
string gender;
int experience;
void input()
{
cin >> ws;
getline(cin, name);
getline(cin, qualification);
getline(cin, gender);
cin >> experience;
}
void display() const
{
cout << name << endl;
cout << qualification << endl;
cout << experience << endl;
}
};
int main() {
int n;
cin >> n;
Employee employees[n];
sortEmployees(employees, n);
return 0;
}
Sample 1 Input
2
ram
Be cse
male
2
pravin
Be ece
male
3
Sample 1 Output
pravin
Be ece
3
ram
Be cse
2
RESULT:
The program successfully sorts the employee objects based on their experience in descending
order and displays their details.