Sunteți pe pagina 1din 83

G. D.

GOENKA PUBLIC SCHOOL

COMPUTER SCIENCE
C++ WORKFILE

SUBMITTED TO SUBMITTED BY
MS. HARSHITA AGARWAL HARSH CHAK

XII P ROLL NO. 09

1
ACKOWLEDGEMENT

I would like to express my special thanks of


gratitude to my teacher Harshita Agarwal as well as
our principal Raveen Pande who gave me the
golden opportunity to do this wonderful project on
the topic C++ programs, which also helped me in
understanding this subject.

2
INDEX
S. NO. CONTENT PAGE

1 PROGRAM 1 5

2 PROGRAM 2 8

3 PROGRAM 3 12

4 PROGRAM 4 15

5 PROGRAM 5 18

6 PROGRAM 6 21

7 PROGRAM 7 24

8 PROGRAM 8 27

9 PROGRAM 9 34

10 PROGRAM 10 36

11 PROGRAM 11 40

12 PROGRAM 12 45

13 PROGRAM 13 50

14 PROGRAM 14 55

15 PROGRAM 15 61

16 PROGRAM 16 63

17 PROGRAM 17 65

18 PROGRAM 18 69

19 PROGRAM 19 72

20 PROGRAM 20 74

21 TABLE 1 77

22 TABLE 2 79

23 TABLE 3 81

3
C++
PROGRAMS

4
PROGRAM 1
#include<iostream.h>

#include<conio.h>

/* Program to illustrate working of constructors and destructors */

class A1

public:

A1() /* Constructor 1 */

cout<<"\n Calling the first constructor";

~A1() /* Destructor 2 */

cout<<"\n Calling the first destructor";

};

class A2

public:

A2() /* Constructor 2 */

cout<<"\n Calling the second constructor";

~A2() /* Destructor 2 */

cout<<"\n Calling the second destructor";

5
}

};

void crobj(void) /* A function for creating objects */

A2 Ob2;

A1 ob2;

void main()

clrscr();

/* Constructor 1 called for first time */

A1 ob1;

/* Calling a function to create objects */

crobj();

/* Constructor 1 gets called for the second time and constructor 2 gets

called for the first time */

/* The destructor 2 is called first and destructor 1 after that once


the

the fuction is done */

/* Constructor 2 called for the second time */

A2 Ob1;

getch();

/* Destructor 2 is called and then destructor 1 is called after exiting

6
main() to destroy remaining objects*/

OUTPUT:

7
PROGRAM 2
#include<iostream.h>

#include<conio.h>

/* This program shows the working of objects and classes */

class A /* Class 1 */

protected:

/* These can be accessed by the member function of this class and the

friends of these class. Also, these can be inherited */

int c;

void fun1()/* fuction to display values */

fun2();

cout<<"\n Values of a, b, c: "<<a<<" "<<b<<" "<<c;

private:

/* These can be accessed by member functions and friends of this class


*/

int b;

void fun2()/* Fuction to update value of b */

b=a*c;

public:

8
/* These can be accessed from any part of the program */

int a;

void fun3() /* Value updation */

cout<<"\n Enter a number: ";

cin>>a;

c=a*10;

void disp() /* Function to access protected function */

fun1();

};

class B : public A /* this class inherits class A publicaly */

public:

void dis()

fun1(); /* Diplay values using inherited member function */

void fun4()

9
c=a+10; /* Values updation */

};

void main()

clrscr();

/* Objects */

A ob1;

B ob2;

/* Call of functions */

ob1.fun3();

cout<<"\n Enter another value: ";

cin>>ob2.a; /* Updating a public member */

/* Call of functions */

ob2.fun4();

/* Values of same memeber for different objects diplayed now */

cout<<"\n Values of memebers of ob1: ";

ob1.disp();

cout<<"\n Values of mamebers of ob2: ";

ob2.dis();

getch();

10
OUTPUT

11
PROGRAM 3
#include<iostream.h>

#include<conio.h>

/* A program to illustrate simple inheritance */

class area /* Base Class */

float x, y;

protected:

float ar;

void calc_ar() /* Function to calculate area of rectangle */

