C++ lab manual
C++ lab manual
Science Engineering
LABORATORY MANUAL
REVISION: 001
Lab Manual
USN:
Name:
2
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
CERTIFICATE
This is to certify that Mr./Ms. ___________________________________________________________________
_____________________________________________________________________________________ prescribed by
Lab Test
Viva Voce
CIE Marks
3
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Content Sheet
Sl. Page
List of Experiments
No Number
1. Lab Programs -1 6
2. Lab Programs -2 10
3. Lab Programs -3 14
4. Lab Programs -4 17
5. Lab Programs -5 22
6. Lab Programs -6 27
7. Lab Programs -7 31
8. Lab Programs -8 33
9. Lab Programs -9 35
4
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
INDEX
Date of Signature
Marks of
Sl.
List of Experiments Experiment
No
Obtained the
Faculty
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
5
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 1
#include <iostream>
using namespace std;
#define PI 3.141
int main()
{
float radius, area;
cout << "Enter radius of circle\n";
cin >> radius;
// Area of Circle = PI x Radius X Radius
area = PI*radius*radius;
cout << "Area of circle : " << area;
return 0;
}
OUTPUT
6
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 1
#include<iostream>
OUTPUT
7
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 1
c) Write a C++ program to find the area of a triangle given its sides
{
float s1,s2,s3,s,area;
cout<<” enter the three sides of a triangle\n”;
cin>>s1>>s2>>s3;
s = (s1 + s2 + s3)/2;
area = sqrt(s*(s - s1)*(s - s2)*(s - s3));
cout <<"Area of a triangle given its sides are= " << endl<<area;
OUTPUT:
8
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 1
d) Write a C++ program to get the name, age and salary of a person and display the
same
int main()
{
char name[10];
int age;
float salary;
return 0;
}
OUTPUT:
9
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 2
2 a) Write a C++ program to find the factorial of a number
int main()
int n;
long factorial = 1.0;
if (n < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else
{
for(int i = 1; i <= n; ++i)
{
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
OUTPUT:
10
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 2
b) Write a C++ program to find whether the entered number is palindrome or not.
#include <iostream>
using namespace std;
int main()
{
int n, num, digit, rev = 0;
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
cout << " The reverse of the number is: " << rev << endl;
if (n == rev)
cout << " The number is a palindrome.";
else
cout << " The number is not a palindrome.";
return 0;
}
OUTPUT:
11
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 2
c) Write a C++ program to find the sum of all the natural numbers from 1 to n.
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a number : ";
cin >> n;
int sum=0;
for(int i=1;i<=n;i++)
sum+=i;
return 0;
}
OUTPUT:
12
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 2
d) Write a C++ program to find sum of all the elements, maximum and minimum
element in an array
// Program on array
#include <iostream>
using namespace std;
int main()
{
int array[10];
int i, max, min, size, sum;
max = array[0];
min = array[0];
sum=sum+array[i];
}
return 0;
}
OUTPUT:
13
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 3
3 a)Write a C++ program to find whether an entered number is prime or not using a
function(with value, with return type)
#include <iostream>
return 0;
}
bool CheckPrime(unsigned long n)
{
unsigned long i;int flag = 1;
for(i = 2; i <= n/2; i++)
{
if(n % i == 0)
{
flag = 0;
cout<<"Number is not a Prime & divisible by "<<i<<endl;
return 0;
}
}
if(flag == 1)
return 1;
}
OUTPUT:
14
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 3
#include <iostream>
#define MAX 5
using namespace std;
void LinearSearch(int[],int);
int main()
{
cout << "Happy Programming!" << endl << endl;
int arr[MAX],Key;
cout << "Enter the elements of the array:"<<endl;
for(int i=0;i<MAX;i++)
{
cin>>arr[i];
}
cout << "Enter the element to find:"<<endl;
cin>>Key;
LinearSearch(arr,Key);
return 0;
}
void LinearSearch(int parr[],int k)
{
int flag = 0;
for(int i=0;i<MAX;i++)
{
if(parr[i]==k)
{
flag = 1;
cout<<"Number is found at location "<<i+1<<endl;
break;
}
}
if(flag == 0)
cout<<"Number not found in array "<<endl;
}
OUTPUT:
15
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 3
c) Write a C++ program to swap 2 values by writing a function that uses call by reference
technique.
#include <iostream>
return 0;
}
void Swap(int &x, int &y)
{
int temp=x;
x=y;
y=temp;
}
OUTPUT:
16
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 4
Method1:
using namespace std;
inline void square(int numb);
int main()
{
cout << "Hello world!" << endl;
int numb ;
cout<<" Enter a numb to find square"<<endl;
cin>>numb;
cout<<"\nSquare of "<<numb<<" is \t ";
square(numb);
return 0;
}
void square(int numb)
{
cout<<numb*numb<<endl;
}
Method 2:
#include <iostream>
public:
inline void get_numb()
{
cout<<" Enter a numb to find square"<<endl;
cin>>numb;
}
inline void disp_square()
{
res = numb *numb;
cout<<"\nSquare of "<<numb<<" is \t " <<res<<endl;
}
};
17
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
int main()
{
cout << "Hello world!" << endl;
Square S;
S.get_numb();
S.disp_square();
return 0;
}
Method 3:
#include <iostream>
using namespace std;
class Square
{
int numb;
int res;
public:
void get_numb();
void disp_square();
};
int main()
{
cout << "Hello world!" << endl;
Square S;
S.get_numb();
S.disp_square();
return 0;
}
inline void Square::get_numb()
{
cout<<" Enter a numb to find square"<<endl;
cin>>numb;
}
inline void Square::disp_square()
{
res = numb *numb;
cout<<"\nSquare of "<<numb<<" is \t " <<res<<endl;
}
OUTPUT:
18
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 4
b) Write a C++ program to create a class called bank_acct with following data member
(cust_name, cust_accno, balance) and member functions (read_details, deposit,
withdraw, display balance). Read and display details using array of objects and
implement deposit and withdraw using inline.
#include <iostream>
#include <iomanip>
#include <string.h>
public:
bank_acct()
{
cust_name[0] ='\0';
cust_accno[0] ='\0';
balance = 500;
}
void read_details();
void deposit();
void withdraw();
void display_balance();
void display_details();
void searchAcc(char[],char);
};
if(wdamt>balance)
cout<<"\n Cannot Withdraw Amount";
balance-=wdamt;
}
inline void bank_acct :: searchAcc(char AccNum[],char op)
{
if(!(strcmp(cust_accno,AccNum)))
{
if(op == 'd')
deposit();
else
withdraw();
display_balance();
}
}
inline void bank_acct :: display_balance()
{
cout<<" The balance amount of "<< cust_name<<endl;
display_details();
}
inline void bank_acct :: display_details()
{
cout<<setw(30)<<"Customer Name: "<<setw(30)<<cust_name<<endl;
cout<<setw(30)<<"Account Number: "<<setw(30)<<cust_accno<<endl;
cout<<setw(30)<<"Balance: "<<setw(30)<<balance<<endl;
}
int main()
{
cout << "Happy Programming!" << endl;
char ch = 'y',c='s';
int i=0;
20
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
char AccNo[30];
bank_acct B[10];
}while(ch!='n');
return 0;
}
OUTPUT:
21
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 5
5 a) Write and execute a C++ Program to display names, roll no’s, and grades of 3
students who have appeared in the examination. Create a class with data members as
Name, Roll no and Marks for 3 subjects. Write a method to calculate the grade of a
student.
#include <iostream>
#include <iomanip>
class STUDENT
{
char Name[30];
char RollNo[30];
int Sub1Mark;
int Sub2Mark;
int Sub3Mark;
char grade;
public:
void getdetail();
void calculategrade();
void displaydetail();
};
}
void STUDENT :: calculategrade()
{
float avg = (Sub1Mark+Sub2Mark+Sub3Mark)/3;
if(avg>=91 && avg <= 100)
22
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
grade = 'S';
else if(avg>=81 && avg <= 90)
grade = 'A';
else if(avg>=71 && avg <= 80)
grade = 'B';
else if(avg>=61 && avg <= 70)
grade = 'C';
else if(avg>=51 && avg <= 60)
grade = 'D';
else if(avg>=40 && avg <= 50)
grade = 'E';
else
grade = 'F';
}
void STUDENT ::displaydetail()
{
calculategrade();
for(int i=0;i<10;i++)
cout<<"*\t";
cout<<endl;
for(int i=0;i<10;i++)
cout<<"*\t";
cout<<endl;
}
int main()
{
cout << "Happy Programming!" << endl;
STUDENT S[3];
char ch = 'y'; int i=0;
do
{
S[i].getdetail();
23
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
i++;
cout<<"Do you want to details of another Student type 'y' or 'n'"<<endl;
cin>>ch;
cout<<endl;
}while(ch!='n' && i<3);
for(int j=0;j<i;j++)
{
S[j].displaydetail();
}
return 0;
}
OUTPUT:
24
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 5
#include <iostream>
#include <string.h>
#include <iomanip>
#define MAX 80
using namespace std;
class STRING
{
char str[MAX];
public:
STRING()
{
str[0]='\0';
}
STRING(const char pstr[])
{
strcpy(str,pstr);
}
STRING strconcat(STRING X)
{
STRING temp;
if(strlen(str)+strlen(X.str)<MAX)
{
strcpy(temp.str,str);
strcat(temp.str,X.str);
}
return temp;
}
void display()
{
cout<<setw(30)<<"The string is:\t"<<str<<endl;
}
};
int main()
{
25
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
26
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 6
Per
son
Tea Stu
che den
r t
Mar
ks
Assume suitable data members and member functions for all the classes.
Display the number of publications for a teacher and percentage marks for a
student.
#include <iostream>
#include <iomanip>
#define MAX 80
using namespace std;
class Person
{
private:
char Name[MAX];
char AadharNum[MAX];
char DOB[MAX];
public:
void GetPersonInfo()
{
cout<<"\n Enter the Name: \t";cin>>Name;
cout<<"\n Enter the Aadhar Number: \t";cin>>AadharNum;
cout<<"\n Enter the DOB in DD-MM-YYYY: \t";cin>>DOB;
}
void DisplayPersonInfo()
{
cout<<setw(30)<<" Name: "<<setw(30)<<Name<<endl;
cout<<setw(30)<<" Aadhar Number: "<<setw(30)<<AadharNum<<endl;
cout<<setw(30)<<" DOB: "<<setw(30)<<DOB<<endl;
27
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
}
};
class Teacher : public Person
{
char Department[MAX];
char Designation[MAX];
int Publication;
public:
void GetTeacherinfo();
void DisplayTeacherInfo();
};
class Student : public Person
{
char Department[MAX];
char RollNo[MAX];
public:
void GetStudentInfo();
void DisplayStudentInfo();
};
class Marks : public Student
{
int Sub1Mark;
int Sub2Mark;
int Sub3Mark;
public:
void GetStudentMarks();
void DisplayStudentMarks();
};
/* Member Function Definitions for class Marks*/
void Marks :: GetStudentMarks()
{
cout<<"\n Enter the Student Subject_1 Mark: \t";cin>>Sub1Mark;
cout<<"\n Enter the Student Subject_2 Mark: \t";cin>>Sub2Mark;
cout<<"\n Enter the Student Subject_3 Mark: \t";cin>>Sub3Mark;
}
void Marks :: DisplayStudentMarks()
{
cout<<setw(30)<<"Student Subject_1 Mark: "<<setw(30)<<Sub1Mark<<endl;
cout<<setw(30)<<"Student Subject_2 Mark: "<<setw(30)<<Sub2Mark<<endl;
cout<<setw(30)<<"Student Subject_3 Mark: "<<setw(30)<<Sub3Mark<<endl;
cout<<setw(30)<<"Percentage:
"<<setw(30)<<((Sub1Mark+Sub2Mark+Sub3Mark)/3)<<"%"<<endl;
28
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
}
/* Member Function Definitions for class Student*/
void Student :: GetStudentInfo()
{
GetPersonInfo();
cout<<"Enter the Department: \t";cin>>Department;
cout<<"\n Enter the Roll Number: \t";cin>>RollNo;
}
void Student ::DisplayStudentInfo()
{
DisplayPersonInfo();
cout<<setw(30)<<" Department: "<<setw(30)<<Department<<endl;
cout<<setw(30)<<" Roll Number: "<<setw(30)<<RollNo<<endl;
}
/* Member Function Definitions for class Teacher*/
void Teacher :: GetTeacherinfo()
{
Person::GetPersonInfo();
cout<<"Enter the Department: \t";cin>>Department;
cout<<"\n Enter the Designation: \t";cin>>Designation;
cout<<"\n Enter the Number of Publication: \t";cin>>Publication;
}
void Teacher :: DisplayTeacherInfo()
{
DisplayPersonInfo();
cout<<setw(30)<<" Department: "<<setw(30)<<Department<<endl;
cout<<setw(30)<<" Designation: "<<setw(30)<<Designation<<endl;
cout<<setw(30)<<" Number of Publication: "<<setw(30)<<Publication<<endl;
}
/* Main Function */
int main()
{
cout << "Happy Programming!" << endl<<endl;
Teacher T[3];
char ch = 'y'; int i=0;
Marks S[3];
ch = 'y'; i=0;
do
{
S[i].GetStudentInfo();
S[i].GetStudentMarks();
i++;
cout<<"Do you want to details of another Student type 'y' or 'n'"<<endl;
cin>>ch;
cout<<endl;
}while(ch!='n' && i<3);
return 0;
}
OUTPUT:
30
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 7
So, if we invoke the methods in this order, car(), fourWheeler(), and vehicle(), then the
output will be
I am a car
I have four wheels
I am a vehicle
#include <iostream>
};
int main()
{
cout << "Happy Programming!" << endl;
Car Cobj;
Cobj.car();
Cobj.fourwheeler();
Cobj.vehicle();
return 0;
}
OUTPUT:
32
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 8
8 a) Write a C++ program to overload function for computing the area triangle, circle and
square
#include <iostream>
#include <iomanip>
using namespace std;
void area(float,float);
void area(float,double);
void area(float);
int main()
{
cout << "Happy Programming! Demonstrating Function Overloading" << endl<<endl;
float height,base;
cout<<"Calculate Area of Triangle for the following measurements"<<endl;
cout<<"\n Enter the Height of Triangle in cms: \t";cin>>height;
cout<<"\n Enter the Base of Triangle in cms: \t";cin>>base;
area(height,base);
float radius;
cout<<"Calculate Area of Circle for the following measurements"<<endl;
cout<<"\n Enter the radius of Circle in cms: \t";cin>>radius;
area(radius,3.14);
float side;
cout<<"Calculate Area of Square for the following measurements"<<endl;
cout<<"\n Enter the side of Square in cms: \t";cin>>side;
area(side);
return 0;
}
void area(float h,float b)
{
cout<<setw(30)<<"Area of Triangle:"<<setw(30)<<(0.5*h*b)<<endl;
}
void area(float r,double C)
{
cout<<setw(30)<<"Area of Circle:"<<setw(30)<<(C*r*r)<<endl;
}
void area(float s)
{
cout<<setw(30)<<"Area of Square:"<<setw(30)<<(s*s)<<endl;
}
33
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 8
b) Write a C++ program to overload a function to add two numbers of different data
types (int, float, double)
#include <iostream>
#include<iomanip>
void add(int,int);
void add(float,float);
int main()
{
cout << "Happy Programming! Demonstrating Function Overloading" <<
endl<<endl;
int num1,num2;
cout<<"Addition of 2 numbers by calling function add(int,int);"<<endl;
cout<<"\n Enter the First Number: \t";cin>>num1;
cout<<"\n Enter the Second Number: \t";cin>>num2;
add(num1,num2);
float fnum1,fnum2;
cout<<"Addition of 2 numbers by calling function void add(float,float);"<<endl;
cout<<"\n Enter the First Number: \t";cin>>fnum1;
cout<<"\n Enter the Second Number: \t";cin>>fnum2;
add(fnum1,fnum2);
return 0;
}
void add(int n1,int n2)
{
cout<<setw(30)<<" Result of Addition of int numbers"<<setw(30)<<(n1+n2)
<<endl;
}
void add(float fn1,float fn2)
{
cout<<setw(30)<<" Result of Addition of float numbers"<<setw(30)<<(fn1+fn2)
<<endl;
}
OUTPUT:
34
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 9
9 a) Write a C++ program to create a text file, check file created or not, if created write some text
into the file and then read the text from the file.
// C++ program to demonstrate file open, read, write and close operations
#include <iostream>
#include <fstream>
int main(){
char text[200];
fstream file;
file.open ("example.txt", ios::out | ios::in );
if(!file)
{
cout<<"Error in creating file!!!"<<endl;
return 0;
}
// Writing on file
file << text << endl;
OUTPUT:
35
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 9
b) Write a C++ program to read the contents from a text file, count and display the
number of alphabets present in it.
OUTPUT:
36
PLC 144 - INTRODUCTION TO C++ PROGRAMMING
Lab Question 10
10 a) Write a C++ program that creates a Calculator class. The class contains two variables of
integer type. Design a constructor that accepts two values as parameter and set those values.
Design four methods named Add (), Subtract (), multiply (), Division ( ) for performing
addition, subtraction, multiplication and division of two numbers.
For addition and subtraction, two numbers should be positive. If any negative number is
entered then throw an exception in respective methods. So design an exception handler
(ArithmeticException) in Add () and Subtract () methods respectively to check whether any
number is negative or not.
For division and multiplication two numbers should not be zero. If zero is entered for any
number then throw an exception in respective methods. So design an exception handler
(ArithmeticException) in multiply () and Division () methods respectively to check whether
any number is zero or not.
return (a-b);
}
int main () {
int x = 50;
int y = 0;
int z = 0;
try {
z = division(x, y);
cout << z << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
try {
z = product(x, y);
cout << z << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
try {
z = add(x, y);
cout << z << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
try {
z = sub(x, y);
cout << z << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
38