Sunteți pe pagina 1din 50

To Calculate Sum of Series:

X2/2! + X4/4! + .->


#includeiostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float f,a,b,j,s,x,I,n;
f=1;
s=x=0;
cout<<Enter Num & limit<<endl;
cin>>x>>n;
for(i=1;i<=2*n;i=i+2)
{
for(j=1;j<=2*I;j++)
{
f=f*j;
}
S=(s+pow(x,2*i)/f));
}
cout<<sum=<<s;
getch();
}

To check wether number is an


Armstrong number ->
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int r;
long int n,c,sum,temp;
n=sum=0;
cout<<Enter the maximum range upto which you want to
find armstrong numbers\n;
cin>>n;
cout<<Following armstrong numbers are found from 1 to
<<n<<endl;
for(c=1;c<=n;c++)
{
sum=0;
temp=c;
while(temp!=0)
{
r=temp%10;
sum=sum+r*r*r;
temp=temp/10
}
if(c==sum)
cout<<c<<endl;
}
getch();
}

To check whether the


number is even or odd or
prime->
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int e;
char ch;
Do
{
cout<<Enter a no.:<<endl;
cin>>e;
if(e%e==0||e%1==e)
{
cout<<The no. is prime;
}
if(e%2==0)
{
cout<<\nThe no is even <<endl;
}
else
{
cout<<\nThe no is odd<<endl;
}
cout<<\Want to see more(y/n)-;
cin>>ch;
}
while(ch==y);
getch();
}

To Calculate Sum of Series:


X2/2! + X4/4! + .->
#includeiostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float f,a,b,j,s,x,I,n;
f=1;
s=x=0;
cout<<Enter Num & limit<<endl;
cin>>x>>n;
for(i=1;i<=2*n;i=i+2)
{
for(j=1;j<=2*I;j++)
{
f=f*j;
}
S=(s+pow(x,2*i)/f));
}
cout<<sum=<<s;
getch();
}

To print Random Numbers ->


#include<stdlib.h>
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
randomize();
int num,rndnum;
cin>>num;
rndnum=random(num)+14;
for( int n = 1;n<=rndnum;n++)
{
Cout<<n<< ;
}
getch();
}

Program based on
Constructor & Destructor ->
#include<iostream.h>
#include<stdio.>
class abc
{
int a,b;
float c;
public:
abc();
abc(int,char);
abc(abc&);
~abc();
void print();
}
abc::abc()
{
a=17;
b=y;
c=10.123;
}
abc::abc(int x,char y)
{
a=x;
b=y;
c=27.17;
}
abc::abc(abc&x)
{
a=x.a;
b=x.b;
}
~abc::abc()
{
cout<<Object Deallocated<<endl;
}
void abc::print()
{
cout<<a=<<a;
cout<<b=<<b;
cout<<c=<<c<<endl;
}
void main()
{
6

clrscr();
abc obj1;
abc obj2(25,g);
abc obj3(obj1);
obj1.print();
obj2.print();
obj3.print();
getch();
}

Program Based on Friend


Function ->
#include<iostream.h>
#include<conio.h>
Class cmplx1
{
int real,imagin;
public:
void get()
{
cout<<Enter the real part:<<endl;
cin>>real;
cout<<Enter the imaginary part:<<endl;
cin>>imagin;
}
Friend void sum(cmplx1,cmplx1);
};
void sum(cmplx1,cmplx2)
{
cout<<Result:<<endl;
cout<<[<<c1.real<<+I<<c1.imagin;
cout<<] + [<<c2.real<<+I<<c1.imagin+c2.imagin;
}
void main()
{
cmplx1 op1,op2;
clrscr();
cout<<Addition of two complex numbers using friend
functions is:<<endl;
cout<<input\noperand 1;
op1.get();
cout<<\noperand 2;
op2.get();
sum(op1,op2);
getch();
}

Program based on Class data


type ->
#include<iostream.h>
#include<conio.h>
Class matrix
{
int a[4][4],b[4][4],I,j,x,y,sum;
public:
void getvalues();
void displaysum();
};
void matrix::getvalues()
{
cout<<Enter the size of the row and column<<endl;
cin>>x>>y;
if(x!=y)
{
cout<<The size of the row and column should be
equal;
getch();
exit(0);
}
cout<<Enter values for the matrix A\n;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cin>>a[i][j];
}
}
cout<<Enter values for the matrix B\n;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cin>>b[i][j];
}
}
}
void matrix::displaysum()
{
cout<<The sum of matrix A and B is\n;

for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
sum=a[i][j]+b[i][j];
}
}cout<<sum;
}
void main()
{
clrscr();
matrix m;
m.getvalues();
m.displaysum();
getch();
}