ar=x*y;

public:

void init(float a, float b) /* Fuction to initialize values of

members */

x=a;

y=b;

};

class cost: public area /* Derived Class */

12
{

float cs, t_cs;

void tot_cos() /* Function to calculate total cost to paint */

t_cs=cs*ar;

public:

void disp() /* Function to display values */

calc_ar();

cout<<"\nTotal Area: "<<ar;

cs=10; /* Cost per square unit */

tot_cos();

cout<<"\nTotal Cost to paint the rectangular wall:


"<<t_cs<<"Rs.";

};

void main()

clrscr();

/* Input values */

float a, b;

cout<<"\n Enter lenght and breadth for rectangle: ";

cin>>a>>b;

13
cost ob1;

ob1.init(a,b); /* Calling function from Base class using object of

Derived class */

ob1.disp();

getch();

OUTPUT

14
PROGRAM 4
#include<iostream.h>

#include<math.h>

#include<conio.h>

/* A program to print the value of a simple series */

class ser

double x, res, ft;

long int i, j, n;

void fact(int p)/* Function to calculate factorial of a number */

ft=1;

for(j=1; j<=p; j++)

ft*=j;

void calc() /* Function to calculate value of series */

res=0;

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

fact(i);

res+=(pow(x, i))/ft; /* Calculating and storing the


values */

/* Displaying the value of series */

cout<<”\n The sum of the series when x is “<<x

<<” upto “<<n<<” terms is: “<<res;

15
}

public:

ser()/* constructor to initialize values */

cout<<”\n This program will print the result of the series: “

<<”\n 1 + x + (x^2)/2! + (x^3)/3!.... till n terms”

<<”\n Enter value of x: “;

cin>>x;

cout<<”\n Enter number of terms: “;

cin>>n;

calc();

};

void main()

clrscr();

ser ob;

getch();

16
OUTPUT

17
PROGRAM 5
#include<iostream.h>

#include<conio.h>

#include<math.h>

/* Program to check if a number is an 18rmstrong number or not */

class arm

int num, *dig, sum;

int i, d;

void check()/* Function to check */

sum=0;

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

sum+=pow(dig[i], 3);

if(sum==num)

cout<<”\n Yes, this is an 18rmstrong number”;

else

cout<<”\n No, this isn’t an 18rmstrong number”;

public:

arm() /* Constructor to initialize values */

cout<<”\n Enter the number to check if it is an 18rmstrong


number: “;

cin>>num;

18
int dum=num;

d=0;

while(dum>0) /* Calculating number of digits */

dum =dum/10;

d++;

dig= new int[d]; /* Array to store digits */

dum=num;

i=d-1;

while(i>=0) /* Storing digits in the array */

dig[i]=dum%10;

dum=dum/10;

i--;

check();

};

void main()

clrscr();

arm ob;

getch();

19
OUTPUT

20
PROGRAM 6
#include<iostream.h>

#include<conio.h>

/* This is a program to illustrate all types of constuctors */

class c1

public:

c1()/* Default Constructor */

cout<<"\n Default constructor";

};

class c2

int a, b;

public:

c2(int x, int y)/* Parameterized Constructor takes values */

a=x;

b=y;

cout<<"\n Parameterized constructor 1"

<<"\n a: "<<a<<" b: "<<b;

};

21
class c3

int a, b;

public:

c3(int x, int y)

a=x;

b=y;

cout<<"\n Parameterized constructor 2 "

<<"\n a: "<<a<<" b: "<<b;

c3( c3 &ob)/* Copy Constructor is invoked when we initialize an

using another object */

a=ob.a+1;

b=ob.b-1;

cout<<"\n Copy Constructor"

<<"\n a: "<<a<<" b: "<<b;

};

void main()

clrscr();

22
c1 ob1;/* Default constructor called */

c2 ob2(10, 12);/* Object has to be defined this way when parameterized

constructor is defined */

c3 ob3(6, 17);/* Parameterized constructor called */

/* Initializations where Copy constructor is called */

c3 ob4(ob3);

c3 ob5=ob4;

/* Copy constructor not called in this case */

