Professional Documents
Culture Documents
PRACTICAL FILE
Source Code:
#include<iostream> using
namespace std; class prime {
int a, k, i; public:
prime(int x) {
a = x;
}
void calculate() {
k = 1;
{
for (i = 2; i <= a / 2; i++)
if (a % i == 0) {
k = 0;
break;
} else {
k = 1;
}
}
}
void output() {
if (k == 1)
cout << a << " is Prime.";
else
cout << a << " is not Prime.";
}
};
int main() {
int a;
cout << "Enter a number : "; cin>>a;
prime obj(a);
obj.calculate();
obj.output(); return 0;
}
Output :
EXPERIMENT 2
AIM : Write a program to take name, address as character string,
age as int, salary as float and contains inline function to set the values
and display it.
Source Code :
class info
{
public:
inline void input()
{
cout << "Enter name : ";
cin >> name;
cout << "Enter address : ";
cin >> address;
cout << "Enter age : ";
cin >> age;
cout << "Enter salary : ";
cin >> salary;
}
int main()
{
info obj;
obj.input();
obj.output();
Output :
EXPERIMENT 3
Source Code:
#include <iostream>
#define pi 3.14
using namespace std;
class CalcArea
{
public:
void area(int); void area(int,
int);
void area(float, int, int);
};
void CalcArea::area(int a)
{
main()
{
int ch;
int a, b, r;
CalcArea obj;
cout << "\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n";
cout << "Enter Choice : "; cin >> ch;
switch (ch)
{
case 1:
cout << "Enter Radius of the Circle : ";
cin >> r; obj.area(r); break;
case 2:
cout << "Enter Sides of Rectangle : ";
cin >> a >> b;
obj.area(a, b); break;
case 3:
cout << "Enter Sides of the Triangle : ";
cin >> a >> b;
obj.area(0.5, a, b); break;
case 4:
exit(0);
}
}
Output
EXPERIMENT 4
Source Code :
public:
float ptge;
void input();
void percent();
void output();
};
void student::percent()
{
float sum = 0;
for (int i = 0; i < 5; i++)
{
sum += marks[i];
}
ptge = (sum / 500) * 100;
}
void student::input()
{
cout << "Enter Name : ";
cin >> name;
cout << "Enter Branch : ";
cin >> branch;
cout << "Enter Roll No. : ";
cin >> rn;
cout << "Enter Age : ";
cin >> age;
cout << "Enter Sex : ";
cin >> sex;
for (int i = 0; i < 5; i++)
{
cout << "Enter marks in subject " << i + 1 << " : ";
cin >> marks[i];
}
}
void student::output()
{
cout << "Name : ";
puts(name);
cout << "Percentage : " << ptge << endl;
}
main()
{
student obj[10]; int n;
cout << "Enter no. of students : ";
cin >> n;
for (int i = 0; i < n; i++)
{
obj[i].input();
}
cout << endl;
for (int j = 0; j < n; j++)
{
obj[j].percent();
if (obj[j].ptge > 70)
{
obj[j].output();
}
}
Shubham Dhoundiyal 4C13 040964002719
\
Output :
Shubham Dhoundiyal 4C13 040964002719
\
EXPERIMENT 5
Source Code :
public:
void input();
void output();
void multiply();
};
void matrix::input()
{
cout << "Enter no. of rows and column for the first matrix: ";
cin >> row1 >> col1;
cout << "Enter the elements for first matrix : ";
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
cin >> a[i][j];
}
}
cout << "Enter the row size & column size of second matrix:";
cin >> row2 >> col2;
if (col1 == row2)
{
cout << "Enter the elements for" << row2 << "*" << col2 << "matrix:";
for (i = 0; i < row2; i++)
{
for (j = 0; j < col2; j++)
{
cin >> b[i][j];
}
}
multiply();
output();
}
else
cout << "Can't Multiply";
}
void matrix::multiply()
{
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
c[i][j] = 0;
for (k = 0; k < row2; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
}
void matrix::output()
{
cout << "After Matrix Multiplication : ";
for (i = 0; i < row1; i++)
{
cout << "\n";
for (j = 0; j < col2; j++)
{
cout << "\t" << c[i][j];
}
}
}
main()
{
matrix obj;
obj.input();
}
Output :
EXPERIMENT 6
Source Code :
public:
complex();
complex(double a);
complex(double x, double y);
void sum(complex c1, complex c2);
void output();
};
complex::complex()
{
r = 0.0;
i = 0.0;
}
complex::complex(double a)
{
r = a;
i = a;
}
complex::complex(double x, double y)
{
r = x;
i = y;
}
void complex::sum(complex c1, complex c2)
{
r = c1.r + c2.r;
i = c1.i + c2.i;
}
void complex::output()
{
if (i > 0)
{
else
} {
cout << r << "+" << i << "i" << endl;
}
}
Source Code :
public:
void input(void);
void output(void);
void calculate(Time t1, Time t2);
};
void Time::input(void)
{
cout << "Enter time :" << endl;
cout << "Hours : ";
cin >> hr;
cout << "Minutes : ";
cin >> min;
cout << "Seconds : ";
cin >> sec;
}
void Time::output(void)
{
cout << endl;
cout << "After Adding : ";
cout << hr << " : " << min << " : " << sec << endl;
}
int main()
{
Time t1, t2, T3;
t1.input();
t2.input();
T3.calculate(t1, t2);
T3.output();
return 0;
}
Output :
EXPERIMENT 8
Source Code :
#include<iostream> using
namespace std;
void Power(double m,int n=2)
{
double p=1;
for(int i=1;i<=n;i++)
{
p*=m;
}
cout<<m<<"^"<<n<<"="<<p<<endl;
}
main()
{
double m; int
n;
cout<<"Enter Power : "; cin>>n;
cout<<"Enter base : "; cin>>m;
cout<<"Not using default value : "<<endl; Power(m,n);
cout<<"Using default value : "<<endl; Power(m);
}
Output :
EXPERIMENT 9
AIM : Write a program to enter any number and find its factorial using
constructor.
Source Code :
#include<iostream> using
namespace std; class factorial
{
int num, f, i; public:
factorial() {
cout << "Enter a number : "; cin>>num;
f = 1;
for (i = 1; i <= num; i++)
{
f = f*i;
}
}
void show() {
cout << "The Factorial is:" << f;
}
};
int main() {
factorial obj;
obj.show();
return 0;
}
Output :
EXPERIMENT 10
Source Code :
public:
fibonacci()
{
f0 = 0;
f1 = 1;
fib = f0 + f1;
}
fibonacci(fibonacci &ptr)
{
f0 = ptr.f0;
f1 = ptr.f1;
fib = ptr.fib;
}
void increment()
{
f0 = f1;
f1 = fib;
fib = f0 + f1;
}
void output()
{
cout << fib << " ";
}
};
main()
{
int n;
cout << "Enter no. of elements in series : ";
cin >> n;
fibonacci obj;
for (int i = 1; i <= n; i++)
{
obj.output();
obj.increment();
}
}
Output :
EXPERIMENT 11
AIM : Write a program to find the biggest of three number using Friend
Function.
Source Code :
#include<iostream> using
namespace std; class Friend
{
private:
int a,b,c;
public:
void input()
{
cout<<"Enter 1st no. : "; cin>>a;
cout<<"Enter 2nd no. : "; cin>>b;
cout<<"Enter 3rd no. : "; cin>>c;
}
friend void biggest(Friend a);
};
void biggest(Friend l)
{
if((l.a>l.b)&&(l.a>l.c))
{
cout<<"Biggest no. : "<<l.a<<endl;
}
else if((l.b>l.a)&&(l.b>l.c))
{
cout<<"Biggest no. : "<<l.b<<endl;
}
else
{
cout<<"Biggest no. : "<<l.c<<endl;
}
}
main()
{
Friend obj;
obj.input();
biggest(obj);
}
Output :
EXPERIMENT 12
Source Code :
class A;
class B
{
private:
int x;
public:
B(int m = 0)
{
x = m;
}
void setB(int m)
{
x = m;
}
friend void output(B &B, A &A);
};
class A
{
private:
int y;
public:
A(int n = 0)
{
y = n;
}
void setA(int n)
{
y = n;
}
friend void output(B &B, A &A)
{
cout << "B : " << B.x << " and A :" << A.y << endl;
}
};
main()
{
A obj1(10);
B obj2(12);
output(obj2, obj1);
}
Output :
EXPERIMENT 13
Source Code :
Output :
EXPERIMENT 14
AIM : Write a program to find the greatest of two given numbers in two
different classes using friend function.
Source Code :
public:
void num()
{
cout << "Enter number : ";
cin >> x;
}
friend void greatest(A a, B b);
};
class B
{
private:
int x;
public:
void num()
{
cout << "Enter number : ";
cin >> x;
}
friend void greatest(A a, B b);
};
void greatest(A a, B b)
{
if (a.x > b.x)
{
cout << "Greatest : " << a.x << endl;
}
else
{
cout << "Greatest : " << b.x << endl;
}
}
main()
{
A obj1;
B obj2;
obj1.num();
obj2.num(); greatest(obj1,
obj2);
}
Output :
EXPERIMENT 15
Source Code:
Output :
EXPERIMENT 16
Source Code :
#include<iostream> using
namespace std;
class unaryInc
{
private:
int n;
public:
void input(int x)
{
n=x;
}
void output(void)
{
cout << "value of N : " << n;
}
void operator ++ (void)
{
n=++n;
}
};
int main()
{
int y;
cout<<"Enter a number : "; cin>>y;
unaryInc obj;
obj.input(y);
++obj;
cout << "After Increment ";
obj.output(); cout
<< endl;
return 0;
}
Output :
EXPERIMENT 17
AIM : Implement a class string containing the following functions:
Source Code:
#include <iostream>
#include <cstring> #include
<string>
my_string
{
private:
char str[30];
public:
void getdata(); void
display();
void operator=(my_string str1);
int operator<=(my_string str1);
void operator+(my_string str1);
int str_length();
void tolower();
void toupper();
};
void my_string::getdata()
{
cin >> str;
}
void my_string::display()
{
cout << "\n"<< str;
}
void my_string::operator+(my_string str1)
{
strcat(str, str1.str);
cout << "String After Concat is : " << str << endl;
}
return 0;
}
int my_string::str_length()
{
return strlen(str);
}
void my_string::toupper()
{
int l = strlen(str);
for (int i = 0; i <= l; i++)
{
if (str[i] >= 97 && str[i] <= 122)
{
str[i] = str[i] - 32;
}
}
cout << "UPPERCASE : " << str << endl;
}
void my_string::tolower ()
{
int l = strlen(str);
for (int i = 0; i <= l; i++)
{
if (str[i] >= 65 && str[i] <= 90)
{
str[i] = str[i] + 32;
}
}
cout << "LOWERCASE : " << str << endl;
}
int main()
{
(opt)
{
case 1: //concatenation
cout << "Enter the 1st string : "; a.getdata();
cout << "Enter the 2nd string : "; b.getdata();
a + b;
break;
case 2: //Copying
cout << "Enter the string : "; a.getdata();
a = b;
break;
case 3: //Comparision
cout << "Enter the 1st string : "; a.getdata();
cout << "Enter the 2nd string : "; b.getdata();
c = a <= b;
if (c == 1)
cout << "Strings are Equal" << endl;
else
cout << "Strings are not Equal" << endl;
break;
case 4: //Length
cout << "Enter the string : "; a.getdata();
cout << "The length of the string is :" << a.str_length() << endl;
;
break;
case 5: //To Uppercase
cout << "Enter the string : "; a.getdata();
a.tolower();
break;
case 6: // To lowercase
cout << "Enter the string : "; a.getdata();
a.toupper();
break;
case 7:
return 0;
default:
cout << "Invalid choice, Enter again : \n";
}
if (opt != 7)
{
cout << "Press 1 to continue : ";
cin >> opt1;
}
}
}
Output :
EXPERIMENT 18
AIM : Create a class called LIST with two pure virtual function
store () and retrieve (). To store a value call store and to retrieve
call retrieve
function. Derive two classes stack and queue from it and override
store and retrieve.
Source Code:
#include<iostream>
#include<stdlib.h>
#include<conio.h> using
namespace std;
struct node
{
int data; node
*next;
};
node *head=NULL,*tail=NULL;
class List
{
public: void
view()
{
node *n = head;
if(head==NULL)
{
cout<<"\n No elements found...";
}
else
{
cout<<" "; while(n!
=NULL)
{
if(n->next==NULL)
{
cout<<n->data;
}
else
{
cout<<n->data<<"->";
}
n = n->next;
}
}
}
virtual void store(int n)=0; virtual int
retrive()=0;
};
int main()
{
Stack s1; int
ch; while(1)
{
cout<<"\n1. Stack";
cout<<"\n2. Queue";
cout<<"\n3. Exit";
cout<<"\nEnter your choice : "; cin>>ch;
if(ch==1)
{
Stack s1; int
ch1; while(1)
{
cout<<"\n\n1. Push Element"; cout<<"\n2.
Pop Element"; cout<<"\n3. View Stack";
cout<<"\n4. Exit";
cout<<"\nEnter your choice : "; cin>>ch1;
if(ch1==1)
{
int element;
cout<<"\nEnter the element you want to push : ";
cin>>element; s1.store(element);
cout<<"\nElement Pushed";
}
else if(ch1==2)
{
int element=0;
element = s1.retrive();
if(element==-1)
{
cout<<"\nStack is Empty";
}
else
{
cout<<"\nElement Popped = "<<element;
}
}
else if(ch1==3)
{
cout<<"\nElements in stack from bottom to top : ";
s1.view();
}
else if(ch1==4)
{
break;
}
else
{
cout<<"\nWrong choice";
}
getch();
}
}
else if(ch==2)
{
Queue q1; int
ch1; while(1)
{
cout<<"\n1. Push Element"; cout<<"\n2. Pop
Element"; cout<<"\n3. View Queue";
cout<<"\n4. Exit";
cout<<"\nEnter your choice - "; cin>>ch1;
if(ch1==1)
{
int element;
cout<<"\nEnter the element you want topush
: ";
cin>>element; q1.store(element);
cout<<"\nElement Pushed";
}
else if(ch1==2)
{
int element=0;
element = q1.retrive();
if(element==-1)
{
cout<<"\nQueue is Empty";
}
else
{
cout<<"\nElement Popped = "<<element;
}
}
else if(ch1==3)
{
cout<<"\nElements in queue from front to rear : ";
q1.view();
}
else if(ch1==4)
{
break;
}
else
{
cout<<"\nWrong choice";
}
getch();
}
}
else if(ch==3)
{
exit(0);
}
else
{
cout<<"\nWrong Choice";
}
getch();
}
return 0;
Output :
EXPERIMENT 19
AIM : Create a base class basic_info with data members name, roll
no, sex and two member functions getdata and display. Derive a
class physical_fit from basic_info which has data members height and
weight and member functions getdata and display. Display all the
information using object of derived class.
Source Code :
#include<iostream> using
namespace std; class
basic_info {
private:
char name[20]; char
sex;
int roll_no;
public:
void getdata();
void displaydata();
};
void getdata();
void displaydata();
};
void physical_fit::displaydata()
{
basic_info::displaydata();
cout<<"Height is: "<<height<<endl;
cout<<"Weight is: "<<weight<<endl;
}
int main()
{
physical_fit obj1;
obj1.getdata();
cout<<"The data of student is : "<<endl; obj1.displaydata();
return 0;
}
Output :
EXPERIMENT 20
AIM : Create class first with data members book no, book name
and member function getdata and putdata. Create a class second
with data members author name, publisher and members getdata
and showdata. Derive a class third from first and second with data
member no of pages and year of publication. Display all this
information using array of objects of third class.
Source Code :
class first
{
private:
char bname[50]; int
bno;
public:
void getdata ()
{
cout << "Enter Book No. : ";
cin >> bno;
cout << "Enter Book name : ";
cin >> bname;
}
void putdata()
{
cout << "Book No.: " << bno << endl;
cout << "Book Name :" << bname << endl;
}
};
class second
{
private:
char author[20];
char publisher[20];
public:
void indata()
{
cout << "Enter Author : ";
cin >> author;
cout << "Enter Publisher : ";
cin >> publisher;
}
void showdata()
{
cout << "Author : ";
cout << author;
cout << "Publisher : ";
cout << publisher;
}
};
class third : public first, public second
{
private:
int year; int
page;
public:
void input()
{
getdata();
indata();
cout << "Enter number of pages : ";
cin >> page;
cout << "Enter Year of Publication : ";
cin >> year;
}
void output()
{
putdata();
showdata();
cout << "Number of pages : " << page << endl;
cout << "Year of Publication: " << year;
}
};
main()
{
int i, n;
cout << "Enter number of Books : ";
cin >> n;
third obj[5];
for (i = 0; i < n; i++)
{
obj[i].input();
}
for (i = 0; i < n; i++)
{
cout << "About Book no. " << i + 1 << " : " << endl; obj[i].output();
}
}
Output :
EXPERIMENT 21
Source Code :
public:
void input()
{
cout << "Enter name : ";
cin >> name;
cout << "Enter roll no : ";
cin >> rno;
}
void output()
{
cout << "Name : " << name << endl;
public:
void input1(); float
add();
};
void exam::input1()
{
input();
for (int i = 0; i < 6; i++)
{
cout << "Enter marks in subject " << i + 1 << " : ";
cin >> marks[i];
}
}
float exam::add()
{
float sum = 0;
for (int i = 0; i < 6; i++)
{
sum = sum + marks[i];
}
return sum;
}
class result : public exam
{
private:
float total;
public:
void output1()
{
total = add();
output();
cout << "Total Marks : " << total << endl;
}
};
main()
{
result obj;
obj.input1();
obj.output1();
}
Output :
EXPERIMENT 22
AIM : Create a base class called SHAPE. Use this class to store two
double type values. Derive two specific classes called TRIANGLE and
RECTANGLE from the base class. Add to the base class, a member
function getdata to initialize base class data members and another
member function display to compute and display the area of figures.
Make display a virtual function and redefine this function in the
derived classes to suit their requirements. Using these three classes
design a program that will accept driven of a TRIANGLE or
RECTANGLE interactively and display the area.
Source Code :
public:
void getdata(double l1, double b1)
{
l = l1; b =
b1;
}
virtual void display_area() = 0;
};
class triangle : public shape
{
public:
void display_area()
{
double area;
area = 0.5 * l * b;
cout << "Area of triangle : " << area;
}
};
class rectangle : public shape
{
public:
void display_area()
{
double area; area =
l * b;
cout << "Area of rectangle : " << area;
}
};
int main()
{
shape *obj1, *obj2; double
a, b; triangle t;
obj1 = &t;
cout << "For Triangle : " << endl;
cout << "Enter base : ";
cin >> a;
cout << "Enter height : ";
cin >> b;
obj1->getdata(a, b);
obj1->display_area(); rectangle
r;
obj2 = &r;
cout << "\nFor Rectangle : " << endl;
cout << "Enter length : ";
cin >> a;
cout << "Enter breadth : ";
cin >> b;
obj2->getdata(a, b);
obj2->display_area();
;
}
Output :
EXPERIMENT 23
AIM : Write a program to define the function template for calculating the
square of given numbers with different data types.
Source Code:
Output :
EXPERIMENT 24
Source Code:
public:
compare(temp x, temp y)
{
a = x; b
= y;
}
void display()
{
cout<<"The bigger number is : ";
if (a > b)
{
cout << a;
}
else
{
cout <<b;
}
}
~compare() {}
};
int main()
{
int a, b;
cout << "Enter 1st no. : ";
cin >> a;
cout << "Enter 2nd no. : ";
cin >> b;
compare<int> obj1(a, b);
obj1.display();
return 0;
}
Output :
EXPERIMENT 25
AIM : Write a program to define the function template for swapping two
items of various datatypes such as integers, float and characters.
Source Code:
int main()
{
int i1 = 7, i2 = 8;
float f1 = 3.4, f2 = 9.12;
char c1 = 'A', c2 = 'Z';
Swap(i1, i2);
Swap(f1, f2);
Swap(c1, c2);
Output :
EXPERIMENT 26
Source Code:
int x;
int n, choice;
cout << "Enter value of x : ";
cin >> x;
cout << "1) Print x one time" << endl;
cout << "2) Print x n times" << endl;
cout << "Enter Choice : ";
cin >> choice; switch
(choice)
{
case 1:
print(x);
break;
case 2:
cout << "How many times to print x : ";
cin >> n;
print(x, n); break;
default:
cout << "Invalid Choice";
};
return 0;
}
Output :
EXPERIMENT 27
Source Code:
public:
sum(temp x, temp y)
{
a = x; b
= y;
}
void display()
{
cout << a << " + " << b << " = " << a + b;
}
};
int main()
{
int a, b;
;
cout << "Enter 1st no. : ";
cin >> a;
cout << "Enter 2nd no. : ";
cin >> b;
sum<int> obj1(a, b);
obj1.display();
return 0;
}
Output :
EXPERIMENT 28
AIM : Write a program to read a set of lines from the keyboard and to
store it on a specified file.
Source Code:
int main()
{
int n;
char str1[100], str2[100];
cout << "Enter no. of lines : ";
cin >> n;
fstream outfile;
outfile.open("exp22.txt", ios::out | ios::binary);
cout << "Writing file....." << endl;
cin.get();
AIM : Write a program to read a text file and display its contents on the
screen.
Source Code :
int main()
{
int n;
char str1[100], str2[100];
cout << "Enter no. of lines : ";
cin >> n;
fstream outfile; outfile.open("exp23.txt", ios::out);
cout << "Writing file...." << endl;
cin.get();
for (int i = 1; i <= n; i++)
{
cout << "Type something : "; gets(str1);
outfile << str1 << endl;
}
outfile.close(); fstream
infile;
infile.open("exp23.txt", ios::in);
cout << "Reading from file : " << endl;
while (infile.getline(str2, 100))
{
puts(str2);
}
infile.close(); return 0;
}
Output :
EXPERIMENT 30
Source Code:
int n;
char str1[100], str2[100], str3[100];
cout << "Enter no. of lines to store : ";
cin >> n;
fstream outfile1; outfile1.open("myfile.txt", ios::out);
cout << "Writing to 1st file....." << endl;
cin.get();
for (int i = 0; i <n; i++)
{
cout << "Enter data : "; gets(str1);
outfile1 << str1 << endl;
}
outfile1.close();
fstream infile1, outfile2; infile1.open("myfile.txt",
ios::in); outfile2.open("myfile1.txt", ios::out);
cout << "Reading from the first file and copying to second file" << endl;
while (infile1.getline(str2, 100))
{
puts(str2);
outfile2 << str2 << endl;
}
infile1.close();
outfile2.close(); fstream
infile2;
infile2.open("myfile1.txt", ios::in);
cout << "Reading from copied file" << endl;
while (infile2.getline(str3, 100))
{
puts(str3);
}
infile2.close(); return 0;
}
Output :
EXPERIMENT 31
AIM : Write a program to perform the deletion of white spaces such
as horizontal tab, vertical tab, space, line feed, new line and carriage
return from a text file and store the contents of the file without the
white spaces on another file.
Source Code:
int main()
{
int n;
char str1[100], str2[100];
cout << "Enter no. of lines : ";
cin >> n;
fstream outfile; outfile.open("exp8in.txt",ios::out);
cout << "Writing file...." << endl; cin.get();
for (int i = 1; i <= n; i++)
{
cout << "Type something : "; gets(str1);
outfile << str1 << endl;
}
outfile.close();
fstream fin("exp8in.txt");
fstream fout("exp8out.txt");
string s;
while (fin >> s)
{
fout << s;
}
fin.close();
fout.close();
fstream infile;
infile.open("exp8out.txt", ios::in);
return 0;
}
Output :
EXPERIMENT 32
AIM : Write a program to read the class object of student info such
as name, age, sex, height and weight from the keyboard and to store
them on a specified file using read () and write () functions. Again, the
same file is opened for reading and displaying the contents of the file
on the screen.
Source Code:
public:
void input()
{
cout << "Enter name : ";
cin >> name;
cout << "Enter age : ";
cin >> age;
cout << "Enter sex : ";
cin >> sex;
cout << "Enter height : ";
cin >> ht;
cout << "Enter weight : ";
cin >> wt;
}
void output()
{
cout << "Name : " << name << endl;
cout << "Age : " << age << endl;
cout << "Sex : " << sex << endl;
cout << "Height : " << ht << endl;
cout << "Weight : " << wt << endl;
}
};
int main()
{
student_info s1, s2; fstream
outfile;
outfile.open("exp9.txt", ios::out | ios::binary);
Output :
EXPERIMENT 33
Source Code:
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(int);
int i;
cout << "Size of array is : " << n << endl;
cout << "Which index you want to access : ";
cin >> i;
try
{
if (i >= n)
{
throw i;
}
else
{
cout << "Element at " << i << " is : " << arr[i];
}
}
catch (int)
{
cout << "Exception caught : Accessing index beyond of array size";
}
return 0;
}
Output :
EXPERIMENT 34
AIM : Write a program to read two numbers and then divide first no by
second no and raise an exception if second number is zero.
Source Code:
Output :