10

Program based on Array of


Structure ->
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
Struct student
{
char name[20],section;
int rollno,semester,age;
};
void main()
{
clrscr();
const int n=2;
student genius[n];
for(int i=0;i<n;i++)
{
cout<<Enter the Bio-data of the student no
<<i+1<<is:\n;
cout<<Enter name<<endl;
gets(genius[i].name);
cout<<Enter roll number<<endl;
cin>>genius[i].rollno;
cout<<Enter section<<endl;
cin>>genius[i].section;
cout<<Enter age<<endl;
cin>>genius[i].age;
}
clrscr();
for(int j=0;j<n;j++)
{
cout<<\nThe Bio-data of the student no
<<j+1<< is:\n;
cout<<Name: <<genius[j].name<<endl;
cout<<Roll No.:<<genius[j].rollno<<endl;
cout<<Section:<<genius[j].section<<endl;
cout<<Semester:<<genius[j].semester<<endl;
cout<<Age:<<genius[j].age<<endl;
}

11

getch();
}

Program based on Nested


Structure ->
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
Struct data
{
char name[25],section;
}ns;
Struct student
{
int rollno,semester,age;
data ns;
};
void main()
{
clrscr();
student genius;
cout<<Enter name<<endl;
gets(genius.ns.name);
cout<<Enter Roll Number<<endl;
cin>>genius.rollno;
cout<<Enter section<<endl;
cin>>genius.ns.section;
cout<<Enter semester<<endl;
cin>>genius.semester;
cout<Enter age<<endl;
cin>>genius.age;
clrscr();
cout<<\nName:<<genius.ns.name<<endl;
cout<<\nRoll number<<genius.rollno<<endl;
cout<<\nSection:<<genius.ns.section<<endl;
cout<<\nSemester:<<genius.semester<<endl;
cout<<\nAge:<<genius.age<<endl;
getch();
}

12

Program to convert number