c3 ob6(1, 2);

ob6=ob4;

getch();

OUTPUT:

23
PROGRAM 7
#include<iostream.h>

#include<conio.h>

/* A program to illustrate function overloading */

void amt(float princ, int time, float rate) /* Function 1 */

cout<<”\nPrincipal Amount: “<<princ

<<”\tTime: “<<time<<” Years”

<<”\tRate: “<<rate

<<”\nInterest Amount: “<<princ*time*rate<<endl;

void amt(float princ, int time) /* Function 2 */

cout<<”\nPrincipal Amount: “<<princ

<<”\tTime: “<<time<<” Years”

<<”\tRate: 0.08”<<”\nInterest Amount: “<<princ*time*0.08<<endl;

void amt(float princ, float rate) /* Function 3 */

cout<<”\nPrincipal Amount: “<<princ

<<”\tTime: 2 Years”<<”\tRate: “<<rate

<<”\nInterest Amount: “<<princ*2*rate<<endl;

void amt(int time, float rate) /* Function 4 */

cout<<”\nPrincipal Amount: 2000”<<”\tTime: “<<time<<” Years”

24
<<”\tRate: “<<rate

<<”\nInterest Amount: “<<2000*time*rate<<endl;

void amt(float princ) /* Function 5 */

cout<<”\nPrincipal Amount: “<<princ

<<”\tTime: 2 Years”<<”\tRate: 0.08”

<<”\nInterest Amount: “<<princ*2*0.08<<endl;

void main()

clrscr();

/* Calling function 5*/

cout<<”\nCase 1”;

amt(2000);

/* Calling function 2*/

cout<<”\nCase 2”;

amt(2500.0, 3);

/* Calling function 1*/

cout<<”\nCase 3”;

amt(2300, 3, 0.11);

/* Calling function 3*/

cout<<”\nCase 4”;

amt(2, 0.12);

25
/* Calling function 4*/

cout<<”\nCase 5”;

amt(6, 0.07);

getch();

OUTPUT

26
PROGRAM 8
#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#include<dos.h>

/* A menu driven program to add, subtract and multiply two matrices */

class menu

int arr1[3][3], arr2[3][3], res[3][3];

int i, j;

void add() /* Funtion to add matrices */

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

for(j=0; j<3; j++)

res[i][j]=arr1[i][j]+arr2[i][j]; /* Addition of
matrix elements */

cout<<"\n The sum of the two matrices is : "<<endl;

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

for(j=0; j<3; j++)

cout<<" "<<res[i][j]; /* Displaying resulting


matrix */

cout<<endl;

27
}

void sub() /* Funtion to subtract matrices */

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

for(j=0; j<3; j++)

res[i][j]=arr1[i][j]-arr2[i][j]; /* Subtraction of
array element */

cout<<"\n The matrix after subtraction : "<<endl;

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

for(j=0; j<3; j++)

cout<<" "<<res[i][j]; /* Displaying resulting matrix


*/

cout<<endl;

void mul() /* Funtion to multiply two matrix */

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

for(j=0; j<3; j++)

res[i][j]=0;

28
for(int k=0; k<3; k++)

res[i][j]+=arr1[i][k]+arr2[k][j]; /* Matrix
Multiplication */

cout<<"\n The product of the two matrices is : "<<endl;

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

for(j=0; j<3; j++)

cout<<" "<<res[i][j]; /* Displaying resulting matrix


*/

cout<<endl;

void disp()

clrscr();

cout<<"Array 1: "<<endl;

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

for(j=0; j<3; j++)

cout<<" "<<arr1[i][j]; /* Displaying matrix 1 */

cout<<endl;

29
cout<<"\nArray 2: "<<endl;

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

for(j=0; j<3; j++)

cout<<" "<<arr2[i][j]; /* Displaying matrix 2 */

cout<<endl;

public:

menu()

cout<<"\n Enter numbers for two 3X3 matices: "<<endl;

cout<<"Array 1: "<<endl;

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

for(j=0; j<3; j++)

cin>>arr1[i][j];

cout<<"\n Array 2: "<<endl;

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

