II PUC Accountancy Question Bank 2019new

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

SDC INDEPENDENT PU COLLEGE

SUBJECT: COMPUTER SCIENCE


TOPIC: C++ LAB PROGRAMS
LAB PROGRAM – 1
1. Write a program to find the frequency of void frequency::display()
presence an element in an array. {
#include<iostream.h> if(freq>0)
#include<conio.h> cout<<”frequency of”<<ele<<”is”<<freq;
#include<iomanip.h> else
class frequency cout<<”element does not exist”;
{ }
private: void main()
int n, a[10], ele, freq, i; {
public: frequency f;
void getdata(); clrscr();`
void findfreq(); f.getdata();
void display(); f.findfreq();
}; f.display();
void frequency::getdata() getch();
{ }
cout<<” enter the size of array:”<<endl;
cin>>n; OUTPUT -1
cout<<” enter elements of array:”<<endl; Enter the size of the array: 5
for( i=0; i<n; i++) Enter 5 elements into the array:
cin>>a[i]; 10 50 40 30 40
cout<<” enter search element:”<<endl; Enter the search element: 40
cin>>ele; Frequency of 40 is 2.
}
void frequency::findfreq() OUTPUT-2
{ Enter the size of the array: 5
freq=0; Enter 5 elements into the array:
for( i=0; i<n; i++) 10 50 40 30 40
if(ele==a[i]) Enter the search element: 80
freq++; 80 doesn’t exist
}
LAB PROGRAM - 2
2. Write a program to insert an element into an int n, a[10], ele, p, i;
array at a given position. public:
voidgetdata();
#include<iostream.h> void insert();
#include<conio.h> void display();
#include<iomanip.h> };
#include<stdlib.h> voidinsertion::getdata()
Class insertion {
{ cout<<”enter how many elements?”<<endl;
private: cin>>n;

C++ LAB PROGRAMS


Ms. Shashikala SDC PU College COMPUTER SCIENCE

cout<<” enter the elements”<<endl; }


for( i=0; i<n; i++)
cin>>a[i]; void main()
cout<<” enter the element to be inserted”<<endl; {
cin>>ele; insertion i;
cout<<”enter the position”<<endl; clrscr();
cin>>p; i.getdata();
} i.insert();
void insertion :: insert() i.display();
{ getch();
if(p>n) }
{
cout<<” invalid position”; OUTPUT-1
exit(0); How many elements:5
} Enter the elements:
else 10 20 30 50 60
{ Enter the elements to be inserted : 40
for( i=n-1; i>=p; i--) Enter the positional value: 3
a[i+1]=a[i]; 40 is inserted successfully
a[p]=ele; The array after insertion :-
n++; 10 20 30 40 50 60
cout<<” element successfully inserted”<<endl;
} OUTPUT-2
} How many elements:5
void insertion :: display() Enter the elements:
{ 10 20 30 50 60
cout<<” the array after insertion is:”<<endl; Enter the elements to be inserted : 40
for(i=0; i<n; i++) Enter the positional value: 5
cout<<setw(4)<<a[i]; 5 is an invalid position
LAB PROGRAM – 3
}
3. Write a program to delete an element from an void deletion :: remove()
array from a given position. {
#include<iostream.h> if (p>n-1)
#include<conio.h> {
#include<iomanip.h> cout<<” invalid position”;
#include<stdlib.h> exit(0);
Class deletion }
{ Else
private: {
int n, a[10], ele, p, i; ele=a[p];
public: for( i=p+1; i<n; i++)
void getdata(); a[i-1]=a[i];
void remove(); n--;
void display(); cout<<ele<<” is successfully removed”>>endl;
}; }
void deletion::getdata() }
{ void deletion :: display()
cout<<”enter how many elements?”<<endl; {
cin>>n; cout<<” the array after deletion is:”<<endl;
cout<<” enter the elements”<<endl; for(i=0; i<n; i++)
for( i=0; i<n; i++) cout<<setw(4)<<a[i];
cin>>a[i]; }
cout<<”enter the position to be deleted”<<endl;
cin>>p; void main()
C++ LAB PROGRAMS 2
Ms. Shashikala SDC PU College COMPUTER SCIENCE

{ Enter the positional value: 3


deletion d; The element 40 at position 3 is successfully
clrscr(); deleted.
d.getdata(); Elements after deletion :
d.remove(); 10 20 30 50
d.display();
getch(); OUTPUT-2
} How many elements:5
Enter the elements:
OUTPUT-1 10 20 30 40 50
How many elements:5 Enter the positional value: 6
Enter the elements: 6 is an invalid position
10 20 30 40 50
LAB PROGRAM – 4
4. Write a program to sort the elements of an array }
in ascending order using insertion sort. j - -;
#include<iostream.h> }
#include<conio.h> }
#include<iomanip.h> }
#include<stdlib.h> void sorting :: display()
Class sorting {
{ cout<<” the array after sorting is:”<<endl;
private: for(i=0; i<n; i++)
int n, a[10], i; cout<<setw(4)<<a[i];
public: }
void getdata();
void sort(); void main()
void display(); {
}; sorting s;
void sorting::getdata() clrscr();
{ s.getdata();
cout<<”enter how many elements?”<<endl; s.sort();
cin>>n; s.display();
cout<<” enter the elements”<<endl; getch();
for( i=0; i<n; i++) }
cin>>a[i];
} OUTPUT-1
void sorting :: sort() Enter the size of array:5
{ Enter the elements :
int temp, j; 50 30 40 20 10
for( i=1; i<n; i++) Elements after sorting:
{ 10 20 30 40 50
j=i;
While(j>=1) OUTPUT-2
{ Enter the size of array:4
If(a[j]<a[j-1]) Enter the elements :
{ 90 5 80 40
temp=a[j]; Elements after sorting:
a[j]= a[j-1]; 5 40 80 90
a[j-1]= temp;
LAB PROGRAM – 5
Write a C++ program to search for a given
element in an array #include<iostream.h>
using binary search method. #include<conio.h>

C++ LAB PROGRAMS 3


Ms. Shashikala SDC PU College COMPUTER SCIENCE

#include<iomanip.h> if(ele < a[mid])


end = mid-1;
class search else
{ beg = mid+1;
private: }
int a[10], n, ele, loc, beg, end, mid, i; }
public: void search::display( )
void getdata( ); {
void bsearch( ); if(loc == -1)
void display( ); cout<<ele<<" Element does not exist...!!!";
}; else
void Search::getdata( ) cout<<ele<<" Found at Location:"<<loc+1;
{ }
cout<<"Enter the size of the array:"<<endl; void main( )
cin>>n; {
cout<<"Enter the array elements in sorted Search s;
order:"<<endl; clrscr( );
for(i=0;i<n;i++) s.getdata( );
cin>>a[i]; s.bsearch( );
cout<<"Enter the element to search:"<<endl; s.display( );
cin>>ele; getch( );
} }
void search::bsearch( ) OUTPUT 1:
{ How many elements? 5
loc = -1; enter the elements: 10 40 50 30 50
beg = 0; enter the search element: 50
end = n-1; position = 2.
while(beg <= end)
{
mid = (beg+end)/2; OUTPUT 2:
if(ele == a[mid]) How many elements? 5
{ enter the elements:40 50 20 30 10
loc = mid; enter the search element: 60
break; search is unsuccesfull.
}
else
LAB PROGRAM – 6
void SimpleInterest::readdata( )
Write a C++ program to create a class with data {
members cout<<"Enter the Principal, Rate and
principal, time and rate. Create a member function Time"<<endl;
to accept data values, to compute simple interest cin>>p>>r>>t;
and to display the result. }
#include<iostream.h> void SimpleInterest::compute( )
#include<conio.h> {
class simpleinterest si=(p*t*r)/100;
{ }
private: void SimpleInterest::display( )
float p, r, t, si; {
public: cout<<"Principal = "<<p<<endl;
void readdata( ); cout<<"Time = "<<t<<endl;
void compute( ); cout<<"Rate = "<<r<<endl;
void display( ); cout<<"Simple Interest = "<<si<<endl;
}; }
void main( )
C++ LAB PROGRAMS 4
Ms. Shashikala SDC PU College COMPUTER SCIENCE

{ Output :
SimpleInterest si; enter principle amount , time and rate: 5000, 5.5,
clrscr( ); 8.75
si.readdata( ); principle: 5000
si.compute( ); time: 5.5
si.display( ); rate: 8.75
getch( ); simple intrest: 2406.25
}
LAB PROGRAM – 7
Write a C++ program to create a class with data x=-b/(2*a);
members a, b, c and member functions to input cout<<"Root is."<<x;
data, compute the discriminant based on the }
following conditions and print the roots. else
If discriminant = 0, print the roots are if(disc>0)
equal and their value. {
If discriminant > 0, print the real roots and
their values. cout<<"Real and Distinct Roots..."<<endl;
If discriminant < 0, print the roots are x1=(-b+sqrt(disc))/(2*a);
imaginary and exit the program. x2=(-b-sqrt(disc))/(2*a);
cout<<"Root 1 is "<<x1<<endl;
#include<iostream.h> cout<<"Root 2 is "<<x2<<endl;
#include<conio.h> }
#include<math.h> else
class Quadratic cout<<"Imaginary Roots..."<<endl;
{ }
private: void main( )
int a, b, c; {
float disc, x, x1, x2; Quadratic q;
clrscr( );
public: q.readdata( );
void readdata( ); q.display( );
void compute( ); getch( );
void display( ); }
};
output 1:
void Quadratic::readdata( ) Enter the co-efficients: 1 2 1
{ roots are equal
cout<<"Enter the values for a, b, c (Co- first root=-1
efficeient)"<<endl; second root=-1
cin>>a>>b>>c;
} output 2:
Enter the co-efficients:2 -3 1
void Quadratic::compute( ) roots are positive and different
{ first root=1
disc = b*b-4*a*c; second root=0.5
}

void Quadratic::display( ) output 3:


{ Enter the co-efficients:2 -2 1
compute( ); roots are imaginary.
if(disc == 0)
{
cout<<"Equal Roots..."<<endl;
LAB PROGRAM – 8

C++ LAB PROGRAMS 5


Ms. Shashikala SDC PU College COMPUTER SCIENCE

if(ans==2)
Write a C++ program to find the area of square/ {
rectangle/ triangle using function overloading. cout<<"enter two sides:";
#include<iostream.h> cin>>x>>y;
#include<conio.h> cout<<"area of the
#include<stdlib.h> rectangle="<<f1.area(x,y)<<endl;
#include<math.h> }
class funcoverload else
{ if(ans==3)
float s; {
public: double area(double a) cout<<"enter three sides: ";
{ cin>>x>>y>>z;
return a*a; cout<<setprecision(8);
} cout<<"area of triangle="<<f1.area(x,y,z)<<endl;
double area(double l , double b) }
{ else
return l*b; cout<<"invalid input";
} getch();
double area(double a, double b, double c) }
{
s=(a+b+c)/2.0; OUTPUT1:-
return(sqrt(s*(s-a)*(s-b)*(s*b))); Enter the number of inputs (1,2,3) :1
} enter the side: 4.5
}; Area of the square = 20.25
void main()
{ OUTPUT 2:-
clrscr(); Enter the number of inputs (1,2,3) :2
double x,y,z; enter the side: 4.5, 2.75
int ans; Area of the retangle = 12.375
funoverload f1;
cout<<"enter the number of inputs(1,2,3):"; OUTPUT 3:-
cin>>ans; Enter the number of inputs (1,2,3) :3
if(ans==1) enter the side: 2,3.5,2.75
{ Area of the triangle =2.74462
cout<<"enter the side:";
cin>>x; output 4:-
cout<<"area of the square="<<f1.area(x)<<endl; Enter the number of inputs (1,2,3) :5
} invlid input
else
LAB PROGRAM – 9
clrscr( );
Write a C++ program to find cube of a number cout<<"Enter the input number"<<endl;
using inline function. cin>>n;
#include<iostream.h> cout<<"Cube of"<<" = "<<cube(n);
#include<conio.h> getch( );
}
inline int cube(int a) OUTPUT 1:
{ Enter a nmber:- -5
return a*a*a; cube of -5 = -125
}
OUTPUT 2:
void main() Enter a nmber:10
{ cube of 10 = 1000
int n;

C++ LAB PROGRAMS 6


Ms. Shashikala SDC PU College COMPUTER SCIENCE

LAB PROGRAM – 10
Write a C++ program to find sum of the series 1 + sum=sum+pow(x,i);
x + x2 + x3 + ….xn using constructors. return sum;
#include<iostream.h> }
#include<conio.h> void main()
#include<math.h> {
class Series int x,n;
{ clrscr( );
private: cout<<"Enter the value for Base(X)="<<endl;
int sum, x, n; cin>>x;
cout<<"Enter the value for Power(n)"<<endl;
public: cin>>n;
int sumseries( ); Series s1(x,n);
Series s2 = s1;
cout<<"Sum of Series using Parameterised
Series(int y, int m) Constructor:";
{ cout<<s1.sumseries()<<endl;
cout<<"Sum of Series using Copy Constructor:";
sum = 1; x = y; cout<<s2.sumseries( );
n = m; getch( );
} }
OUTPUT 1:
};
Enter the base and the power(x,n): 2 , 5
Object1: sum of the series is 63
int Series::sumseries( ) object2: sum of the series is 63
{
for(int i=1;i<=n; i++)
LAB PROGRAM – 11
cin>>name;
Create a base class containing the data member }
roll number and name. Also create a member void display( )
function to read and display the data using the {
concept of single level inheritance. Create a cout<<"\nRollNumber:"<<rollnumber<<endl;
derived class that contains marks of two subjects cout<<"Student Name:"<<name<<endl;
and total marks as the data members. }
#include<iostream.h> };
#include<conio.h>
class Report : public Student
class Student {
{ private:
private: int marks1, marks2, total;
int rollnumber;
char name[20]; public:

public:
void readmarks( )
{
void readdata( )
{ cout<<"\nEnter Subject 1 Marks: ";
cin>>marks1;
cout<<"Enter the Roll Number: "; cout<<"Enter Subject 2 Marks: ";
cin>>rollnumber; cin>>marks2;
cout<<"Enter the Student Name:"; }

C++ LAB PROGRAMS 7


Ms. Shashikala SDC PU College COMPUTER SCIENCE

void compute( ) 111 ganesh


{ Enter two subject marks: 95 98
total = marks1 + marks2; Roll no: 111
Name: ganesh
cout<<endl<<"Total Marks : "<<total; subject1= 95
} subject2=98
}; total marks=193

void main( ) OUTPUT 2:


{
Report R; Enter Roll no. and name
R.readdata( ); 222 rani
R.display( ); Enter two subject marks: 99 100
R.readmarks( ); Roll no: 222
R.compute( ); Name: rani
getch( ); subject1= 99
} subject2=100
OUTPUT 1: total marks=199

Enter Roll no. and name


LAB PROGRAM – 12
{
Create a class containing the following data cout<<"Register Number : "<<regno<<endl;
members Register_No, cout<<"Student Name : "<<name<<endl;
Name and Fees. Also create a member function to cout<<"Fees : "<<fees<<endl;
read and display the data using the concept of }
pointers to objects.
#include<iostream.h> void main( )
#include<conio.h> {
Student *S;
class Student clrscr( );
{ S->readdata( );
S->display( );
private: getch( );
long regno; }
char name[20];
float fees; OUTPUT 1:
public: Enter reg.no:111
void readdata( ); Enter name:lakshmi
void display( ); enter student fees:50000
}; reg.no:111
name:lakshmi
void Student::readdata( ) student fees:50000
{
cout<<"Enter the Register Number:"<<endl; OUTPUT 2:
cin>>regno; Enter reg.no:222
cout<<"Enter the Student Name:"<<endl; Enter name:rani
cin>>name; enter student fees:50000
cout<<"Enter the Fees:"<<endl; reg.no:222
cin>>fees; name:rani
} student fees:50000

void Student::display( )

C++ LAB PROGRAMS 8

You might also like