from one system to another
and vice-versa ->
#include<iostream.h>
#include<conio.h>
#include<maths.h>
Void bdfunc();
Void bofunc();
Void main()
{
clrscr();
int I;
cout<<Enter a Choice:<<endl;
cout<<1:Binary to Decimal & vice-versa<<endl;
cout<<2.Binary to Octal & vice-versa<<endl;
cin>>I;
switch(i)
{
Case 1: bdfunc();
Case 2: bofunc();
}
getch();
}
Void bdfunc()
{
int a,g,h,s=0,i=0,d,b,ch;
cout<<Enter a choice<<endl;
cout<<1:Binary to Decimal<<endl;
cout<<2:Decimal to Binary<<Endl;
cin>>ch;
switch(ch)
{
Case 1: { cout<<Enter the Binary Digit<<endl;
cin>>b;
while(b>0)
{
a=b%10;
13

s=s+a*(pow(2,i));
i=i+1;
b=b/10;
}
Cout<<Answer:<<s<<endl;
}
break;
Case 2: { cout<<Enter the decimal<<endl;
cin>>d;
while(d>0)
{
a=d%2;
a=a*pow(10,i);
s=s+a;
i=i+1;
d=d/2;
}
cout<<Answer:<<s<<endl;
}
break;
Default: cout<<Not a valid choice<<endl;
}
}
Void bofunc()
{
int oc,p,r,ch,n,a,i=0,x=0d=0,j,o;
long int b,s=0,sum=0
cout<<Enter choice<<Endl;
cout<<1:Binary to Octal<<Endl;
cout<<2:Octal to Binary<<endl;
cin>>ch;
Switch(ch)
{
Case 1: { cout<<Enter the Binary digit<<endl;
Cin>>b;
While(b>0)
{
a=b%1000;
j=0;
d=0;
while(a>0)
{
n=(a%10);
d=d+(n*pow(2,i));
j=j+1;
a=a/10;
}
x=x+(d*pow(10,i);
i=i+1;

14

b=b/1000;
}
cout<<Answer:<<x;
}
break;
Case 2: { cout<<Enter the Octal<<endl;
cin>>oc;
i=0;
j=0;
sum=0;
s=0;
while(oc>0)
{
p=oc%1000;
while(p>0)
{
p=p%2;
s=s+r*pow(10,j);
j++;
p=p/2;
}
sum=sum+(s*pow(1000,i));
i++;
oc=oc/10;
i=0;
}
cout<<Binary<<sum<<endl;
}
break;
Default: cout<<Invalid choice<<endl;
}
}

15

Program based on 1-D array


->
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>
#include<dos.h>
Class array
{
int n,ar[10];
Public:
void creation(int);
void traversal();
void insertion(int,int);
void deletion(int);
void bsort();
void bsearch();
void lsearch();
void merging(int);
};
void array::creation(int no)
{
int I;
if(no>10)
{
cout<<Overflow!!;
getch();
}
else
{
cout<<\nEnter<<no<<values ->\n;
for(i=0;i<no;i++)
{
cin>>ar[i];
}
}
n=no;

16

}
void array::traversal()
{
clrscr();
int I;
for(i=0;i<n;i++)
{
cout<<setw(5)<<ar[i];
}
}
void array::insertion(int num,int pos)
{
if(n==10)
{
cout<<\nOverflow!!!\n;
getch();
}
else
{
int I;
for(i=n-1;i>=pos;i--)
{
ar[i+1]=ar[i];
}
ar[pos]=num;
n++;
}
}
void array::deletion(int pos)
{
int x;
if(n==0)
{
cout<<\nunderflow!!!\n;
getch();
}
else
{
int x;
for(x=pos;x<n;x++)
{
ar[x]=ar[x+1];
}
n--;
}
}
void array::bsort()
{
int x,y,temp;

17

for(x=0;x<n-1;x++)
{
for(y=0;y<((n-1)-x);y++)
{
if(ar[y]>ar[y+1])
{
temp=ar[y];
ar[y]=ar[y+1];
ar[y+1]=temp;
}
}
}
}
void array::bsearch()
{
bsort();
int num,pos;
cout<<\nEnter the number to be searched->\n;
cin>>num;
int beg,end,mid;
beg=0; end=n-1;
mid=(beg+end)/2;
while(num!=ar[mid]&&beg<=end)
{
if(num>ar[mid])
{
beg=mid+1;
}
else
{
end=mid-1;
}
mid=(beg+end)/2;
}
if(beg>end)
{
pos=-1;
cout<<\nNumber not found!!\n;
getch();
}
else
{
pos=mid;
cout<<endl<<wait while the list is being automatically
sorted!!\n;
delay(2000);
cout<<Number found at position ->pos+1;
}
getch();

18

}
void array::lsearch()
{
int i,pos,num;
int flag=0;
cout<<\nEnter the number to be searched ->;
cin>>num;
for(i=0;i<n;i++)
{
if(ar[i]==num)
{
flag=1;
break;
}
else
{
flag=0;
}
}
if(flag==1)
{
cout<<Element is at position:i+1;
}
else
{
cout<<Element not found;
}
getch();
}
void array::merging(int an)
{
bsort();
int bn,C[20],B[10];
cout<<Enter number of elements in second array(max 10)
->;
cin>>bn;
cout<<\nEnter elements ->;
int i,b,c,a;
for(i=0;i<bn;i++)
{
cin>>B[i];
int x,y,temp;
for(x=0;x<n-1;x++)
{
for(y=0;y<((n-1)-x);y++)
{
if(B[y]>B[y+1])
{
temp=B[y];

19

B[y]=B[y+1];
B[y+1]=temp;
}
}
}
}
for(a=0,b=0,c=0;a<an&&b<bn;)
{
if(ar[a]<=B[b])
{
C[c++]=ar[a++];
}
else
{
C[c++]=B[b++];
}
}
if(a<an)
{
while(a<an)
{
C[c++]=ar[a++];
}
}
else
{
while(b<bn)
{
C[c++]=B[b++];
}
}
for(i=0;i<(an+bn);i++)
{
Cout<<setw(5)<<C[i];
}
getch();
}
Void main()
{
array obj;
int opt,num,pos,no;
Start:
clrscr();
int x=5;
gotoxy(20,x);
cout<<OPERATION On 1-D ARRAY\n;
x=x+2;
gotoxy(20,x);
cout<<1.To Create an array;

20

x++;
gotoxy(20,x);
cout<<2.To display the array;
x++;
gotoxy(20,x);
cout<<3.To insert an element;
x++;
gotoxy(20,x);
cout<<4.To delete an element;
x++;
gotoxy(20,x);
cout<<5.To sort the list;
x++;
gotoxy(20,x);
cout<<6.To search for an element in the list (Binary
Search);
x++;
gotoxy(20,x);
cout<<7.To search for an element in tha list (Linear Search);
x++;
gotoxy(20,x);
cout<<8.To merge two arrays;
x++;
gotoxy(20,x);
cout<<\nEnter your option ->;
cin>>opt;
switch(opt)
{
Case 1: { cout<<\nEnter number of values to be
entered(max 10) ->\n;
cin>>no;
obj.creation(no);
}break;
Case 2: { obj.traversal();
getch();
}break();
Case 3: { cout<<Enter number and position ->;
cin>>num>>pos;
obj.insertion(num,pos);
}break;
Case 4: { cout<<Enter the position at which the
element which has to be deleted;
cin>>pos;
obj.deletion(pos);
}break;
Case 5: { obj.bsort();
}break;
Case 6: { obj.bsearch();
}break;

21

Case 7: { obj.lsearch();
}break;
Case 8: { obj.merging(no);
}break;
}
if(opt<8)
{
goto start;
}
getch();
}

Program based on 2-D array


->
#include<iostream.h>
#include<conio.h>
#include<process.h>
int main()
{
int a[30][30],b[30][30],c[30][30];
int i,j,m,n,p,q,sum=0,sumA=0,sumb=0;
cout<<Input size of row of matrix-A\n;
cin>>m;
cout<<Input size of column of matrix-A\n
cin>>n;
cout<<Input size of row of matrix-B\n;
cin>>p;
cout<<Input size of column of matrix-B\n
cin>>q;
if(m==p)&&(n==q)
{
cout<<Matrices can be added>>endl;
}
else
{
cout<<matrices cannot be added as Matrix A is
<<m<<*<<n<< But Matrix B is
<<p<<*<<q;
getch();
exit(0);
}
cout<<\nEnter matrix-A\n<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
22

cin>>a[i][j];
}
}
cout<<\nEnter matrix-B\n<<endl;
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
cin>>b[i][j];
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
cout<<The sum of the matrices is:\n;
for(i=0;i<m;i++)
{
cout<<\n;
for(j=0;j<n;j++)
{
cout<<\t<<c[i][j];
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
cout<<The difference of the matrices is:\n;
for(i=0;i<m;i++)
{
cout<<\n;
for(j=0;j<n;j++)
{
cout<<\t<<c[i][j];
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j||i+j==m-1)
{

23

sumA+=a[i][j];
}
}
}
cout<<\nThe sum of the diagonals of Matrix-A is:\tsumA;
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
if(i==j||i+j==p-1)
{
sumB+=b[i][j];
}
}
}
cout<<\nThe sum of the diagonals of Matrix-A is:\tsumB;
cout<<\nThe sum of the diagonals of Matrix-A and B
:\t<<sumA+sumB;
int ip;
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(ip=0;ip<n;ip++)
{
c[i][j]+=(a[i][ip]*b[ip][j]);
}
}
}
cout<<\nProduct of the two matrices is:<<endl;
for(i=0;i<m;i++)
{
cout<<\n;
for(j=0;j<q;j++)
{
cout<<c[i][j]<< ;
}
}
cout<<Transpose of Matrix-A is<<endl;
for(i=0;i<m;i++)
{
c[i][j]=0;
for(j=0;j<q;j++)
{
c[i][j]=a[j][i];
cout<<c[i][j]<< ;
}

24

}
cout<<Transpose of Matrix-B is<<endl;
for(i=0;i<m;i++)
{
c[i][j]=0;
for(j=0;j<q;j++)
{
c[i][j]=b[j][i];
cout<<c[i][j]<< ;
}
}
getch();
}