for(j=0; j<3; j++)

cin>>arr2[i][j];

30
clrscr();

cout<<"\n Operations:"

<<"\n 1-Addition"

<<"\n 2-Subtraction"

<<"\n 3-Multiplication"<<endl;

int ch;

cin>>ch;

switch(ch)

case 1: disp();

add();

break;

case 2: disp();

sub();

break;

case 3: disp();

mul();

break;

default: break;

};

void main()

31
char ch;

again:

clrscr();

menu ob;

cout<<"\n Continue(Y/N)?? ";

cin>>ch;

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

goto again;

else

cout<<"\n EXITING....";

delay(200);

exit(0);

32
OUTPUT

ADDITION OF MATRIX

SUBTRACTION OF MATRIX

MULTIPLICATION OF MATRIX

33
PROGRAM 9
#include<iostream.h>

#include<conio.h>

/* A program to illustrate function overloading with default parameters */

void amt(float prn, float r, int t=2) /* Function 1 */

cout<<"\nPrinciple Amount : "<<prn

<<"\nTime : "<<t<<" Years"

<<"\nRate : "<<r

<<"\nInterest Amount : "<<(prn*t*r)<<endl;

void amt(float prn, int t=2) /* Fuction 2 */

cout<<"\nPrinciple Amount : "<<prn

<<"\nTime : "<<t<<" Years"

<<"\nRate : 0.05"<<"\nInterest Amount : "<<(prn*t*0.05)<<endl;

void amt(int t, float r=0.06) /* Function 3 */

cout<<"\nPrinciple Amount : 2500"

<<"\nTime : "<<t<<" Years"

<<"\nRate : "<<r

<<"\nInterest Amount : "<<(2500*t*r)<<endl;

void main()

34
clrscr();

cout<<"\nCase 1 :";

amt(2200.0f, 0.13f); /* Function 1 called*/

cout<<"\nCase 2 :";

amt(2400.0f); /* Function 2 called */

/* f tells the compiler to evaluate a number as a float type */

cout<<"\nCase 3: ";

amt(4); /* Function 3 called */

getch();

OUTPUT

35
PROGRAM 10
#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<process.h>

/* Program to illustrate a static stack */

class stat_stack

int top, a[20];

char ch;

void push() /* Function to push an element i.e add an element to

the top of the stack */

if(top==19) /* last element already entered */

cout<<"Overflow";

exit(0);

top++;

cin>>a[top]; /* Entering value in stack */

void pop() /* Function to pop element i.e. delete topmost element of

the stack */

if(top==-1)/* All elements in stack have been deleted */

36
{

cout<<"Underflow";

exit(0);

cout<<"Deleted "<<a[top];

top--;

void display() /* Displaying a stack */

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

cout<<a[i]<<endl;

public:

stat_stack()

top=-1;

do /* Loop to push, pop and display stack elements */

clrscr();

cout<<"\nPush an Element: 1";

cout<<"\nPop an Element: 2";

cout<<"\nDisplay Stack: 3";

cout<<"\nExit: 4 "<<endl;

int k;

37
cin>>k;

switch(k)

case 1: clrscr();

push();

break;

case 2: clrscr();

pop();

break;

case 3: clrscr();

display();

break;

case 4: exit(0);

cout<<"\n Want to Continue(y/n)? ";

cin>>ch;

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

};

void main()

38
clrscr();

stat_stack ob1;

getch();

OUTPUT

PUSHING AN ELEMENT

POPPING AN ELEMENT

DISPLAYING THE STACK

39
PROGRAM 11
#include<iostream.h>

#include<process.h>

#include<conio.h>

/* Program to illustrate a dynamic stack */

struct node

int info;

node *next;

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

node *New(int n) /* Function creates a new node dynamically and returns

the pointer to the new node */

ptr=new node;

ptr -> info=n;

ptr -> next=NULL;

return ptr;

void push(node *np)/* Function pushes a new node into the linked stack */

if(top==NULL)

top=np;

else

save=top;

top=np;

np -> next=save;

40
}

void pop() /* Function pops a node from the linked stack */

if(top==NULL)

