Anda di halaman 1dari 55

Q1.

WRITE A PROGRAM USING MULTIPLE INHERITANCE FOR THE CLASSES STUDENT, GAME AND
PERSON.

#include<stdio.h>

#include<conio.h>

int age;

public:

void indata()

gets(name);

cout<<"\n\nEnter the age : ";

cin>>age;

void outdata();

};

void person::outdata()

cout<<"\n\n";

cout<<"-";

cout<<"\n\nAge of the student is : "<<age;

class game {

char game_name[20];

Page | 1
public:

void input()

cout<<"\n\nEnter the game name : ";

cin.get();cin.getline(game_name,20);

void output()

};

{ float Tmarks; // Multiple inheritance

int rollno;

public:

{if(Tmarks>90)

return 'A';

else if(Tmarks>80&&Tmarks<=90)

return 'B';

else if(Tmarks>70&&Tmarks<=80)

return 'C';

else if(Tmarks>60&&Tmarks<=70)

return 'D';

else

Page | 2
return 'E';

void enter()

indata();

input();

cin>>Tmarks;

void display()

outdata();

OUTPUT();

};

void main()

{ clrscr();

student A;

A.enter();

cout<<”\n\nstudent details are : \n\n”;

A.display();

Page | 3
getch();

OF CIRCLE, SQUARE, RECTANGLE, RIGHT TRIANGLE AND TRIANGLE USING
HERON’S FORMULAE
#include<iostream.h>

#include<conio.h>

float area(float p)

return 3.14*p*p;

float area(int p)

return p*p;

return p*q;

float area(int p, int q)

return (0.5*p*q);

float area(float p,float q, float r)

Page | 4
float s;

s=(p+q+r)/2;

return sqrt(s*(s-p)*(s-q)*(s-r));

void main()

{clrscr();

int p,q,ch;

float a,b,r;

char choice;

do{

switch(ch)

cin>>p;

break;

case 2: cout<<"\n\nEnter radius of circle : ";

cin>>a;

Page | 5
cout<<"\n\narea of circle is : "<<area(a);

break;

cin>>a;

cin>>b;

break;

cin>>p;

cin>>q;

break;

cin>>a;

cin>>b;

cin>>r;

break;

cout<<"\n\nWant to choose again : ";

cin>>choice;

}while(choice=='y'||choice=='Y');

getch();

Page | 6

#include<math.h>

#include<conio.h>

return pow(a,b);

return pow(a,b);

return pow(a,b);

return pow(a,b);

void main()

{clrscr();

int P,Q;

float R,S;

cout<<"Enter int P : "; cin>>P;

Page | 7
cout<<"Enter int Q : "; cin>>Q;

cout<<P<<"^"<<Q<<" = "<<pow(P,Q)<<"\n\n"; // Using ‘pow’ from math.h

cout<<P<<"^"<<R<<" = "<<pow(P,R)<<"\n\n";

cout<<R<<"^"<<Q<<" = "<<pow(R,Q)<<"\n\n";

cout<<R<<"^"<<S<<" = "<<pow(R,S);

getch();

Q4.WRITE A PROGRAM TO CREATE A TEXT FILE TO INPUT ROLL NO AND MARKS

OF TEN STUDENTS AND DISPLAY THEM ON SCREEN AFTER READING FROM THE
TEXT FILE USING DATA FILE HANDLING.

#include<conio.h>

void main()

clrscr();

cout<<"\n\tEnter the marks of student "<<i+1<<" => "; cin>>b[i];

ofstream student;

student.open("stud.txt");

Page | 8
{

student.close();

i=0;

cout<<”\n\nStudent details are :\n\n”;

char str[80][80];

ifstream stude;

stude.open("stud.txt",ios::in);

stude.seekg(0); // Pointing to beginning of the text

while(!stude.eof())

{stude.getline(str[i],80);

cout<<str[i]<<"\n\n";

i++;

getch();

Q5.PROGRAM TO SORT AN ARRAY IN ASCENDING ORDER USING SELECTION

SORT SELECTION
#include<iostream.h>

void main()

{clrscr();

int A[80],n;

Page | 9
cin>>n;

if(n<=0||n>80)

goto lb;

cin>>A[i];

void selection_sort(int A[],int n);

selection_sort(A,n);

cout<<A[i]<<" ";

getch();

for(int i=0; i<n; i++)

{ small=A[i]; count++;

if(A[j]<small)

{small=A[j];

A[j]=A[i];

A[i]=small;

Page | 10
}

cout<<A[k]<<" ";

Q6.WRITE A PROGRAM USING POINTERS TO FIND THE LENGTH OF A STRING

AND PRINT THE REVERSED STRING
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

cout<<"\n\nEnter the string : ";

cin.getline(str,256);

cout<<"\n\nThe reverse string is : ";

char intermediate;

for(int j=strlen(str)-1,i=0; i<=strlen(str)/2; j--,i++)

intermediate = *(str+j);

//Loop to reverse the string

*(str+j)=*(str+i);

Page | 11
*(str+i)=intermediate;

cout<<str;

getch();

Q7.WRITE A PROGRAM USING POINTERS TO SWAP TWO INTEGERS.

#include<conio.h>

void main()

clrscr();

int a,b;

cout<<"\n\nEnter second integer : "; cin>>b;

sWrite a program_using_pointers(&a,&b);

getch();

int temp;

temp=*a;

*a=*b;

*b=temp;

Page | 12
}

Q8.WRITE A PROGRAM IN C++ USING POINTERS TO FIND THE SMALLEST AND

THE LARGEST ELEMENT IN AN ARRAY

#include<conio.h>

void main()

clrscr();

cin>>n;

array=new[n];

cin>>array[i];

i=0;

cout<<"\n\nThe array formed is : \n\n";

while(i!=n)

cout<<array[i]<<" ";

i++;

smallest=array[0];

for(i=0; i<n; i++)

Page | 13
if(array[i]<=smallest)

smallest=array[i]; //Comparing the elements

largest=array[0];

for(i=0; i<n; i++)

if(array[i]>=largest)

largest=array[i];

cout<<"\n\nThe smallest element is : "<<smallest<<"\n\nThe largest element is :

"<<largest;

getch();

Q9.WRITE A PROGRAM IN C++ TO PRINT AND FIND THE SUM OF EVEN OR ODD
NUMBERS USING RECURSION.

#include<conio.h>

int sum_even(int );

int sum_odd(int );

int sum=0;

int num=0;

void main()

{ clrscr();

int n;

int s_e, s_o;

cout<<"\n\nEnter the number upto which you want the sum of even/odd numbers : ";

Page | 14
cin>>n;

cout<<l<<"\n";

s_e=sum_even(n);

s_o=sum_odd(n);

cout<<"\n\nThe sum of odd numbers upto "<<n<<" = "<<s_o;

getch();

int sum_even(int n)

if(num%2==0)

sum=sum+num;

if(num!=n && num<n)

++num;

sum_even(n);

return sum;

int sum_2=0;

int num_2=0;

int sum_odd(int n)

Page | 15
if(num_2%2!=0)

sum_2=sum_2+num_2;

if(num_2!=n)

num_2++;

sum_odd(n);

return sum_2;

Q10.WRITE A PROGRAM TO PRINT AND FIND THE SUM OF FIBONACCI SERIES

USING RECURSION

#include<conio.h>

int fibonacci(int n);

void main()

clrscr();

int n;

cout<<"\n\n Enter the number of terms upto which you want the sum of fibonnaci series :

";

cin>>n;

cout<<"\n\nThe fibonacci series generated is : \n\n";

cout<<"1 1 ";

int s=fibonacci(n);

cout<<"\n\nThe sum of fibonacci series for "<<n<<" terms = "<<s;

getch();

Page | 16
}

int first=1;

int second=1;

int third;

int i=2;

int sum=0;

int fibonacci(int n)

{ if(n==1)

sum=1;

else if(n==2)

sum=2;

// n = 1 2 3 4 5

else if(n>1 && i!=n) // num = 1 1 2 3 5

third=first+second;

cout<<third<<” “;

if(i==2)

sum+=first+second+third;

else

sum+=third;

first=second;

second=third;

++i;

fibonacci(n);

Page | 17
return sum;

Q11.WRITE A PROGRAM IN C++ TO READ FILE “SPORTS.DAT” AND COPY ONLY

THOSE RECORDS WHERE EVENT NAME IS ATHLETIC USING THE CONCEPT OF
DATA FILE HANDLING.
#include<fstream.h>

#include<conio.h>

#include<stdio.h>

#include<stdlib.h>

struct sports {

char event[20];

char participants[10][30];

int no_of_participants;

} s[20], s2[20];

void copy(fstream &ob);

int i=0;

void main()

clrscr();

char choice;

fstream ob("sports.dat",ios::binary|ios::in|ios::out);

do

if(i>0)

cin.get();

Page | 18
cout<<"\n\nEnter the name of Event : ";

cin.getline(s[i].event,20);

cout<<"\n\nEnter the total number of participants in Event "<<s[i].event<<" : ";

cin>>s[i].no_of_participants;

cin.get();

for(int j=0; j<s[i].no_of_participants; j++)

cin.getline(s[i].participants[j],30);

cout<<"\n\n\nWant to Enter Details of Another Event (Y/N) : ";

cin>>choice;

i++;

} while(choice=='y'||choice=='Y');

cout<<"\n\n\n\n\n*********************************************************************
*********

******************************\n\n";

copy(ob);

cout<<"\n\n***************************************************************************
*********

*****************************\n\n\n";

getch();

void copy(fstream &o)

sports s[20];

o.seekg(0);

Page | 19
ofstream file;

file.open("athletic.dat",ios::binary);

file.seekp(0);

int j,n=0;

int c=0;

while(o)

if(strcmp("athletic",s[c].event)==0)

file.write((char*)&s[c], sizeof(sports));

n=1;

break;

c++;

if(n==0)

cout<<"\n\nUnsuccessful Search.";

getch();

exit(0);

o.close();

file.close();

sports sp;

Page | 20
ifstream oo;

oo.open("athletic.dat",ios::binary);

while(oo)

cout<<"\n\nThe Records of file are : \n\n";

cout<<"\n\nEvent = "<<sp.event;

for(int i=0; i<sp.no_of_participants; i++)

cout<<sp.participants[i]<<"\n\n";

oo.close();

remove("athletic.dat");

remove("sports.dat");

Q12.WRITE A PROGRAM IN C++ TO CREATE A LINKED LIST OF INTEGERS AND

PERFORM SOME BASIC OPERATION ON IT.

#include<process.h>

#include<conio.h>

struct node {

int info;

node*next;

}*rear,*start,*newptr,*save,*ptr;

Page | 21
void delnode()

if(start==NULL)

cout<<"underflow!!";

else

ptr=start;

start=start->next;

delete ptr;

cout<<"\n\nFirst node deleted";

void insert_beg(node*np)

//To insert at the beginning

if(start==NULL)

start=np;

else

save=start;

start=np;

np->next=save;

void display(node*np)

Page | 22
{

while(np!=NULL)

cout<<np->info<<"->";

np=np->next;

cout<<"\n";

node*create_new_node(int n)

ptr=new node;

ptr->info=n;

ptr->next=NULL;

return ptr;

void insert_end(node*np)

if(start==NULL)

start=rear=np;

else

rear->next=np;

rear=np;

Page | 23
void search(node*np,int a)

int flag=0;

while(np!=NULL)

if(np->info==a)

flag=1;

break;

else

np=np->next;

if(flag)

cout<<a<<"\n\nFound";

else

void main()

clrscr();

start=rear=NULL;

char ans='y',choice;

int ch,info,n;

Page | 24
{

cin>>ch;

switch(ch)

case 1: do

cout<<"\n\nEnter information for the new node : ";

cin>>info;

newptr=create_new_node(info);

if(newptr!=NULL)

else

cout<<"\n\nNode cannot be created;";

exit(1);

insert_end(newptr);

display(start);

cout<<"\n\nWant to insert again : ";

cin>>choice;

}while(choice=='y'||choice=='Y');

Page | 25
break;

case 2: do

cout<<"\n\nEnter info for new node: ";

cin>>info;

newptr=create_new_node(info);

if(newptr!=NULL)

else

cout<<"\n\nNode cannot be created ";

exit(1);

insert_beg(newptr);

display(start);

cout<<"\n\nWant to insert again : ";

cin>>choice;

}while(choice=='y'||choice=='Y');

break;

case 3: do

cout<<"\n\nEnter info to be searched : ";

cin>>n;

search(start,n);

Page | 26
cout<<"\n\nWant to search again : ";

cin>>choice;

}while(choice=='y'||choice=='Y');

break;

case 4: delnode();

break;

case 5: display(start);

break;

cout<<"\n\nWant to choose again : ";

cin>>ans;

while(ans=='y'||ans=='Y');

getch();

Q13. WRITE A PROGRAM IN C++ TO IMPLEMENT QUEUE AS A LINKED LIST

#include<conio.h>

struct node {

int roll;

node* next;

}*front,*rear,*ptr,*newptr,*np;

node *create(int a)

Page | 27
ptr->roll=a;

ptr->next=NULL;

return ptr;

void insert(node *np)

if(front==NULL)

front=rear=np;

else

rear->next=np;

rear=np;

void delet()

if(front==NULL)

cout<<"\n Underflow!!!!";

else

ptr=front;

front=front->next;

delete ptr;

Page | 28
void display(node *np)

while(np!=NULL)

cout<<np->roll<<"-> ";

np=np->next;

void main()

clrscr();

front=rear=NULL;

int n,m;

char ans,ch;

do

{ cout<<"\nChoose from the menu : "

<<"\n 2) Delete

<<"\n 3) Display."

<<"\n\n Enter your choice : ";

cin>>n;

switch(n)

case 1: ans='y';

while(ans=='y'||ans=='Y')

Page | 29
{

cout<<"\n Enter element to be inserted .";

cin>>m;

newptr=create(m);

if(newptr==NULL)

insert(newptr);

display(front);

cout<<"\n Want to enter more nodes ?: ";

cin>>ans;

break;

case 2: ans='y';

while(ans=='y'||ans=='Y')

delet();

display(front);

cin>>ans;

break;

display(front);

Page | 30
break;

default: cout<<"\n You entered wrong choice...";

cin>>ch;

}while(ch=='y'||ch=='Y');

getch();

Q14.WRITE A PROGRAM TO IMPLEMENT STACK AS A LINKED LIST

#include<conio.h>

#include<process.h>

struct node {

int roll;

node* next;

}*top,*save,*ptr,*newptr,*np;

node *create(int a)

ptr->roll=a;

ptr->next=NULL;

return ptr;

void push(node *np)

Page | 31
if(top==NULL)

top=np;

else

save=top;

top=np;

np->next=save;

void pop()

if(top==NULL)

cout<<"\n Underflow!!!!";

else

ptr=top;

top=top->next;

delete ptr;

while(np!=NULL)

cout<<np->roll<<" -> ";

Page | 32
np=np->next;

void main()

clrscr();

top=NULL;

int n,m;

char k,ch;

do {

<<"\n 1. Push."

<<"\n 2. Pop."

<<"\n 4. Quit."

cin>>n;

switch(n)

case 1: k='y';

while(k=='y'||k=='Y')

cout<<"\n Enter element to be inserted .";

cin>>m;

Page | 33
newptr=create(m);

if(newptr==NULL)

push(newptr);

display(top);

cout<<"\n\n Want to enter again ?: ";

cin>>k;

break;

case 2: k='y';

while(k=='y'||k=='Y')

pop();

display(top);

cin>>k;

break;

case 3: cout<<"\n The Stack formed is : ";

display(top);

break;

case 4: exit(0);

break;

Page | 34
default: cout<<"\n Please enter desired keyword : ";

cout<<"\n Do you want to continue..? : ";

cin>>ch;

}while(ch=='y'||ch=='Y');

getch();

Q15.WRITE A PROGRAM IN C++ TO IMPLEMENT QUEUE AS AN ARRAY

#include<conio.h>

#include<process.h>

const int size=50;

int q[size],rear=-1,front=-1;

int insert(int q[],int ele)

if(rear==size-1)

return(-1);

else if(rear==-1)

front=rear=0;

q[rear]=ele;

else

rear++;

Page | 35
q[rear]=ele;

return(0);

int r ;

if(front==-1)

return(-1);

else

r=q[front];

if(rear==front)

front=rear=-1;

else

front++;

return r;

void display(int q[],int front,int rear)

if(front==-1)

return;

else

Page | 36
{

for(int i=front;i<=rear;i++)

cout<<q[i]<<" ";

void main()

clrscr();

int n,u,k,m;

char ch,ans;

do

<<"\n 1. Insert"

<<"\n 3. Display"

<<"\n\n Enter your choice : "; cin>>n;

switch(n)

case 1: ans='y';

while(ans=='y'||ans=='Y')

k=insert(q,m);

Page | 37
if(k==-1)

cout<<"\n The resultant Queue is : ";

display(q,front,rear);

cout<<"\n\n Want to enter again ?: ";

cin>>ans;

break;

case 2: ans='y';

while(ans=='y'||ans=='Y')

u=delet(q);

if(u==-1)

break;

else

cout<<"\n The resultant Queue is : \n\n";

display(q,front,rear);

Page | 38
cout<<"\n\n Want to delete again ?: ";

cin>>ans;

break;

case 3: cout<<"\n The Queue is : \n\n";

display(q,front,rear);

break;

cout<<"\n Choose from the menu again ? : ";cin>>ch;

}while(ch=='y'||ch=='Y');

getch();

Q16.WRITE A PROGRAM IN C++ TO IMPLEMENT STACK AS AN ARRAY

#include<iostream.h>

#include<process.h>

int pop(int[],int&);

int push(int[],int&,int);

void display(int[],int);

const int size=50;

void main()

clrscr();

Page | 39
char m,ch;

int k,stack[size],item,top=-1,res;

do

<<"\n 1. Push"

<<"\n 2. Pop"

<<"\n 4. Exit"

<<"\n\nEnter your choice : "; cin>>k;

switch(k)

case 1: ch='y';

while(ch=='y'||ch=='Y')

{ cout<<"\nEnter the element : ";

cin>>item;

res=push(stack,top,item);

if(res==-1)

{cout<<"\nOverflow !!!!";

exit(1); }

cout<<"\nThe stack formed is : \n\n";

display(stack,top);

cout<<"\n\n\nWant to enter again ?: ";

cin>>ch;

break;

Page | 40
case 2: ch='y';

while(ch=='y'||ch=='Y')

{ res=pop(stack,top);

if(res==-1)

cout<<"\nUnderflow !!!!";

exit(1);

else

cout<<"\nThe resultant stack is : \n\n";

display(stack,top); }

cin>>ch;

break;

case 3: cout<<"\nThe resultant stack is : ";

display(stack,top);

break;

case 4: exit(0);

break;

default: cout<<"\nPlease enter desired keyword : ";

} // end of switch

Page | 41
cin>>m;

}while(m=='y'||m=='Y'); // end of do-while loop

getch();

} // end of main()

if(top==size-1)

return -1;

else

top++;

stack[top]=el;

return 0;

int ret;

if(top==-1)

return -1;

else

ret=stack[top];

top--;

return ret;

Page | 42
}

void display(int stack[],int top)

cout<<stack[top]<<"<--";

for(int i=top-1;i>=0;i--)

cout<<stack[i]<<"<--";

Q16.PROGRAM TO SEARCH AN ELEMENT OF AN ARRAY USING LINEAR SEARCH

METHOD

#include<conio.h>

void main()

{ clrscr();

int A[50]; int n; int p; int subscript; /*Note: subscript and index are same.*/

cin>>A[i];

cout<<A[i]<<" ";

cin>>p; // p=element to be searched

Page | 43
void linear_search ( int A[], int n, int p); //function declaration

linear_search(A,n,p);

getch();

{ flag=count;

if( A[i]==p)

B[count]=i;

flag=count;

if(flag==0)

else

{ if(A[i]==p)

Page | 44
}

Q17.WRITE A PROGRAM IN C++ TO PERFORM BASIC OPERATIONS ON THE TWO

COMPLEX NUMBERS USING STRUCTURES.

#include<math.h>

#include<conio.h>

struct complex {

int i,r;

}x,y;

";

{int ch;

l:

switch(ch)

case 2 : cout<<"\n\nResultant of subtraction of complex number => "<<"("<<b.ra.r<<")"<<"

Page | 45
+ ("<<b.i-a.i<<") i"; break;

cout<<"\n\nResultant of multiplication of complex number => "<<"("<<a.r*b.ra.i*b.i<<")"<<"

+ ("<<a.r*b.i+a.i*b.r<<") i";

{int ch;

switch(ch)

case 1 : cout<<"\n\nResultant of division of complex number =>

"<<((a.r*b.r)+(a.i*b.i))/(pow(b.r,2)+pow(b.i,2))

break;

case 2 : cout<<"\n\nResultant of division of complex number =>

"<<((b.r*a.r)+(b.i*a.i))/(pow(a.r,2)+pow(a.i,2))

break;

Page | 46
}

void main()

clrscr();

int choice;

char ch;

cout<<"\n\n";

cout<<"\n\n";

do

switch(choice)

Page | 47
{

cout<<"\n\nWant to Choose again => ";

cin>>ch;

while(ch=='y'||ch=='Y');

getch();

Q18.PROGRAM TO SORT THE GIVEN ARRAY IN ASCENDING ORDER USING

BUBBLE SORT METHOD

#include<conio.h>

void main()

{clrscr();

int A[80],n;

cin>>A[i];

for(i=0; i<n; i++)

cout<<A[i]<<" ";

Page | 48
cout<<"\n\nNow Elements will be arranged in ascending order using bubble sort :\n\n";

void bubble_sort(int A[],int n);

bubble_sort(A,n);

getch();

for(int j=0; j<n-1; j++) //Bubble sorting..

{ if(A[j+1]<A[j])

{ count++;

temp=A[j+1];

A[j+1]=A[j];

A[j]=temp;

cout<<A[k]<<" ";

Q19.WRITE A PROGRAM IN C++ TO CREATE A BINARY FILE AND WRITE OBJECTS

OF CLASS BOOK TO IT AND DISPLAY THEM ON SCREEN AFTER READING FROM
THE FILE.

Page | 49
#include<conio.h>

int p;

class book {

int book_no;

char book_title[80];

float price;

float total_cost(int n)

float p;

p=n*price;

return p;

public :

void input()

cout<<"\n\nEnter book title : ";

cin.get();

cin.getline(book_title,80);

void purchase()

Page | 50
}

void display()

cout<<"\n\nTotal cost of "<<p<<" books => "<<total_cost(p)<<"

Rs\n\n\n\n";

};

void main()

{clrscr();

book b[3],p[3];

fstream bo;

cout<<"---------------------------------------------------------Enter Details----------------------------------\n";

b[i].input();

b[i].purchase();

bo.open("xy.txt",ios::out|ios::binary); // Writing to the binary file

bo.write((char *)&b[i],sizeof(b[i]));

bo.close();

bo.close();

Page | 51
}

p[i].display();

getch();

Q20.PROGRAM TO SEARCH AN ELEMENT OF AN ARRAY USING BINARY SEARCH

#include<process.h>

#include<conio.h>

void main()

{ clrscr();

cin>>n;

cin>>A[i];

cout<<A[i]<<" ";

cout<<"\n\nEnter the element to be searched : ";

cin>>p;

Page | 52
void binary_search(int A[], int n, int p); //declaration

binary_search(A,n,p);

getch();

lb: L=0; U=n-1;

while(L<=U) //i.e loop will continue if L<=u. if L>U loop will end

{ mid=(L+U)/2;

if(A[mid]==p)

cout<<"\n\nSubscript = "<<mid<<" \n\nPosition = "<<mid+1;

break;

else if(p<=A[mid])

U=mid-1;

else

L=mid+1;

}//end of while loop

if(L>U)

{cout<<"\n\nUnsuccessful search.";

cout<<"\n\n\n\nWant to search again. : "; cin>>ch;

if(ch=='y'||ch=='Y')

cin>>p;

Page | 53
goto lb;}

else

exit(1);

Q21.WRITE A PROGRAM TO CREATE A TEXT FILE TO INPUT ROLL NO. AND

MARKS OF FIVE STUDENTS AND DISPLAY THEM ON SCREEN AFTER READING
FROM THE TEXT FILE USING DATA FILE HANDLING.

#include<conio.h>

void main()

clrscr();

cout<<"\n\tEnter the marks of student "<<i+1<<" => "; cin>>b[i];

ofstream student;

student.open("stud.txt");

Page | 54
student.close();

i=0;

cout<<”\n\nStudent details are :\n\n”;

char str[80][80];

ifstream stude;

stude.open("stud.txt",ios::in);

stude.seekg(0);

while(!stude.eof())