Program based on
implementing LIFO operations
using ARRAY ->
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
Class stack
{
Int A[6];
int top;
public:
stack()
{
top = -1;
}
void push();
void pop();
int isempty();
void display();
};
void stack::push()
{
if(top==6)
{
cout<<O/F;
}
else
{
top++;
cout<< enter number:;
25

cin>>A[top];
}
}
void stack::pop()
{
cout<<Deleted is:<<A[top];
top--;
}
int stack::isempty()
{
if(top==-1)
{
return 1;
}
else
{
return 0;
}
}
void main()
{
stack obj;
int opt;
start:
clrscr();
cout<<1.PUSH\n;
cout<<2.POP\n;
cout<<3.DISPLAY\n;
cout<<4.EXIT\n;
cin>>opt;
switch(opt)
{
Case1: obj.push();
Break;
Case2: if(obj.isempty()==1)
cout<<U/F;
else
obj.pop();
getch();
break;
case 3: obj.display();
getch();
break;
}
if(opt<4)
goto start;
getch();
}

26

Program based on
implementing LIFO operations
using LINKED LIST ->
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
Struct node
{
int data;
node*ptr;
};
void push(node*&top)
{
node*t;
t=new node;
cout<<enter number:;
cin>>t ->data;
top=t;
}
void pop(node*&top)
{
cout<<deleted element is:<<top->data;
node*t;
t=top;
top=top->ptr;
delete t;
}
int isempty(node*top)
27