cout<<"\n UNDERFLOW!!! ";

getch();

exit(0);

else

ptr=top;

top= top-> next;

delete ptr;

void disp(node *np) /* Function to display the linked stack */

while(np != NULL)

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

np=np->next;

cout<<endl;

void main()

41
{

clrscr();

top=NULL;

int inf;

char ch='y';

while(ch=='y' || ch=='Y') /* Pushing in new nodes */

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

cin>>inf;

newptr=New(inf);

if(newptr==NULL)

cout<<"\n Cannot create new node!!!";

getch();

exit(1);

push(newptr);

cout<<"\n Continue??(Y/N): ";

cin>>ch;

clrscr();

do /* Popping the nodes */

cout<<"\n The current stack is: "<<endl;

disp(top);

42
getch();

cout<<"\n Pop Element??(Y/N): ";

cin>>ch;

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

pop();

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

getch();

43
OUTPUT

44
PROGRAM 12
#include<iostream.h>

#include<process.h>

#include<conio.h>

/* A program to illustrate a Static Queue */

class stat_que

int q[20], fr, rr, chk, it;

void insert() /* Function to insert item in queue */

cin>>it;

if(rr==19) /* Checking if last item has alredy been inserted */

cout<<”\nOVERFLOW!!!”;

getch();

exit(0);

else if(rr==-1) /* If no item has been inserted yet */

fr=rr=0;

q[rr]=it;

else /* Other cases of item insertion */

rr++;

q[rr]=it;

45
}

void disp() /* Fuction to display the queue */

if(fr==-1) /* If no item was inserted in queue */

cout<<”\n EMPTY QUEUE”;

getch();

exit(0);

cout<<”\n The queue from front to rear is: “<<endl;

for(int i=fr; i<rr; i++) /* Displaying Queue */

cout<<q[i]<<” “;

cout<<q[rr]<<endl; /* Displaying last item */

void del() /* Function to delete item in queue */

if(fr==-1) /* No item in Queue */

cout<<”\nUNDERFLOW!!!”;

getch();

exit(0);

else if(fr==rr)

46
fr=rr=-1;

else /* Displaying and deleting the items of the queue */

cout<<”\n Item Deleted is :”<<q[fr++];

public:

stat_que()

fr=rr=-1;

char ch;

do /* Loop to insert, delete and display queue items */

clrscr();

cout<<”\nInsert Item in Queue: 1”;

cout<<”\nDelete Item from Queue: 2”;

cout<<”\nView Queue: 3”;

cout<<”\nExit: 4 “<<endl;

int k;

cin>>k;

switch(k)

case 1: clrscr();

insert();

break;

case 2: clrscr();

47
del();

break;

case 3: clrscr();

disp();

break;

case 4: exit(0);

default: break;

cout<<”\n Want to Continue(y/n)? “;

cin>>ch;

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

};

void main()

clrscr();

stat_que ob1;

getch();

48
OUTPUT

INSERTING ITEM IN QUEUE

DELETING ITEM IN A QUEUE

DISPLAYING QUEUE

49
PROGRAM 13
#include<iostream.h>

#include<conio.h>

#include<process.h>

/* Program to illustrate a dynamic queue */

struct node

int info;

node *next;

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

node *New(int n) /* Function creates a new node dynamically and returns

the pointer to the new node */

ptr=new node;

ptr -> info=n;

ptr -> next=NULL;

return ptr;

void ins(node *np) /* Function to insert item in the Linked Queue */

if(front==NULL)

front=rear=np;

else

rear -> next=np;

rear=np;

50
}

void del() /* Function to delete first item in Linked Queue */

if(front==NULL) /* Empty Queue */

cout<<"\n UNDERFLOW!!!";

getch();

exit(0);

else

ptr=front;

front=front -> next;

delete ptr;

void disp(node *np) /* Function to display the linked stack */

while(np != NULL)

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

np=np->next;

cout<<endl;

void main()

51
clrscr();

front=rear=NULL;

int inf;

char ch='y';

while(ch=='y' || ch=='Y') /* Inserting new node */

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

cin>>inf;

newptr=New(inf);

if(newptr==NULL)

