0% found this document useful (0 votes)
82 views

Computer Science Practical File Class 12

This document contains code and text related to file handling in C++. It includes code for handling binary data files using classes and reading/writing data to a binary file. It also includes code for handling text files, reading a string from the user and writing it to a text file. The document provides examples of reading and writing data to both binary and text files in C++.

Uploaded by

MagiC
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Computer Science Practical File Class 12

This document contains code and text related to file handling in C++. It includes code for handling binary data files using classes and reading/writing data to a binary file. It also includes code for handling text files, reading a string from the user and writing it to a text file. The document provides examples of reading and writing data to both binary and text files in C++.

Uploaded by

MagiC
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 66

{ D E L H I P U B L I C S C H O O L

D W A R K A }

C++ #include<dpsdw.h>
void main() {

cout<<”I love C++”;

COMPUTER SCIENCE
dfvdfvdvvdfPractical Filefdfvdfvdf

cout<<”Khush Malik”<<endl;
cout<<”Class: 12th C”<<endl;
cout<<”Roll no: 15”;
dCertificate. /

This is hereby to certify that, the original and genuine project


work has been

carried out and completed solely, sincerely and satisfactorily


by Khush Malik, a student

of class 12th C under the roll number __________________ for


the academic session

2019 - 20 for the Computer Science department under the direct


supervision of the

undersigned as per the requirement for the Board Examination.

_________________________
Teacher in-charge
vfAcknowledgment.d
The successful completion of any task would be
incomplete without mentioning the names of those
persons who helped to make it possible. I take this
opportunity to express my gratitude in few words and
respect to all those who helped me in the completion
of this project.

It is my humble pleasure to acknowledge my deep sense


of gratitude to my Computer Science teacher, Mr. Amit
Dua for her valuable support, constant help and
guidance at each and every stage, without which this
project would not have come forth. I would also like
to thank my friends and family for encouraging me
during the course of this project.

Last but not the least, I would like to thank CBSE


for giving me the opportunity to undertake this
project.
aINDEX ++v
S. No TOPIC Signature

1 Number Conversion

2 Searching
3 Sorting
4 2-D Array address mapping
5 Data file handling (Binary)
6 Data file handling (Text)
7 Classes (Inline and offline functions)

8 Classes (Friends)
9 Classes(Inheritance)
10 Classes (constructor and destructor)
11 Classes (Feature of OOPS)
12 This pointer
13 Boolean Algebra
14 Circular queue
15 2-D Array
16 String
17 Switch Case (Menu driven program)
18 Loops (For, While and Do While)
19 Queue Code
20 Postfix to infix
21 SQL
d<NumberConversion.cpp>s

#include<iostream.h>
#include<conio.h>
#include<process.h>

float deci(float x) {

return (x-(int)x);

void dec(float x, int b) {

int i=0;
char rem[9]={0},a[6]={'A','B','C','D','E','F'};
while(i!=8 &&(deci(x))!=0)
{
x*=b;
if((int)x<=9)
rem[i]=(int)x+48;
else
rem[i]=a[(int)x-10];
i++;
x=deci(x);
}
if(i>0)
{
cout<<"." ;
cout<<rem ;
}

void conv(int x,int b) {

char rem[64]={0}, a[6]={'A','B','C','D','E','F'};


int i=0;
while(x>0)
{
int temp=x%b;
if(temp<10)
rem[i]=temp+48;
else
rem[i]=a[temp-10];
x/=b;
i++;
}i--;
for(int j=i;j>-1;j--)
cout<<rem[j];

void menu() {

float x;
int op;
do{
clrscr();
cout<<"Enter No.\n";
cin>>x;
cout<<"1. Binary\n 2. Octal\n 3. Hex\n 4. Exit\n";
cin>>op;
switch(op)
{
case 1:{
conv((int)x,2);
dec(deci(x),2);
cout<<endl;
break;
}
case 2:{
conv((int)x,8);
dec(deci(x),8);
cout<<endl;
break;
}
case 3:{
conv((int)x,16);
dec(deci(x),16);
cout<<endl;
break;
}
case 4: exit(0);
}getch();
}while(x!=4);

}
void main() {

clrscr();
menu();
getch();

OUTPUT

d<Searching.cpp>s
#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
gotoxy(30,1);
cout<<"Arrays\n";
cout<<"There are two types of arrays\n";
cout<<"1D array\n";
cout<<"There are 2 types of searches \n";
cout<<"1. linear search 2. binary search\n";
cout<<"2D array\n";
int ch;
cin>>ch;
switch(ch)
{
case 1: int a[20],size,flag=0,i,num,pos;
cout<<"\nEnter the number of elements in the array:";
cin>>size;
cout<<"\nEnter the elements of array(in ascending order):";
for(i=0;i<size;i++)
cin>>a[i];
cout<<"\nEnter the element to be searched";
cin>>num;
for(i=0;i<size;i++)
if(a[i]==num)
{
flag=1;
pos=i;
break;
}
if(flag==0)
cout<<"\n Element not found";
else
cout<<"\nElement found at position"<<(pos+1);
break;

case 2: cout<<"\nBinary search\n";


int f[8]={1,2,3,4,5,6,7,8};
int z=0,l=0,u=7,s,m;
cout<<"Enter element to be searched";
cin>>s;
while(l<=u)
{
z=1;
m=(l+u)/2;
if(f[m]==s)
{
cout<<"\nElement found at"<<m;
}
else if(f[m]>s)
u--;
else
l++;
}
if(z==0)
cout<<"not found";
break;

case 3 : int A[3][3],c,r;


cout<<"2-D Arrays\n";
for(r=0;r<3;r++)
for(c=0;c<3;c++)
cin>>A[r][c];
for(r=0;r<3;r++)
for(c=0;c<3;c++)
cout<<A[r][c]<<endl;
break;
}
getch();
}

OUTPUT
d<Sorting.cpp>s
#include<iostream.h>
#include<conio.h>
#include<limits.h>
void main()
{
int a[10],b[5],j=0,l=0,la=0,i=0,m=0,r[11],aa[15];
while(m!=5)
{
clrscr();
cout<<"1. Bubble\n2. Selection\n3. Insertion\n4. Merge\n5.
Exit\n";
cin>>m;
if(m==1)
{
cout<<"Enter array [ Size 10 ]\n";
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"enter\n1. assending\n2. descending\n";
cin>>la;
if(la==1)
{
for(i=9;i>=0;i--)
{
for(int j=0;j<i;j++)
{
if(a[j]>a[j+1])
{
l=a[j];
a[j]=a[j+1];
a[j+1]=l;
}
}
}
}
else if(la==2)
{
for(i=9;i>=0;i--)
{
for(int j=0;j<i;j++)
{
if(a[j]<a[j+1])
{
l=a[j];
a[j]=a[j+1];
a[j+1]=l;
}
}
}
}
else
{
cout<<"Wrong choice\n";
}
if(la==1||la==2)
{
cout<<"Sorted array \n";
for(int i=0;i<10;i++)
{
cout<<"\t"<<a[i];
}
}
}
else if(m==2)
{
cout<<"Enter array [ size 10 ]\n";
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"enter\n1. assending\n2. descending\n";
cin>>la;
if(la==1)
{
for(i=10;i<10;i++)
{
for(int j=i;j<10;j++)
{
if(a[i]>a[j])
{
l=a[i];
a[i]=a[j];
a[j]=l;
}
}
}
}
else if(la==2)
{
for(i=0;i<10;i++)
{
for(int j=i;j<10;j++)
{
if(a[i]<a[j])
{
l=a[i];
a[i]=a[j];
a[j]=l;
}
}
}
}
else
{
cout<<"Wrong choice\n";
}
if(la==1||la==2)
{
cout<<"Sorted array \n";
for(int i=0;i<10;i++)
{
cout<<"\t"<<a[i];
}
}
}
else if(m==3)
{
cout<<"Enter array [ Size 10 ]\n";
for(i=0;i<10;i++)
{
cin>>r[i+1];
}
cout<<"enter\n1. assending\n2. descending\n";
cin>>la;
if(la==1)
{
r[0]= INT_MIN;
for(i=11;i>0;i--)
{
for(int j=i=1;j>0;j--)
{
if(r[i]>r[j])
{
l=r[i];
r[i]=r[j];
a[j]=l;
}
}
}
}
else if(la==2)
{
for(i=11;i>0;i--)
{
for(int j=i-1;j>0;j--)
{
if(r[i]<r[j])
{
l=r[i];
r[i]=r[j];
a[j]=l;
}
}
}
}
else
{
cout<<"Wrong choice\n";
}
if(la==1||la==2)
{
cout<<"Sorted array \n";
for(int i=1;i<11;i++)
{
cout<<"\t"<<r[i];
}
}
}
else if(m==4)
{
cout<<"Enter array [ Size 10 ]\n";
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"Enter array [ Size 5 ]\n";
for(i=0;i<5;i++)
{
cin>>b[i];
}
cout<<"enter\n1. assending\n2. descending\n";
cin>>la;
if(la==1)
{
for(i=0;i<10;i++)
{
for(int j=i;j<10;j++)
{
if(a[i]>a[j])
{
l=a[i];
a[i]=a[j];
a[j]=l;
}
}
}
for(i=0;i<5;i++)
{
for(int j=i;j<5;j++)
{
if(b[i]>b[j])
{
l=b[i];
b[i]=b[j];
b[j]=l;
}
}
}
for(i=0,j=0;i<10,j<5;)
{
if(a[i]<b[j])
{
aa[i+j]=a[i];
i++;
}
else
{
aa[i+j]=b[j];
j++;
}
}
if(j!=5)
{
while(j!=5)
{
aa[10+j]=b[j];
j++;
}
}
else
{
while(i!=10)
{
aa[5+i]=a[i];
i++;
}
}
}
else if(la==2)
{
for(i=0;i<10;i++)
{
for(int j=i;j<10;j++)
{
if(a[j]<a[j])
{
l=a[i];
a[i]=a[j];
a[j]=l;
}
}
}
for(i=0;i<5;i++)
{
for(int j=i;j<5;j++)
{
if(a[i]<a[j])
{
l=b[i];
b[i]=b[j];
b[j]=l;
}
}
}
for(i=0,j=0;i<10,j<5;)
{
if(a[i]>b[j])
{
aa[i+j]=a[i];
i++;
}
else
{
aa[i+j]=b[j];
j++;
}
}
if(j!=5)
{
while(j!=5)
{
aa[10+j]=b[j];
j++;
}
}
else
{
while(i!=10)
{
aa[5+i]=a[i];
i++;
}
}
}
else
{
cout<<"Wrong choice\n";
}
if(la==1||la==2)
{
cout<<"Sorted array \n";
for(int i=0;i<10;i++)
{
cout<<"\t"<<a[i];
}
}
}
else if(m!=5)
{
cout<<"Wrong choice\n";
getch(); } }

OUTPUT
d<AddressMapping.cpp>s
#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int opt,base,storage,n,m,addr,i,j;
cout<<"1. column major"<<endl;
cout<<"2. row major"<<endl;
cin>>opt;
if(opt==1)
{
cout<<"enter the location"<<endl;
cin>>i>>j;
cout<<"enter the base address"<<endl;
cin>>base;
cout<<"enter the size of element"<<endl;
cin>>storage;
cout<<"enter the number of rows"<<endl;
cin>>m;
cout<<"enter the number of columns"<<endl;
cin>>n;
cout<<endl;
addr=base+storage*(n*i+j);
}
else if(opt==2)
{
cout<<"enter the location"<<endl;
cin>>i>>j;
cout<<"enter the base address"<<endl;
cin>>base;
cout<<"enter the size of element"<<endl;
cin>>storage;
cout<<"enter the number of rows"<<endl;
cin>>n;
cout<<"enter the number of columns"<<endl;
cin>>m;
cout<<endl;
addr=base+storage*(n*j+i);
}
else
cout<<"invalid input";
cout<<"you addr is"<<addr;
getch();
}
OUTPUT

d<DataFileHandling_Binary.cpp>s
#include<fstream.h>
#include<conio.h>

class stud
{
int roll_no;
char name;
float marks;
public:
void getdata()
{
cin>>roll_no;
cin>>name;
cin>>marks;
}
void showdata()
{
cout<<roll_no<<endl<<name<<marks<<endl;
}
int ret_roll()
{
return roll_no;
}
}s;
void insert()
{
ofstream ofile;
ofile.open("aman.dat",ios::app);
ofile.write((char *)&s,sizeof(s));
s.getdata();
ofile.close();
}
void display()
{
ifstream ifile;
ifile.open("aman.dat");
while(ifile.read((char *)&s,sizeof(s)))
s.showdata();
ifile.close();
}

void main()
{
clrscr();
char ans;
do{
cout<<"1. Add data"<<endl;
cout<<"2. Display data"<<endl;
cout<<"3. Exit"<<endl;
cout<<"Enter your choice"<<endl;
int ch;
cin>>ch;
switch(ch)
{
case 1: insert();
break;
case 2: display();
break;
}
cout<<"Do you want to continue?";
cin>>ans;
}while(ans!='n');
getch();
}

OUTPUT
d<DataFileHandling_Text.cpp>s

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

void main()
{
clrscr();
char z[1000],a,b[20],c[1000];
int j=0;
fstream az("text.txt",ios::out);
cout<<"\nEnter string: ";
gets(z);
for(j=0;j<strlen(z);j++)
{
az.put(z[j]);
}
az.close();
for(int i=0;i!=4;i++)
{
ifstream aa("text.txt");
j=0;
clrscr();
cout<<"1. count of vowels\n2. count of or\n3. cout of line\n4.
exit\n";
cin>>i;
if(i==1)
{
while(!aa.eof())
{
aa.get(a);
if(a=='A'||a=='E'||a=='I'||a=='O'||a=='U'||a=='a'||a=='e'||a=='i
'||a=='o'||a=='u')
{
j++;
}
}
cout<<"no. of vowels are"<<j;
}
else if(i==2)
{
while(!aa.eof())
{
aa>>b;
if(strcmpi(b,"or")==0)
{
j++;
}
}
cout<<"no. of or are "<<j;
aa.close();
}
else if(i==3)
{
while(!aa.eof())
{
aa.getline(c,1000);
j++;
}
cout<<"no. of lines are "<<j;
}
else if(i!=4)
{
cout<<"Wrong choice";
}
aa.close();
getch();
}
getch();
}
OUTPUT

d<Classes_InlineOffline.cpp>s
#include<iostream.h>
#include<conio.h>
#include<string.h>

class taxi
{
int taxinumber;
float distance;
char startlocation[20],destination[20];
public:
void allocate()
{
cout<<"Enter the taxi number: ";
cin>>taxinumber;
cout<<"Enter the place from where the taxi is boarded: ";
cin>>startlocation;
cout<<"Enter the distance: ";
cin>>distance;
cout<<"Enter the place where the taxi is to be deboarded: ";
cin>>destination;
}
~taxi()
{
cout<<"\n\n\n\n\t\tTHANK YOU";
}
void show();
};
void taxi::show()
{
cout<<"\n\n\t\tDetails of the taxi are-"<<"\ntaxi number is:
"<<taxinumber<<"\nPlace o]where taxi will be boarded:
"<<startlocation<<"\nDistance between the places: "<<distance;
};

void main()
{
clrscr();
taxi mycab;
mycab.allocate();
clrscr();
mycab.show();
getch();
}

OUTPUT
d<ClassesFriends.cpp>s
#include<iostream.h>
#include<conio.h>

class data1
{
int a,b;
public:
void get1();
friend void sum(data1,data2);
};
class data2
{
int c,d;
public;
void get2();
class display
{
int s;
public:
void sum(int a, int b)
{
s=a+b;
}
void show()
{
cout<<"\nSum of a and b is:: "<<s;
}
};
friend void sum(data1,data2);
};
void sum(data1 a1,data2 a2)
{
cout<<"a1.a+a2.c"<<a1.a+a2.c<<endl;
cout<<"a1.b+a2.d"<<a1.b+a2.d<<endl;
}
void data1::get1()
{
cout<<"Enter value for a: ";
cin>>a;
cout<<"Enter value for b: ";
cin>>b;
}
void data2::get2()
{
cout<<"Enter value for c: ";
cin>>c;
cout<<"Enter value for d: ";
cin>>d;
}

void main()
{
clrscr();
data1 a;
data2 b;
a.get1();
b.get2();
sum(a,b);
data2::display x;
cout<<"ENTER THE VALUE IN P AND Q :";
int p,q;
cin>>p>>q;
x.sum(p,q);
x.show();
getch();
}

OUTPUT

d<ClassesInheritance.cpp>s
#include<iostream.h>
#include<conio.h>

class a
{
public:
a()
{
cout<<"a"<<endl;
}
~a()
{
cout<<"A"<<endl;
}
}oba;

class b:public a
{
public:
b()
{
cout<<"b"<<endl;
}
~b()
{
cout<<"B"<<endl;
}
}obb;

class c: public b
{
public:
c()
{
cout<<"c"<<endl;
}
~c()
{
cout<<"C"<<endl;
}
}obc;

void main()
{
clrscr();
c.obc;
getch();
}

OUTPUT
d<ClassesConstructorDestructor.cpp>s
#include<iostream.h>
#include<conio.h>
#include<string.h>
class myclass{
int a;
char b;
public:
myclass()
{
cout<<"Default Constructor Called\n";
add();
}
myclass(int x,char y)
{
cout<<"Parameterized Constructor Called\n";
a=x;
b=y;
}
myclass(myclass &x)
{
cout<<"Copy Constructor Called\n";
a=x.a;
b=x.b;
}
void add()
{
cout<<"\nEnter an integer value: ";
cin>>a;
cout<<"\nEnter a character value: ";
cin>>b;
cout<<endl;
}
void display()
{
cout<<"\nProvided values are\n";
cout<<a<<endl<<b<<endl;
}
~myclass()
{
cout<<"\n\tDestructor called"<<endl;
}
};

void main()
{
clrscr();
myclass first;
clrscr();
first.display();
myclass second(25,'A');
second.display();
myclass third=first;
third.display();
getch();
}

OUTPUT
d<ClassesFeaturesOOPS.cpp>s
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class competition
{
int event_no,score,noofschools;
char description[30];
char qualified;
public:
competition()
{
noofschools=10;
}
void input()
{
cout<<"Enter your event number and description ";
cin>>event_no>>description;
}
void award()
{
cout<<"Enter your score";
cin>>score;
if(score>87)
{
cout<<"Y"<<endl;
}
else
{
cout<<"N"<<endl;
}
}
void show()
{
cout<<"The number of schools participating are:
"<<noofschools<<endl;
cout<<event_no<<endl<<description<<endl<<score<<endl;
}
};

void main()
{
clrscr();
competition d;
d.input();
d.award();
d.show();
getch();
}

OUTPUT
d<Pointer_This.cpp>s

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct student{
int rollno;
char name[20];
void showdata();
void getdata();
};
void student::showdata()
{
ifstream file_input;
file_input.open("TXT.txt");
while(file_input.read((char*) this , sizeof(*this)))
{
cout<<"\nRoll number is: "<<rollno<<endl;
cout<<"\nName is: "<<name<<endl;
}
file_input.close();
getch();
}
void student::getdata()
{
cout<<"\nEnter roll number: ";
cin>>rollno;
cout<<"\nEnter name: ";
cin>>name;
ofstream file_output;
file_output.write((char*) this , sizeof(*this));
file_output.close();
getch();
}
void remove()
{
remove("TXT.txt");
cout<<"\nData has been removed and the file is empty!\n";
getch();
}
void main()
{
clrscr();
int choice=0;
while(choice!=4)
{
clrscr();
cout<<"\t\t\nProgram for 'This' pointer";
cout<<"\n1. Enter a new record\n2. View all records\n3. Delete
all records\n4. Exit\n";
cout<<"Enter your choice: \n";
cin>>choice;
student temp;
switch(choice)
{
case 1: temp.getdata();
break;
case 2: temp.showdata();
break;
case 3: remove();
break;
case 4: exit(0);
default:
cout<<"\nInvalid input!\n";
}
}
getch();
}
OUTPUT
d<BooleanAlgebra.cpp>s

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
#include<math.h>
void andgate()
{
clrscr();
int a,b;
cout<<"\nEnter Two Inputs(0,1):\n";
cin>>a>>b;
int p=a&b;
cout<<"Output is:";
cout<<p;

gotoxy(32,6);
cout<<"TRUTH TABLE";
gotoxy(25,8);
cout<<"_______________";
gotoxy(25,9);
cout<<"| a | b | o/p |";
gotoxy(25,10);
cout<<"| 1 | 1 | 1 |";
gotoxy(25,11);
cout<<"| 1 | 0 | 0 |";
gotoxy(25,12);
cout<<"| 0 | 1 | 1 |";
gotoxy(25,13);
cout<<"| 0 | 0 | 0 |";
gotoxy(25,14);
cout<<"_______________";
getch();
}
void orgate()
{
clrscr();
int a,b;
cout<<"\nEnter Two Inputs(0,1):\n";
cin>>a>>b;
int p=a|b;
cout<<"Output is:";
cout<<p;

gotoxy(32,6);
cout<<"TRUTH TABLE";
gotoxy(25,8);
cout<<"_______________";
gotoxy(25,9);
cout<<"| a | b | o/p |";
gotoxy(25,10);
cout<<"| 1 | 1 | 1 |";
gotoxy(25,11);
cout<<"| 1 | 0 | 1 |";
gotoxy(25,12);
cout<<"| 0 | 1 | 1 |";
gotoxy(25,13);
cout<<"| 0 | 0 | 0 |";
gotoxy(25,14);
cout<<"_______________";
getch();
}
void notgate()
{
clrscr();
int a;
cout<<"\nEnter The Inputs(0,1):\n";
cin>>a;
cout<<"Output is:";
cout<<!a;

gotoxy(32,6);
cout<<"TRUTH TABLE";
gotoxy(25,8);
cout<<"___________";
gotoxy(25,9);
cout<<"| a | o/p |";
gotoxy(25,10);
cout<<"| 1 | 0 |";
gotoxy(25,11);
cout<<"| 1 | 1 |";
gotoxy(25,12);
cout<<"___________";
getch();
}
void nandgate()
{
clrscr();
int a,b;
cout<<"\nEnter Two Inputs(0,1):\n";
cin>>a>>b;
int p=a&b;
cout<<"Output is:";
cout<<!p;

gotoxy(32,6);
cout<<"TRUTH TABLE";
gotoxy(25,8);
cout<<"_______________";
gotoxy(25,9);
cout<<"| a | b | o/p |";
gotoxy(25,10);
cout<<"| 1 | 1 | 0 |";
gotoxy(25,11);
cout<<"| 1 | 0 | 1 |";
gotoxy(25,12);
cout<<"| 0 | 1 | 1 |";
gotoxy(25,13);
cout<<"| 0 | 0 | 1 |";
gotoxy(25,14);
cout<<"_______________";
getch();
}
void norgate()
{
clrscr();
int a,b;
cout<<"\nEnter Two Inputs(0,1):\n";
cin>>a>>b;
int p=a|b;
cout<<"Output is:";
cout<<p;

gotoxy(32,6);
cout<<"TRUTH TABLE";
gotoxy(25,8);
cout<<"_______________";
gotoxy(25,9);
cout<<"| a | b | o/p |";
gotoxy(25,10);
cout<<"| 1 | 1 | 0 |";
gotoxy(25,11);
cout<<"| 1 | 0 | 0 |";
gotoxy(25,12);
cout<<"| 0 | 1 | 0 |";
gotoxy(25,13);
cout<<"| 0 | 0 | 1 |";
gotoxy(25,14);
cout<<"_______________";
getch();
}
void xorgate()
{
clrscr();
int a,b;
cout<<"\nEnter Two Inputs(0,1):\n";
cin>>a>>b;
cout<<"Output is:";
if(a==b)
{
cout<<"0";
}
else
{
cout<<"1";
}
gotoxy(32,6);
cout<<"TRUTH TABLE";
gotoxy(25,8);
cout<<"_______________";
gotoxy(25,9);
cout<<"| a | b | o/p |";
gotoxy(25,10);
cout<<"| 1 | 1 | 0 |";
gotoxy(25,11);
cout<<"| 1 | 0 | 1 |";
gotoxy(25,12);
cout<<"| 0 | 1 | 1 |";
gotoxy(25,13);
cout<<"| 0 | 0 | 0 |";
gotoxy(25,14);
cout<<"_______________";
getch();
}
void xnorgate()
{
clrscr();
int a,b;
cout<<"\nEnter Two Inputs(0,1):\n";
cin>>a>>b;
cout<<"Output is:";
if(a==b)
{
cout<<"1";
}
else
{
cout<<"0";
}
gotoxy(32,6);
cout<<"TRUTH TABLE";
gotoxy(25,8);
cout<<"_______________";
gotoxy(25,9);
cout<<"| a | b | o/p |";
gotoxy(25,10);
cout<<"| 1 | 1 | 1 |";
gotoxy(25,11);
cout<<"| 1 | 0 | 0 |";
gotoxy(25,12);
cout<<"| 0 | 1 | 0 |";
gotoxy(25,13);
cout<<"| 0 | 0 | 1 |";
gotoxy(25,14);
cout<<"_______________";
getch();
}

void main()
{
clrscr();
gotoxy(30,5);
cout<<"Logic Gates";
gotoxy(30,8);
cout<<"1. And gate";
gotoxy(30,9);
cout<<"2. Or gate";
gotoxy(30,10);
cout<<"3. Not gate";
gotoxy(30,11);
cout<<"4. Nand gate";
gotoxy(30,12);
cout<<"5. Nor gate";
gotoxy(30,13);
cout<<"6. Xor gate";
gotoxy(30,14);
cout<<"7. Xnor gate";
gotoxy(30,15);
cout<<"8. Exit";
gotoxy(30,16);
cout<<"Enter your choice";
int ch;
cin>>ch;
switch(ch)
{
case 1:andgate();
break;
case 2:orgate();
break;
case 3:notgate();
break;
case 4:nandgate();
break;
case 5:norgate();
break;
case 6:xorgate();
break;
case 7:xnorgate();
break;
case 8:exit(0);
}
getch();
}
OUTPUT
d<CircularQueue.cpp>s

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

int q[100],sz,frnt=0,rear=0;
void remove()
{
if(frnt==rear)
cout<<"\n QUEUE IS EMPTY!\n";
else
{
frnt=(frnt+1)%sz;
cout<<q[frnt]<<"IS DELETED\n";
}
getch();
}
void display()
{
int i=frnt;
if(frnt==rear)
cout<<"\n QUEUE IS EMPTY!\n";
else
{
while(i!=rear)
{
i=(i+1)%sz;
cout<<q[i]<<endl;
}
}
getch();
}
void insert()
{
int x;
cout<<"\nENTER ELEMENT\n";
cin>>x;
if((rear+1)%sz==frnt)
cout<<"\nQUEUE!\n";
else
{
rear=(rear+1)%sz;
q[rear]=x;
}
getch();
}
void main()
{
clrscr();
int test=0;
cout<<"\nENTER SIZE OF QUEUE\n";
cin>>sz;
while(test!=4)
{
clrscr();
cout<<"\t\t\nTHIS IS CIRCULAR QUEUE\n";
cout<<"\n1. ADD A NEW RECORD\n2. DELETE A RECORD\n3. SHOW ALL
RECORDS\n4. EXIT";
cout<<"\nENTER OPTION\n";
cin>>test;
switch(test)
{
case 1:insert();
break;
case 2:display();
break;
case 3:remove();
break;
case 4: exit(0) ;
default:
cout<<"\nWRONG OPTION!\n";
}
}
getch();
}

OUTPUT
d<2DArray.cpp>s
#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
cout<<"Enter the number\n";
int a[3][3],c,r;
for(r=0;r<3;r++)
for(c=0;c<3;c++)
{
cin>>a[r][c];
}
cout<<"1. The general array\n";
cout<<"2. To show diagonals\n";
cout<<"3. Formation of triangle and the sum of numbers in
triangls\n";
cout<<"Enter the option\n";
int ch;
cin>>ch;
switch(ch)
{
case 1: cout<<"The numbers are\n";
for(r=0;r<3;r++) //to show all numbers
present
for(c=0;c<3;c++)
cout<<a[r][c]<<endl;
break;
case 2: cout<<"The numbers are\n";
for(r=0;r<3;r++) //to show numbers present
in diagonals
for(c=0;c<3;c++) //(0,0) (0,2) (1,1) (1,0)
(2,0)
if(r==c||r+c==2)
cout<<a[r][c]<<endl;
break;
case 3: cout<<"The sum of the numbers are\n";
int sum=0; //to show numbers forming a
triangle
for(r=0;r<3;r++) //(,) (,) (,) (,) (,)
for(c=0;c<3;c++)
if(r<=c)
sum= sum + a[r][c];
cout<<sum;
break;
}
getch();
OUTPUT

d<String.cpp>s
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

void main()
{
clrscr();
char a[100],b[100],c[100];
cout<<"Enter the string\n";
gets(a);
int ch,i,j,c=0,temp=0;
cout<<"Enter the option\n";
cin>>ch;
switch(ch)
{
case 1: cout<<"The general way of entering a string\n";
cout<<a;
break;
case 2: cout<<"To determine the string length\n";
cout<<strlen(a);
break;
case 3: cout<<"To find number of words\n";
for(i=0;a[i]!='\0';i++)
if(a[i]=' ')
c++;
cout<<c;
break;
case 4:cout<<"To copy the string from 1st array to the
second\n";
for(i=0;a[i]!='\0';i++)
temp=a[i];
a[i]=b[i];
b[i]=temp;
cout<<b;
break;
}
getch();
}

OUTPUT
d<Switch.cpp>s
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<math.h>
#include<process.h>
void main()
{
clrscr();
cout<<"Choose one of the following questions\n";
cout<<"1. Question 1 - Age of voting\n";
cout<<"2. Question 2 - Greater number\n";
cout<<"3. Question 3 - exponetial power\n";
cout<<"4. Question 4 - square root of number\n";
cout<<"5. Exit\n";
int ch;
cin>>ch;
switch(ch)
{
case 1 : cout<<"Eligibility for voting\n";
cout<<"Enter your age\n";
int x;
cin>>x;
{
if (x>=18)
cout<<"You can vote";
else
cout<<"You cannot vote";
}
break;
case 2 : cout<<"Greater number\n";
int a,b;
cout<<"Enter 1st number";
cin>>a;
cout<<"Enter 2nd number";
cin>>b;
{
if (a>=b)
cout<<"1st number is greater than or equal to 2nd";
else
cout<<"2nd number is greater than 1st";
break;
}
case 3 : cout<<"Exponenetial power\n";
int p,q,r;
cout<<"Enter the base number\n";
cin>>p;
cout<<"Enter the power the number is to be raised\n";
cin>>q;
r= pow(p,q);
cout<<"The answer is =\n";
cout<<r;
break;
case 4 : cout<<"Square root of a number\n";
int l,m;
cout<<"Enter number\n";
cin>>l;
m=sqrt(l);
cout<<"Square root of the number is=";
cout<<m;
break;
case 5 : cout<<"Exit";
exit (0);
break;
}
getch();
}

OUTPUT
d<Loops.cpp>s

#include<iostream.h>
#include<conio.h>
#include<dos.h>

void main()
{
clrscr();
gotoxy(30,1);
cout<<"Loops\n";
cout<<"There are 3 types of loops\n";
cout<<"1. For loop \t\t 2. While loop \t\t 3. Do While loop\n";
cout<<"choose one of the following type to see an example\n";
int ch;
cin>>ch;
switch(ch)
{
case 1 : cout<<"Example of for loop\n";
cout<<"numbers from 1-9\n";
int a;
cout<<" for(a=1;a<10;a++) ";
cout<<"{cout<<a;}";
delay(1000);
for(a=1;a<10;a++)
{
cout<<a;
}
break;

case 2 : cout<<"Example of while loop\n";


cout<<"factorial\n";
delay(1000);
int i,b,c=1;
cout<<"enter integer\n" ;
cin>>b;
i=b;
while(b)
{
c*=b;
--b;
}
cout<<"factorial of"<<i<<"is"<<c<<"\n";
break ;

case 3 : cout<<"example of do while loop";


cout<<"numbrs from 1-10\n";
int d=1;
do
{
cout<<endl<<d;
d++;
}
while (d<=10);
break;
}
getch();
}
OUTPUT

d<Queue.cpp>s

#include<iostream.h>
#include<conio.h>

int queue[5], rear=-1, front=-1;


void insert()
{
if(front==4)
cout<<"\nFull";
else
{
front++;
cout<<"\n Enter no. ";
cin>>queue[front];
}
}
void del()
{
if(rear==front)
cout<<"\nEmpty";
else
{
rear++;
cout<<queue[rear]<<", ";
}
}
void show()
{
if(rear==front)
cout<<"\nEmpty";
else
{
int i=rear;
while(i<front)
{
i++;
cout<<queue[rear]<<", ";
}
}
}
void main()
{
int i=0;
while(i!=4)
{
clrscr();
cout<<"Enter your choice:\n1. insert\n2. delete\n3. show\n4.
exit\n";
cin>>i;
if(i==1)
insert();
else if(i==2)
del();
else if(i==3)
show();
else if(i!=4)
cout<<"\nincorrect choice";
getch();
}
}
OUTPUT
d<PostfiInfix.cpp>s

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<math.h>

char arr[50],c,d;
float i, top=-1,a,b,temp,stack[50]={NULL};
void main()
{
clrscr();
cout<<"Enter in postfix from :"<<endl;
gets(arr);
int l=strlen(arr);
cout<<"\nINPUT\t\tSTACK\n";
for(i=0;i<l;i++)
{
if((arr[i]>='0')&&(arr[i]<='9'))
{top++; stack[top]=arr[i]-48;}
if((arr[i]=='^')||(arr[i]=='*')||(arr[i]=='-
')||(arr[i]=='+')||(arr[i]=='/'))
{
a=stack[top];
b=stack[top-1];
c=arr[i];
temp=((c=='^')? pow((int)b,(int)a): ((c=='+') ?b+a:((c=='-') ?b-
a : ((c=='*') ?b*a : ((c=='/') ?b/a : '\0')))));
top--;
stack[top]=temp;
}
cout<<endl<<arr[i]<<"\t\t"<<stack[top];
}
cout<<"\n Answer is:"<<stack[top];
getch();
}

OUTPUT

You might also like