{
if(top==NULL)
return 1;
else
return 0;
}
void display(node*top)
{
node*t;
t=top;
while(t!=NULL)
{
cout<<t->data<<\n;
t=t->ptr;
}
}
void main()
{
node*top;
top=NULL;
int opt;
start:
clrscr();
cout<<1.PUSH\n;
cout<<2.POP\n;
cout<<3.DISPLAY\n;
cout<<4.EXIT\n;
cin>>opt;
switch(opt);
{
case1:push(top);
break;
case2: if(isempty(top)==1)
cout<<U/F;
else
pop(top);
getch();
break;
}
if(opt<4)
goto start;
getch();
}

28

Program based on
implementing FIFO
operations using ARRAY:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void insertion(intq[],int&f,int&r,int num)
{
if(f==0&&r==7)
{ cout<<overflow\n;
getch();}
else
{
if(f==-1)
f=r=0;
else if(r!=7)
r++;
else
for(int i=f;i<=r;i++)
q[i-1]=q[i];
q[r]=num;
}
}
void deletion(int q[],int&f,int&r)
{
cout<<the deleted element is:<<q[f];
if(f==r)
f=r=-1;
else
f++;
}

29

Int isempty(intf)
{
if(f==-1)
Return1;
else
return 0;
};
void display(int q[],int f,int r)
{
for(int i=f;i<=r;i++)
cout<<
setw(5)<<q[i];
}
void main ()
{
intq[8],opt,num,f,r;
f=r=-1;
start;
clrscr();
cout<<setw(10)<<QUEUE OPERATIONS\n\n;
cout<<1.Insertion\n;
cout<<2.Deletion\n;
cout<<3.Display\n;
cout<<4.Exit\n;
cout<<Enter your option\n;
cin>>opt;
switch(opt)
{
case1:cout<<enter the number to be inserted;
Cin>>num;
Insertion(q,f,r,num);
Break;
Case2:if(isempty(f)==1)
Cout<<underflow;
Else
deletion(q,f,r);
getch();
break;
case3:display(q,f,r);
getch();
break;
}
if(opt<4)
goto start;
}

30

Program based on
implementing FIFO
operations using LINKED LIST
->
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct node
{
int num;
node*ptr;
};
void insertion(node*&f,node*&r, int num)
{
node*t;
t=new node;
t->num=num;
t->ptr=NULL;
if(f==NULL)
f=r=t;
else
{
r->ptr=t;
r=t;
}
}
void deletion(node*&f,node*&r)
{
node*t;

31

Cout<<the deleted element is:<<f->num;


t=f;
f=f->ptr;
delete t;
if(f==NULL)
f=r;
}
int isempty(node*f)
{
if(f==NULL)
Return 1;
else
Return0;
}
void display(node*f)
{
node*t;
t=f;
while(t!= NULL)
{
cout<<t->num<< ;
t=t->ptr;
}
}
void main()
{
node*f;
node*r;
f=r=NULL;
int num,opt;
Start:
clrscr();
cout<<QUEUE OPERATIONS\N;
cout<<1.Insertion\n;
cout<<2.Deletion\n;
cout<<3.Display\n;
cout<<4.Exit\n;
cout<<Enter your option\n;
cin>>opt;
switch(opt)
{
case1:cout<<enter the number to be inserted;
Cin>>num;
Insertion(f,r,num);
Break;
Case2:if(isempty(f)==1)
Cout<<underflow;
Else
deletion(f,r);
getch();
break;
case3:display(f);
getch();
}
if(opt<4)
goto start;
}

32