cout<<"\n Cannot create new node!!!";

getch();

exit(1);

ins(newptr);

cout<<"\n Continue??(Y/N): ";

cin>>ch;

clrscr();

do /* Deleting nodes */

cout<<"\n Linked Queue is: "<<endl;

disp(front); /* displaying the queue */

52
cout<<"\n Delete first item??(Y/N): ";

cin>>ch;

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

del();

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

getch();

53
OUTPUT

54
PROGRAM 14
#include<iostream.h>

#include<limits.h>

#include<conio.h>

/* This is a program to illustrate different type of sorting */

class sort

int t, *x, sz, i, j;

void bubsort() /* Function to perform bubble sort */

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

for(int j=0; j<(sz-1); j++)

if(x[j] > x[j+1]) /* Checking for smaller element */

t=x[j];

x[j]=x[j+1]; /* Swapping positions */

x[j+1]=t;

cout<<"\n Sorted Array: "; /* Displaying sorted array */

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

cout<<x[i]<<" ";

55
}

void inssort() /* function to perform insertion sort */

for(i=1; i<=sz; i++)

t=x[i];

j=i-1;

while(t<x[j] && j>=0)

x[j+1]=x[j]; /* Moves element forward */

j--;

x[j+1]=t; /* Insert element in proper place */

cout<<"\n Sorted Array: ";

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

cout<<x[i]<<" ";

void selsort()/* Function to perform selection sort */

int sml, pos;

56
for(i=0; i<sz-1; i++)

sml=x[i];

pos=i;

for(j=i+1; j<sz; j++)

if( x[j]<sml)

sml=x[j];

pos=j;

t=x[i];

x[i]=x[pos];

x[pos]=t;

cout<<"\n Sorted Array: ";

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

cout<<x[i]<<" ";

public:

sort()

cout<<"\n Enter number of elements you want to enter: ";

57
cin>>sz;

x=new int[sz]; /* Creating the array */

cout<<"\n Enter elements(integers): ";

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

cin>>x[i];

cout<<"\n 1-Selection Sort"

<<"\n 2-Bubble Sort"

<<"\n 3-Insertion Sort"

<<"\n Choose sorting option: ";

int ch;

cin>>ch;

re:

switch(ch)

case 1: selsort();

break;

case 2: bubsort();

break;

case 3: inssort();

break;

default: cout<<"\n Wrong choice. Re-enter Choice: ";

58
cin>>ch;

goto re;

};

void main()

clrscr();

sort ob;

getch();

OUTPUT:

59
60
PROGRAM 15
#include<iostream.h>

#include<conio.h>

/* This is a function which illustrates the working of a function which


receives and returns pointers */

class pnt_test

int a, b, *c;

int *swap_big(int *x, int *y) /* Funtion which recieves and returns

pointer */

int t;

t=*x; /* Swapping values */

*x=*y;

*y=t;

if(*x>*y) /* Comparing after swapping values */

return(x);

else

return(y);

public:

pnt_test()

cout<<"\n ENTER 2 NUMBERS: ";

cin>>a>>b;

61
c=swap_big(&a, &b);

cout<<"\n SWAPPED VALUES: "<<a<<" "<<b

<<"\n The larger of the two is: "<<*c;

};

void main()

clrscr();

pnt_test ob1;

getch();

OUTPUT:

62
PROGRAM 16
#include<conio.h>

#include<iostream.h>

#include<stdio.h>

/* A program to illustrate working of pointers with strings */

void main()

clrscr();

char str[40], *pt;

cout<<"Enter Any string [below 40 chars] : ";

gets(str); /* Entering a string */

pt = str; /* pointer store a the string */

while (*pt != NULL)

cout<<*pt; /* Printing array */

pt++; /* Moving forward in array */

getch();

63
OUTPUT:

64
PROGRAM 17
#include<stdio.h>

#include<dos.h>

#include<iostream.h>

#include<conio.h>

/* This is a program to print a pattern */

class patt

int i, j, space, rows;

char elem;

void print()

/* Printing the upper half */

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

textcolor(i); /* To change color of text */

for(j=1; j<space; j++)/* Printing spaces */