Program based on
implementing FIFO
operations using ARRAY as
CIRCULAR QUEUE ->
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>
void insertion (int q[],int&f,int&r)
{
if((f==0&&r==7)||(r+1==f))
Cout<<overflow\n;
else
{
int num;
cout<<enter element to be inserted\n;
cin>>num;
if(f==-1)
f=r=0;
else if(r==7)
r=0;
else
r=r+1;
q[r]=num;
}
}
void deletion(int q[],int&f,int&r)
{
cout<<the deleted element is:<<q[f];
33

if(f==r)
f=r=-1;
else
f=f+1;
}
int isempty(int f)
{
if(f==-1)
return 1;
else
return 0;
}
void display(int q[],int f,int r)
{
int i;
if(f<r)
for(i=f,i<r;i++)
cout<<setw(5)<<q[i];
else
for(i=f;i<=7;i++)
cout<<setw(5)<<q[i];
for(i=0;i<r;i++)
cout<<setw(5)<<q[i];
}
void main()
{
int f,r;
f=r=-1;
int q[8];
int opt;
start:
clrscr();
cout<<CIRCULAR QUEUE OPERATIONS\n;
cout<<1.Insertion\n;
cout<<2.Deletion\n;
cout<<3.Display\n;
cout<<4.Exit\n;
cout<<Enter your option\n;
cin>>opt;
switch(opt)
{
case1: insertion(q,r,f);
Break;
Case2: if(isempty(f)==1)
cout<<underflow\n;
else
deletion(q,f,r);
getch();
Break;
Case3:display(q,f,r);
getch();
}
if(opt<4)
goto start;
}

34

Program based on Reading Of


TEXT data file character by
character ->
#include<iostream.h>
#include<fstream.h>
#include<ctype.h>
void ucase_alphabets()
{
ifstream afile;
afile.open(PARA.txt);
char ch;
int c=0;
while(afile)
{
afile.get(ch);
if(isdigit(ch))
{
c++;
}
}
cout<<The number of digits are:<<c;
}
void main()
{
35

clrscr();
Ucase_alphabets();
getch();
}

Program based on reading of


TEXT data file word by word
->
#include<fstream.h>
#include<conio.h>
#include<ctype.h>
#include<iostream.h>
void count_word()
{
fstream tfile(STORY.txt,ios::in);
char str;
int c=0;
clrscr();
while(tfile)
{
tfile.get(str);
if(str== )
{
tfile.get(str)
if(tolower(str)==t)
{
tfile.get(str);
if(islower(str)=h)
{
tfile.get(str);
if(tolower(str)==e)
36

{
tfile.get(str);
}
}
if(str== )
{
c++;
}
}
}
}
tfile.close();
cout<<Number of times the appear is:<<c;
}
void main()
{
clrscr();
count_word();
getch();
}

Program based on reading of


TEXT data file sentence by
sentence ->
#include<iostream.h>
#include<fstream.h>
#include<ctype.h>
#include<conio.h>
void countline()
{
char Aline[80];
int count=0;
ifstream file(LINES.txt);
while(file.getline(Aline,80,\n)
{
if(Aline==A)
{
count++;
}
}
file.close();
cout<<No. of lines starting with A:<<count<<endl;
}
void main()
{
37

clrscr();
countline();
getch();
}

Program based on reading of


binary data file for particular
record->
#include<iostream.h>
#include<fstream.h>
#include<ctype.h>
#include <conio.h>
#include<string.h>
#include<stdio.h>
Class FLIGHT
{
int Fno;
char From[20];
char To[20];
public:
char* GetFrom() {return From;}
char* GetTo() {return To;}
void Enter() {cin>>Fno;gets(From);gets(To);}
void Display() {cout<<Fno<<:<<From<<:<<To<<endl;}
};
void Flight_Enter()
{
38

FLIGHT F;
fstream fin;
fin.open(FLIGHT.DAT, ios::app|ios::out|ios::binary);
int n, I;
clrscr();
cout<< Enter how many flight data you want to enter : ;
cin>> n;
for(i=0; i<n; i++)
{
cout<< Enter data for flight : <<i+1;
F. Enter();
fin.write((char*))&F,sizeof(FLIGHT));
}
fin.close();
}
Void Read()
{
FLIGHT F;
ifstream fin;
fin.open( FLIGHT.DAT, ios::binary);
while(fin.read((char*)&F,sizeof(FLIGHT)))
{
if(strcmp(F.GetTo(), Mumbai)==0)
F.Display();
}
Fin. Close();
}
void main()
{
clrscr();
Flight_Enter();
Read();
getch();
}

39

Program based on appending


of binary data file for n
records->
#include<fstream.h>
#include<conio.h>
#include<string.h>
#invclude<stdio.h>
#include<stdlib.h>
Struct telrec
{
char name[20];
int tno;
} tele;
void append()
{
Ofstream file;
tfile.open(tele.dat , ios::app);
int n,I;
cout<< Enter how many customers;

40

cin<<n;
for(i=0; i<n; i++)
{
cout<<\n Enter the telephone number;
cin>>tele.tno;
cout<<\n Enter the name;
cin>>tele.name;
tfile.write((char*)&tele,sizeof(tele));
}
}
void display()
{
Ifstream tfile;
tfile.open(tele.dat,ios :: binary);
int no, flag;
flag=0;
cout<<\n Enter the telephone number to be searched;
cin>>no;
while(tfile)
{
tfile.read((char*)&tele,sizeof(tele));
if(!tfile)
break;
if(tele.tno==no)
{
cout<<\n Name<<tele.name;
cout<<\n Telephone No<<tele.tno;
flag=1;
}
}
if(flag==0)
{
Cout<<\n Record does not exist;
}
}
void main()
{
clrscr();
append();
display();
getch();
}

41

Program based on Deletion in


Binary data file ->
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
Class stu
{
int rollno;
char name[25];
char class[4];
float marks;
char grade;
public:
voidgetdata()
{
cout<<rollno;
cin>>rollno;
cout<<name;

42

cin>>name;
cout<<class;
cin>>class;
cout<<marks;
cin>>marks;
if(marks>=75)
grade=A;
else if(marks>=60)
grade=B;
else if(marks>=50)
grade= C;
else if(marks>=40)
grade=D;
else grade=F;
}
voidputdata()
{
cout<<rollno<<rollno<<\tname:<<name<<\nmarks:<<marks<<\
tgrade<<grade<<endl;
}
int getrno()
{
return rollno;
}
}s1,stud;
int main()
{
ifstream fio(stu.dat,ios::in);
ofstream file(temp.dat,ios::out);
int rno;
char found=f,confirm=n;
cout<<Enter rollno of student whose record is to be deleted\n;
cin>>rno;
while(!fio.eof())
{
fio.read((char*)&s1,size of(s1));
if(s1.getrno()==rno)
{
s1.putdata();
found=t;
cout<<are you sure,you want to delete this
record(y/n)..;
cin>>confirm;
if(confirm==n)
file.write((char*)&s1,size of(s1));
}
else
{
file.write((char*)&s1,size of(s1));
}
if(found==F)
cout<<record not found!!\n;

43

fio.close();
file.close();
remove(stu.dat);
rename(temp.dat,stu.dat);
fio.open(stu.dat,ios::in);
cout<<now the file contains\n;
while(!fio.eof())
{
fio.read((char*)&stud,size of(stud));
if(fio.eof())
Break;
stud.putdata();
}
fio.close();
return 0;
}
}

Program based on SQL ->


Table: 01
CREATE TABLE Personal
(
empno
integer NOT NULL,
name
char(10),
dobirth
date,
native-place
char(20),
hobby
char(20)
);
INSERT INTO Personal
VALUES(123,Amit,23-jan-1965,Delhi);
INSERT INTO Personal
VALUES(127,Manoj,12-dec-1976,Mumbai);
INSERT INTO Personal
VALUES(124,Abhai,11-aug-1975,Allahabad);
44

INSERT INTO Personal


VALUES(125,Vinod,04-apr-1974,Mumbai);
INSERT INTO Personal
VALUES(129,Ramesh,28-oct-1981,Pune);
Empno
123
127
124
125
129

Name
Amit
Manoj
Abhai
Vinod
Ramesh

Dobirth
23-jan-1965
12-dec-1976
11-aug-1975
04-apr-1974
28-oct-1981

Native-place
Delhi
Mumbai
Allahabad
Mumbai
Pune

ALTER TABLE Personal


ADD (Hobby char(15));
INSERT INTO Personal(Hobby)
VALUES(Music,Writing,Music,Sports,Sports);
SELECT*FROM Personal;
Empno
Name
123
Amit
127
Manoj
124
Abhai
125
Vinod
129
Ramesh
SELECT * FROM Personal
WHERE Hobby = MUSIC;
Empno
123
124

Name
Amit
Abhai

Dobirth
23-jan-1965
12-dec-1976
11-aug-1975
04-apr-1974
28-oct-1981

Dobirth
23-jan-1965
11-aug-1975

Native-place
Delhi
Mumbai
Allahabad
Mumbai
Pune

Hobby
Music
Writing
Music
Sports
Sports

Native-place
Delhi
Allahabad

Hobby
Music
Music

45

Table: 02
Table: Student
No.
1.
2.
3.
4.
5.
6.
7.

Name
Pankaj
Shalini
Sanjay
Sudha
Rakesh
Shikha
Surya

Age
24
21
22
25
22
23
23

Department
Computer
History
Hindi
History
Hindi
Hindi
Computer