delay(5);

cout<<" ";

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

delay(5);

cprintf("%c",elem); /* printing colored element */

cout<<" ";

65
}

cout<<endl;

space--;

/* Printing lower half */

for(i=rows-2; i>=0; i--)

textcolor(i); /* Changing color of text */

for(j=1; j<space+1; j++)

delay(5); /* Printing spaces */

cout<<" ";

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

delay(5);

cout<<" ";

cprintf("%c", elem); /* Printing colored element */

cout<<endl;

space++;

66
public:

patt()

int dum;

cout<<"\n This program will print a double pyramid."

<<"Please enter number of rows: ";

cin>>rows;

space=rows;

cout<<"\n Type the ASCII code of any character: ";

cin>>dum;

elem=dum;

clrscr();

print();

};

void main()

clrscr();

patt ob;

getch();

67
OUTPUT:

68
PROGRAM 18
#include<iostream.h>

#include<conio.h>

#include<dos.h>

/* A program to print the pasccal's triangle */

class psc

int x, y, i, j, rows, spaces;

double fact(int n) /* Function to find factorial of a number */

double res=1;

long ct=1;

do

res*=ct;

ct++;

}while(ct<=n);

return(res);

void draw()/* Function to print the triangle */

spaces=rows;

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

for(j=0; j<=spaces; j++)

69
{

delay(10);

cout<<" ";

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

cout<<(fact(i))/(fact(j)*fact(i-j))<<" "; /* Finding


value

of num in series*/

delay(10);

spaces--;

cout<<endl;

public:

psc()

cout<<"\n This program prints a pascal's triangle. Enter number


of rows: ";

cin>>rows;

draw();

};

void main()

clrscr();

70
psc h;

getch();

OUTPUT:

71
PROGRAM 19
#include<iostream.h>

#include<conio.h>

/* program to illustrate calling of memeber functions from inside

of other memeber functions */

class tri_ar

int base, height;

double area;

void input() /* Function to input data */

cout<<"\n Enter base ";

cin>>base;

cout<<"\n Enter height ";

cin>>height;

void calc() /* Function to calculate area */

input();

area=0.5*base*height;

public:

void output() /* Function to display area */

72
calc();

cout<<"\n Area of triangle: "<<area;

};

void main()

clrscr();

tri_ar ob1;

ob1.output();

getch();

OUTPUT:

73
PROGRAM 20
#include<fstream.h>

#include<iostream.h>

#include<conio.h>

/* Program to count number of vowels and spaces in a line from a file */

void main ()

clrscr();

ofstream mf("xyz.txt"); /* Extracting a file */

if(mf)

mf<<" This is a test line "; /* Writing in the file */

mf.close(); /* Closing the file */

else

cout<<"\n unable to open file ";

mf.close();

fstream fin;

fin.open("xyz.txt",ios::in); /* Opening a file to read it */

char ch;

int c1=0,c2=0;

while(fin)

fin.get(ch);

74
cout<<ch;

if(ch=='a'|| ch=='A'|| ch=='e'|| ch=='E'|| ch=='i'|| ch=='I'||


ch=='o'|| ch=='O'|| ch=='u'|| ch=='U')

c1++; /* Counting vowels */

if(ch==' ')

c2++; /* Counting spaces */

fin.close(); /* Closing the file */

/* Displaying number of vowels and spaces in the string */

cout<<"\n Number of vowels:"<<c1;

cout<<"\n Number of blank spaces:"<<c2;

getch();

OUTPUT:

75
SQL
QUERIES

76
TABLE 1

TABLE: SENDER

SEN_ID SEN_NAME SEN_ADD SEN_CITY

MN36 Errol Gardner 23- Hudson Bay Mangalore


KB21 Ron Disilva 21st house, 2nd Bangalore
Street Avenue
TW18 Akshay Verma 35/21, Drunken Yard Hydrabad
QT62 Ivy Garcia 7th Piviot Drive Lucknow

TABLE: RECIPIENT

REC_ID SEN_ID REC_NAME REC_ADD REC_CITY

TY43 MN36 Natanael 32/III, VIHAR Delhi


Young COLONY
QS54 KB21 Cherrie 2-D, Central Mangalore
Gardner Avenue
HR32 TW18 Avanie 25th , Blossom Calcutta
Srivastava Lane
RI12 QT62 Hazel Grace 51U, Gora Fora Mangalore
Street

77
QUERIES
(i) To display name of all receiver from Mangalore:

QUERY: Select * from Receiver where Rec_City =’Mangalore’;

(ii) To display the sender details in ascending order of SenderName:

QUERY : Select * from Sender order by SenderName;

(iii) To display the Recipient ID, Sender Name, sender Address,


Rec_Name,

Rec_Address for every Reciept:

QUREY: Select REC_ID, SEN_NAME,SEN_ADD, REC_ADD, REC_ADD

from Sender, Recipient Where Sender.SEN_ID=Recipient.SEN_ID;

(IV) To display number of Recipients from each city:

QUERY: Select REC_CITY,Count(*) from Recipient group by REC_CITY;

(V) To Display Names and Address of all senders from Mangalore and
Bangalore:

QUERY: SELECT SEN_NAME,SEN_ADD from sender Where SEN_CITY =


‘Mangalore’ ,’Bangalore’;

78
TABLE 2

TABLE: CONSIGNER

CnorID CnorNAME CnorAddress City

LK03 Rajesh Mishra 23/IV, Vrindavan Lucknow


ND08 Rakesh Kumar 32-D,Central New Delhi
Avenue
ND01 Ramesh Yadav 4CD,South Market New Delhi
CH03 Rita Agarwal 3-U Mohali Mohali

TABLE: CONSIGNEE

CneeID CnorID CneeName CneeAddress CneeCity

MB01 LK03 Sumit Arora 7C, Bandra Mumbai


Market
PT03 ND01 Rahul Singh 23-U, Gandhi Patna
Marg
K04 RJ11 Krishna Yadav 3/IV , Central Kanpur
Park
LK91 ND03 Govind Rai 12/III, Lucknow
Alambagh

79
QUERIES

(i) To display the names of all consignors from Mumbai.

QUERY: Select CnorName from Consignor where city=”Mumbai”;

(ii) To display the cneeID, cnorName, cnorAddress, CneeName, CneeAddress for


every Consignee.

QUERY: Select CneeId, CnorName, CnorAddress, CneeName, CneeAddress from


Consignor,Consignee where Consignor.CnorId=Consignee.CnorId;

(iii) To display the consignee details in ascending order of CneeName.

QUERY: Select * from Consignee Orderby CneeName Asc;

(iv) To display number of consignors from each city.

QUERY: Select city, count(*) from Consignors group by city;

(v) To Display Names Of all cities from Consignee:

QUERY: SELECT DISTINCT City FROM CONSIGNEE;

80
TABLE 3

TABLE: CLASS_A

ROLL_NO1 NAME SUBJECT MARKS

1 Raj Chemistry 78
2 Ayush English 65
3 Adya Chemistry 87
4 Parth Chemistry 19
5 Harsh Physics 54

TABLE: CLASS_B

ROLL_NO2 NAME SUBJECTS MARKS

1 Divya English 76
2 Pranav English 59
3 Akshay Physics 81
4 Sameer Physics 79
5 Surya Chemistry 67

81
QUERIES

(i) To Display Names of All Distinct subjects :


QUERY: Select Distinct Subject from Class_A,Class_B;

(ii) To Display Names And marks of students Studying English:


QUERY: Select Names,Marks from Class_A,Class_B Where Subject =
‘English’;

(iii) To Display the total Marks Of students studying chemistry:


QUERY : select Sum(MARKS) from class_A,Class_B Where SUBJECT =
‘Chemistry’;

(iv) To Display Names of students Having less than 80 marks:


QUERY : Select NAMES , SUBJECT From Class_A,Class_B Where MARKS <
80;

(v) To Display Names of students Having More than 65 marks in ascending


order:
QUERY: Select NAME, MARKS , SUBJECTS From Class_A,Class_B Where
MARKS>=65 Order By Marks Desc;

82
83

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