Dateofadm
10/01/97
24/03/98
12/12/96
01/07/99
05/09/97
31/07/97
25/02/97

Fee
120
200
300
400
250
200
210

SELECT Department,Fee FROM Student


GROUP BY Department Having Fee>200;
Department
Hindi
History
SELECT * FROM Student
ORDER BY Name ASC;
46

Sex
M
F
M
F
M
F
M

Pankaj
Rakesh
Sanjay
Shalini
Shikha
Sudha
Surya

24
22
22
21
23
25
34

Computer
Hindi
Hindi
History
Hindi
History
Computer

10/01/97
05/09/97
12/12/96
24/03/98
31/07/97
01/07/99
25/02/97

120
250
300
200
200
400
210

M
M
M
F
F
F
M

SELECT * FROM Student


WHERE SEX LIKE F;
Shalini
Shikha
Sudha

21
23
25

History
Hindi
History

24/03/98
31/07/97
01/07/99

200
200
400

F
F
F

SELECT COUNT(distinct Department) FROM Student;


3
SELECT MAX(Age) FROM Student WHERE Sex = F;
25
SELECT AVG(Fee) FROM Student WHERE Dateofadm<{01/01/98};
236
SELECT SUM(Fee) FROM Student WHERE Dateofadm <{01/01/98};
1080
SELECT MIN(Age) FROM Student WHERE Sex = M;
22

Table: 03
Table:Furniture
No.
1
2
3
4
5
6
7
8
9
10

Itemname
White lotus
Pink feather
Dolphin
Decent
Comfort zone
Donald
Royal finish
Royal tiger
Econo sitting
Eating
paradise

Type
Double bed
Baby cot
Baby Cot
Office table
Double bed
Baby cot
Office table
Sofa
Sofa
Dining table

Dateofstock
23/02/02
20/01/02
19/02/02
01/01/02
12/01/02
24/02/02
20/02/02
22/02/02
13/12/01
19/02/02

Price
30000
7000
9500
25000
25000
18000
18000
31000
9500
11500

Discount
25
20
20
30
25
30
30
30
25
25

47

SELECT Type,AVG(Discount),MAX(Price) FROM Furniture


GROUP BY Type;
Double bed
Baby cot
Office table
Sofa
Dining table

25
35
30
52.5
25

30000
18000
25000
31000
11500

SELECT * FROM Furniture


WHERE Dateofstock BETWEEN 14/02/02 AND 24/02/02;
White lotus
Dolphin
Donald
Royal finish
Royal tiger
Eating paradise

Double bed
Baby Cot
Baby cot
Office table
Sofa
Dining table

23/02/02
19/02/02
24/02/02
20/02/02
22/02/02
19/02/02

DELETE FROM Furniture


WHERE Discount<30;
SELECT Itemname,Type,Dateofstock,Price,Discount;
Itemname
Type
Dateofstock
Decent
Office table
01/01/02
Donald
Baby cot
24/02/02
Royal finish
Office table
20/02/02
Royal tiger
Sofa
22/02/02

30000
9500
18000
18000
31000
11500

25
20
30
30
30
25

Price
25000
18000
18000
31000

Discount
30
30
30
30

DROP TABLE Furniture;

Table: 04
Table: Books
Book_id
C0001
F0001

Book_name
Fast Cook
The tears

T0001
T0002

My first C++
C++
brainworks
Thunderbolts

F0002

Author_name
Lata Kapoor
William
Hopkins
Brian & brooke
A.W. Rossaine

Publishers
EPB
First publ.

Price
355
650

Type
Cookery
Fiction

Qty
5
20

EPB
TDH

350
350

Text
Text

10
15

Anna Roberts

First publ.

750

Fiction

50

Publishers
First publ.

Price
650

Type
Fiction

Qty
30

First publ.

750

Fiction

60

UPDATE Books
SET Qty=Qty+10
WHERE Price>350;
Book_id
F0001

Book_name
The tears

F0002

Thunderbolts

Author_name
William
Hopkins
Anna Roberts

48

SELECT * FROM Books


WHERE Grade In(TEXT,COOKERY);
Book_id

Book_name

Author_name

Publishe
rs
EPB
EPB

C0001
T0001

Fast Cook
My first C++

T0002

C++
brainworks

Lata Kapoor
Brian &
brooke
A.W. Rossaine TDH

Price

Type

Qty

355
350

Cookery
Text

5
10

350

Text

15

CREATE VIEW NBooks


AS SELECT Book_name,Author_name,Type FROM Books;
DROP VIEW NBooks;

49

50

S-ar putea să vă placă și