Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 82

PRACTICLE FILE

2009-2010
ON

C++ PROGRAMS
&
SQL COMMANDS

PROJECT SUBMITTED BY: PROJECT SUBMITTED TO:


ABHISHEK UPADHYAY MRS. GEETA GOGIA MAM
INDEX
• One Dimensional Array
Searching
 Program to search an element in an
array by using Binary Search
method when the array is sorted in
ascending order

Sorting
 Program to sort the elements of an
array in ascending order by using
Selection Sort method
 Program to sort the elements of an
array in ascending order by using
Bubble Sort method
 Program to sort the elements of an
array by using Insertion Sort
 Program to sort the two arrays(arrA
and arrB) while merging(merge
sort) and the resultant array that we
will get after merging should also be
sorted in ascending order if arrA and
arrB are in ascending order

• Two Dimensional Array


Matrix
 Program to add two matrices
 Program to find out transpose of
a matrix

• Array of Structure
 Program to store information of
ten employees by using array
of structure and then display
the information of those
employee(s) that the user want

• Classes and Array of Object


 Program to first store
information about 10 items by
creating the array of objects
and display the information of
only that item whose price is
maximum amongst the price of
all the items
 Program to find out grade of
the marks obtained by the
student by using class
 Program to separate integers
and fractional parts from a
number by using class

• Data File Handling


Text File
 Program to count number
of 'to' in an existing text file
 Program to calculate size of
an existing file

 Program to create another


file from an existing file
having only those words of an
existing text file which starts
from "T/t"

 Program to count ( no . of
Alphabet , no of uppercase
letter, no of lowercase letter,
no of digits, no of spaces, no
of lines.) in a text file
according to user choice.

Binary File
 Program to first create the
binary file and then display
a particular record(when
records are implemented by
using objects)

 Program to create another


binary file from the existing
binary file by writing only
those records which will
satisfy some particular
condition.

• Default Arguments
 Program to calculate the
Simple Interest by making a
function having default
arguments
• Stack and Queue
Stack
 Program to perform PUSH
and POP operation in
dynamically allocated stack

Queue
 Program to perform
INSERT and DELETE
operations in a dynamically
alloacted queue
SQL COMMANDS
• Create Table

• Alter Table

• Insert Into

• Select (to display all field)

• Select (to display particular field)


• Select (to display only those records which fulfill the
particular condition)
• Select (With more than one condition)
• Select (with condition involving Date type data)

• Update
• Order by (With date Type Data)

• IN

• Between

• Count

• Min

• Max
• Like

• Delete

• Drop Table

ONE
DIMENSIONAL
ARRAY
•SEARCHING
•SORTING

PROGRAM NO.1

/*Program to search an element in an array by using Binary Search method when


the array is sorted in ascending order*/

//Preprocessor directive statements


#include<iostream.h>//For input and output
#include<conio.h>//For getch() and clrscr()

//Function prototype
int Bsearch(int[ ],int,int);//To search the element in an array by binary search

//main( ) function
void main( )
{
clrscr( ); //To clear the screen
//Type declaration statements
int arr[50],range,i,sitem,ipos;
//Display the message for taking the range of an array from the user
cout<<"\n Enter the range(max 50):";
cin>>range;
//Now read the elements of an array from the user
cout<<"\n Enter the elements of an array:";
for(i=0;i<range;i++)
cin>>arr[i];
//Display the message for taking the element to be searched from the user
cout<<"\n Enter the element to be searched:";
cin>>sitem;
//Now call the function Bsearch( ) with appropriate arguments
ipos=Bsearch(arr,range,sitem);
if(ipos==-1)
cout<<"\n The element:"<<sitem<<"does not exist in an array:";
else
cout<<"\n The element:"<<sitem<<"exists at position number:"<<ipos;
getch( );
} //End of main( )

//Function definition
int Bsearch(int arr[ ],int range,int sitem)
{
//Type declaration statement(s)
int pos,beg=0,last=range-1,mid;
while(beg<=last)
{
mid=(beg+last)/2;
if(arr[mid]==sitem)
{
pos=mid+1;
break;
}
else
{
if(sitem>arr[mid])
beg=mid+1;
else
last=mid-1;
} //end of else
} //End of while loop
//Now check whether the control has come out from the loop either due to
break statememt or due to false value of testing expression
if(beg>last)
pos=-1;
return pos;
}//End of function definition
Output:-
Enter the range(max 50):5

Enter the elements of an array:


1
2
3
4
5

Enter the element to be searched:3

The element:3 exists at position number:3


PROGRAM NO.2

/*Program to sort the elements of an array in ascending order by using Selection


Sort method*/

//Preprocessor directive statements


#include<iostream.h> //For input and output
#include<conio.h> //For getch( ) and clrscr( )

//Function prototype
void selsort(int[ ],int);//To sort the element of array in ascending order by
using Selection Sort method

//main( ) function
void main( )
{
clrscr( );//To clear the screen
//Type declaration statement(s)
int arr[50],range,i;
//Now accept the range of an array from the user
cout<<"\n Enter the range(max.50):";
cin>>range;
//Now accept the elements of array according to the range from the user
cout<<"\n Enter the elements of an array:";
for(i=0;i<range;i++)
{
cout<<"\n Enter the element no:"<<i+1;
cin>>arr[i];
}
/*Now pass the array and its range to selsort( ) function for sorting
(function calling)*/
selsort(arr,range);
//now display the sorted array
cout<<"\n The sorted array(ascending order) is given below:";
for(i=0;i<range;i++)
cout<<"\n"<<arr[i];
getch ( );
}//End of main( )

//Function definition
void selsort(int arr[ ],int size)
{
int temp,i,j;
for(i=0;i<size-1;i++)
{
for(j=i+1;j<size;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}//End of if
}//End of inner for loop
}//End of outer for loop
}//End of function definition
Output:-
Enter the range(max.50):5

Enter the elements of an array:


Enter the element no:1 3

Enter the element no:2 2

Enter the element no:3 6

Enter the element no:4 -5

Enter the element no:5 7

The sorted array(ascending order) is given below:


-5
2
3
6
7
PROGRAM NO.3 

/*Program to sort the elements of an array in ascending order by using Bubble


Sort method*/

//Preprocessor directive statements


#include<iostream.h> //for input and output
#include<conio.h> //for getch( )and clrscr( )

//Function prototype
void Bubble(int[ ],int); //to sort the elements of an array in ascending order
by bubble sort method

//main( ) function
void main( )
{
clrscr( ); //To clear the screen
//Type declaration statements
int arr[50],i,range;
//Display the message for taking the range of an array from the user
cout<<"\n Enter the range:(max 50):";
cin>>range;
//Now accept the elements of an array according to the range from the user
cout<<"\n Enter the elements of an array:";
for(i=0;i<range;i++)
cin>>arr[i];
/*Now call the function Bubble() with appropriate values for sorting the
array*/
Bubble(arr,range);
//Now display the sorted array
cout<<"\n Sorted array(ascending order) is given below:";
for(i=0;i<range;i++)
cout<<"\n"<<arr[i];
getch( );
} //End of main( )

//Function definition
void Bubble(int arr[ ],int size)
{
int temp,i,j;
for(i=0;i<size-1;i++)
{
for(j=0;j<(size-i)-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
} //End of if
} //End of inner for loop
} //End of outer for loop
} //End of function definition

Output:-
Enter the range:(max 50):5

Enter the elements of an array:


6
5
-1
0
9

Sorted array(ascending order) is given below:


-1
0
5
6
9
PROGRAM NO.4 

/*Program to sort the elements of an array by using Insertion Sort*/

//Preprocessor directive statements


#include<iostream.h> //For input and output
#include<conio.h> //For getch() and clrscr()
include<limits.h> //For INT_MIN

//Function prototype
void Inssort(int[ ],int);//To sort the elements by Insertion Sort method

//main( ) function
void main( )
{
clrscr( ); //To clear the screen
//Type declaration statements
int arr[50],i,range;
//Now read the range of array from the user
cout<<"\n Enter the range(max 50):";
cin>>range;
//Now read the elements of array from the user
for(i=0;i<=range;i++)
cin>>arr[i];
//Now call the function Inssort( ) with appropriate arguments(values to
sort the element of an array in ascending order by using insertion sort
method)
Inssort(arr,range);
//Now display the sorted array
cout<<"\n The sorted array(ascending order) is given below:";
for(i=1;i<=range;i++)
cout<<"\n"<<arr[i];
getch( );
} //End of main()

//Function definition
void Inssort(int arr[ ],int range)
{
//Type declaration statements
int temp,i,j;
//Store the minimum value of integers into arr[0]
arr[0]=INT_MIN;
for(i=1;i<=range;i++)
{
temp=arr[i];
j=i-1;
while(temp<arr[j])
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
} //End of for loop
} //End of function definition
Output:-
Enter the range(max 50):5
5
2
7
8
4

The sorted array(ascending order) is given below:


2
4
5
7
8
PROGRAM NO.5 

/*Program to sort the two arrays(arrA and arrB) while merging(merge sort) and
the resultant array that we will get after merging should also be sorted in
ascending order if arrA and arrB are in ascending order*/

//Preprocessor directive statements


#include<iostream.h> //For input and output
#include<conio.h> //For getch() and clrscr()

//Function prototype
void mergesort(int[ ],int,int[ ],int,int[ ]); //to apply merge sort technique

//main( ) function
void main( )
{
clrscr( ); //To clear the screen
//Type declaration statements
int arrA[50],arrB[50],arrC[100],rangeA,rangeB,rangeC,i;
//Now display the message for taking the range for first array
cout<<"\n Enter the range (max 50) for first array:";
cin>>rangeA;
//Now read the elements of first array
cout<<"\n Enter the elements of first array(elements should be in
ascending order):";
for(i=0;i<rangeA;i++)
cin>>arrA[i];
//Now read the range for second array
cout<<"\n Enter the range for second array:";
cin>>rangeB;
//Now read the elements for second array
cout<<"\n Enter the elements of second array(elements should be in
ascending order)";
for(i=0;i<rangeB;i++)
cin>>arrB[i];
//Now call the function merge sort()to get the resultant array with
appropriate arguments(values)
mergesort(arrA,rangeA,arrB,rangeB,arrC);
//Now display the resultant sorted array
rangeC=rangeA+rangeB;
cout<<"\n The resultant sorted array is given below:";
for(i=0;i<rangeC;i++)
cout<<"\n"<<arrC[i];
getch();
} //End of main( )

//Function definition
void mergesort(int arrA[ ],int rangeA,int arrB[ ],int rangeB,int arrC[ ])
{
//Type declaration statements
int CtrA=0,CtrB=0,CtrC=0;
//Now apply merge sort technique
while((CtrA<rangeA)&&(CtrB<rangeB))
{
if(arrA[CtrA]<=arrB[CtrB])
{
arrC[CtrC]=arrA[CtrA];
//Now increment the CtrC as well as CtrA by1
CtrC+=1;
CtrA+=1;
}//End of if
else
{
arrC[CtrC]=arrB[CtrB];
/*Now increment the CtrC as well as CtrB by 1*/
CtrC+=1;
CtrB+=1;
} //End of else
} //End of while loop
if(CtrA==rangeA)
{
/*Then copy the remaining element of arrB into arrC*/
while(CtrB<rangeB)
{
arrC[CtrC]=arrB[CtrB];
/*Now increment the CtrC and CtrB by 1*/
CtrC+=1;
CtrB+=1;
} //End of while loop
} //End of if
else
{
/*Then copy the remaining element of aaA into arrC*/
while(CtrA<rangeA)
{
arrC[CtrC]=arrA[CtrA];
//Now increment the CtrA and CtrC by 1
CtrC+=1;
CtrA+=1;
} //End of while loop
} //End of else
} //End of function definition
Output:-
Enter the range (max 50) for first array:5

Enter the elements of first array(elements should be in ascending order):


5
8
10
12
14

Enter the range for second array:6

Enter the elements of second array(elements should be in ascending order)


7
9
11
16
17
18

The resultant sorted array is given below:


5
7
8
9
10
11
12
14
16
17
18
TWO
DIMENSIONAL
ARRAY
•MATRIX
PROGRAM NO.6

//Program to add two matrices.


//Preprocessor directive statement(s)
#include<iostream.h>//For input and output
#include<conio.h>//For getch( ) and clrscr( )
//main( ) function
void main( )
{
clrscr( );//To clear the screen
//Type declaration instruction(s)
int a[10][10],b[10][10],c[10][10],rows1,columns1,rows2,columns2,i,j;
//Now accept the no. of rows and columns of first matrix from the user
cout<<"\n Enter the no. of rows for first matrix:";
cin>>rows1;
cout<<"\n Enter the no. of columns for first matrix:";
cin>>columns1;
//Now accept the elements for first matrix from the user
cout<<"\n Enter the elements for first matrix:";
for(i=0;i<rows1;i++)
for(j=0;j<columns1;j++)
cin>>a[i][j];
//Now accept the no. of rows and columns for second matrix
cout<<"\n Enter the no. of rows for second matrix:";
cin>>rows2;
cout<<"\n Enter the no. of columns for second matrix:";
cin>>columns2;
//Now accept the elements for second matrix from the user
cout<<"\n Enter the elements for second matrix:";
for(i=0;i<rows2;i++)
for(j=0;j<columns1;j++)
cin>>b[i][j];
//Now check whether the addition is possible or not
if((rows1==rows2)&&(columns1==columns2))
{
//Yes,addition is possible
//Now add two matrices
for(i=0;i<rows1;i++)
for(j=0;j<columns1;j++)
c[i][j]=a[i][j]+b[i][j];
//Now display the resultant matrix
cout<<"\n The resultant matrix is given below:";
for(i=0;i<rows1;i++)
{
cout<<"\n";
for(j=0;j<columns1;j++)
cout<<c[i][j]<<'\t';
}
}//End of if
else
{
//Addition is not possible
cout<<"\n Addition is not possible:";
}
getch ( );
}
Ouput:-

Enter the no. of rows for first matrix:3

Enter the no. of columns for first matrix:3

Enter the elements for first matrix:


1 2 3
4 5 6
7 8 9

Enter the no. of rows for second matrix:3

Enter the no. of columns for second matrix:3

Enter the elements for second matrix:


1 2 3
4 5 6
7 8 9

The resultant matrix is given below:


2 4 6
8 10 12
14 16 18
PROGRAM NO.7 

/*Program to find out transpose of a matrix*/

//Preprocessor directive statements


#include<iostream.h>//For input and output
#include<conio.h>//For getch ( ) and clrscr ( )

//main( ) function
void main( )
{
clrscr( );//To clear the screen
//Type declaration instruction(s)
int a[10][10],b[10][10],c[10][10],rows,columns,i,j;
//Now accept the no. of rows and columns for matrix
cout<<"\n Enter the no. of rows:";
cin>>rows;
cout<<"\n Enter the no. of columns:";
cin>>columns;
//Now accept the elements of matrix from the user
cout<<"\n Enter the elements of matrix:";
for(i=0;i<rows;i++)
for(j=0;j<columns;j++)
cin>>a[i][j];
//Now find out transpose of the matrix
for(i=0;i<columns;i++)
for(j=0;j<rows;j++)
b[i][j]=a[j][i];
//Now display the transpose of the matrix
cout<<"\n Transpose of matrix is given below:";
for(i=0;i<columns;i++)
{
cout<<"\n";
for(j=0;j<rows;j++)
cout<<b[i][j]<<'\t';
}//end of for loop
getch ( );
}//End of main()
Output:-
Enter the no. of rows:2

Enter the no. of columns:2

Enter the elements of matrix:


1 3
2 3

Transpose of matrix is given below:


1 2
3 3
ARRAY OF
STRUCTURE
PROGRAM NO.8 
/*Program to store information of ten employees by using array of structure and
then display the information of those employee(s) that the user want*/

//Preprocessor directive statements


#include<iostream.h>//for input and output
#include<conio.h>//for getch ( ) and clrscr ( )
#include<stdio.h>//for gets( ) function

//Structure definition
struct addr
{
int houseno;
char city[30];
char state[30];
};

struct emp//nested structure


{
int emp_code;
char name[20];
char desig[20];
addr address;
float basic;
};

/*Now declare the array of structure to store information of ten employees*/


emp employee[10];

//Function prototype(s)
void enter( );//To take input for storing the information about employee
void display(int);//To display information of particular employee
int search(int);//To locate the particular employee

//Function definition
void enter( )
{
//enter the information of ten employees
for(int i=0;i<10;i++)
{
clrscr();
cout<<"\n Employee Number "<<i+1<<" :";
cout<<"\n Enter the employee code:";
cin>>employee[i].emp_code;
cout<<"\n Enter the name of employee:";
gets(employee[i].name);
cout<<"\n Enter employee designation:";
cin>>employee[i].desig;
cout<<"\ Enter the address:houseno:";
cin>>employee[i].address.houseno;
cout<<"\n Enter the address:city:";
gets(employee[i].address.city);
cout<<"\n Enter the address:state:";
gets(employee[i].address.state);
cout<<"\n Enter the basic salary:";
cin>>employee[i].basic;
}//End of for loop
}//End of function definition

void display(int a)
{
//display the information of particular employee
cout<<"\n Employee code is:"<<employee[a].emp_code;
cout<<"\n Employee name is:"<<employee[a].name;
cout<<"\n Employee designation is:"<<employee[a].desig;
cout<<"\n Employee address is : "
<<employee[a].address.houseno<<'\t,'<<employee[a].address.city<<'\t,'
<<employee[a].address.state;
cout<<"\n Employee basic salary is:"<<employee[a].basic;
}//end of function definition

int search(int eno)


{
for(int i=0;i<10;i++)
{
if(employee[i].emp_code==eno)
return i;
}
return -1;
}//end of function definition

//main( ) function
void main( )
{
clrscr ( );//To clear the screen
//Type declaration statement
int ecode,i,pos;
char ch;
//Now call enter( ) function to store information about ten employees
enter( );
//Now execute the loop till the choice is'y'
do
{
//Accept the value of employee code to be searched
cout<<"\n Enter the employee code of which you want to see the
details";
cin>>ecode;
//Now call the function search( ) to locate the employee
pos=search(ecode);
//Now check whether the particular employee code exists or not
if((pos>=0)&&(pos<10))
{
//Yes empcode exists
//Now display the information of that employee by calling
display( ) function
display(pos);
}//End of if
else
{
//No,empcode doesnot exist
cout<<"\n Sorry:Employee information doesnot exists:";
}//End of else
cout<<"\n Do you want more:";
cin>>ch;
}while(ch=='y');//End of do
getch ( );
}//End of main( ) function
Output:-
Employee Number 1 :

Enter the employee code:201


Enter the name of employee:alka
Enter employee designation:manager
Enter the address:houseno:75
Enter the address:city:noida
Enter the address:state:up
Enter the basic salary:80000

Employee Number 2 :

Enter the employee code:202


Enter the name of employee:mamta
Enter employee designation:clerk
Enter the address:houseno:76
Enter the address:city:noida
Enter the address:state:up
Enter the basic salary:10000

Employee Number 3 :

Enter the employee code:203


Enter the name of employee:ritu
Enter employee designation:clerk
Enter the address:houseno:77
Enter the address:city:noida
Enter the address:state:up
Enter the basic salary:10000

Employee Number 4 :

Enter the employee code:204


Enter the name of employee:renu
Enter employee designation:accountant
Enter the address:houseno:78
Enter the address:city:noida
Enter the address:state:up
Enter the basic salary:11000

Employee Number 5 :

Enter the employee code:205


Enter the name of employee:ruchi
Enter employee designation:accountant
Enter the address:houseno:89
Enter the address:city:noida
Enter the address:state:up
Enter the basic salary:11000

Employee Number 6 :
Enter the employee code:206
Enter the name of employee:ram
Enter employee designation:clerk
Enter the address:houseno:89
Enter the address:city:noida
Enter the address:state:up
Enter the basic salary:10000

Employee Number 7 :

Enter the employee code:207


Enter the name of employee:rajesh
Enter employee designation:clerk
Enter the address:houseno:56
Enter the address:city:noida
Enter the address:state:up
Enter the basic salary:10000

Employee Number 8 :

Enter the employee code:208


Enter the name of employee:rakesh
Enter employee designation:clerk
Enter the address:houseno:54
Enter the address:city:noida
Enter the address:state:up
Enter the basic salary:10000

Employee Number 9 :

Enter the employee code:209


Enter the name of employee:rohit
Enter employee designation:accountant
Enter the address:houseno:43
Enter the address:city:noida
Enter the address:state:up
Enter the basic salary:11000

Employee Number 10 :

Enter the employee code:210


Enter the name of employee:rahul
Enter employee designation:clerk
Enter the address:houseno:66
Enter the address:city:noida
Enter the address:state:up
Enter the basic salary:10000

Enter the employee code of which you want to see the details : 209

Employee code is:209


Employee name is:rohit
Employee designation is:accountant
Employee address is:43 , noida , up
Employee basic salary is:11000
Do you want more:y

Enter the employee code of which you want to see the details : 201

Employee code is:201


Employee name is:alka
Employee designation is:manager
Employee address is:75 , noida , up
Employee basic salary is:80000
Do you want more:n
CLASSES AND
ARRAY OF
OBJECT
PROGRAM NO.9 

/*Program to first store information about 10 items by creating the array of


objects and display the information of only that item whose price is maximum
amongst the price of all the items*/

//Preprocessor directive statement(s)


#include<iostream.h>//for input and output
#include<conio.h>//for getch( ) and clrscr( )

//Class definition
class item
{
//Declaration of data members
int icode;
float price;
public:
//Member function(s) prototype
void getdata(int,float);
void dispdata( );
float retcost( );
};

//Member function(s) definition

void item::getdata(int ino,float cost)


{
//Assign the value to the data members
icode=ino;
price=cost;
}

void item::dispdata( )
{
//Display the data members on the screen
cout<<"\n The item code is:"<<icode;
cout<<"\n The price is:"<<price;
}

float item::retcost( )
{
return price;
}

//Declare the constant variable


const int size=10;

//Declare the array of objects


item order[size];

//Function(s) prototype(s)
void enter( );//To store information about 10 items
float maxcost( );//To find maximum cost amongst 10 items

//Main( ) function
void main( )
{
clrscr( ); //To clear the screen
//Type declaration statement(s)
float max_cost;
int i;
//Now call the enter( ) function to store information about 10 items
enter( );
/*Now call maxcost( ) to find out the maximum cost among 10 items and the
value returned by this function is of float type so store the value
returned by the function into max_cost*/
max_cost=maxcost( );
/*Now display the information of only those items whose price is maximum
amongst all the items*/
cout<<"\n the details of items whose price is maximum is given below:";
for(i=0;i<size;i++)
{
if(order[i].retcost( )==max_cost)
{
//call the display( ) member function of class item
order[i].dispdata();
}//End of if
}//End of for loop
getch( );
}//End of main( )

//Function(s) definition
void enter( )
{
int ino;
float cost;
/*Now execute the loop for 10 items to store information about 10 items*/
for(int i=0;i<10;i++)
{
cout<<"\n Enter the item code:";
cin>>ino;
cout<<"\n Enter the price of item:";
cin>>cost;
/*Now call getdata( ) function of class item to assign the value
input by user to the data members of the class*/
order[i].getdata(ino,cost);
}//End of for loop
}//End of function definition

float maxcost( )
{
float tempmax=order[0].retcost( ),tempcost;
for(int i=0;i<size;i++)
{
if(order[i].retcost( )>tempmax)
tempmax=order[i].retcost( );
}//End of for loop
//Now return the maximum cost
return tempmax;
}//End of function definition
Output :-
Enter the item code:101
Enter the price of item:300

Enter the item code:102


Enter the price of item:400

Enter the item code:103


Enter the price of item:500

Enter the item code:104


Enter the price of item:200

Enter the item code:105


Enter the price of item:150

Enter the item code:106


Enter the price of item:250

Enter the item code:107


Enter the price of item:750

Enter the item code:108


Enter the price of item:567

Enter the item code:109


Enter the price of item:234

Enter the item code:110


Enter the price of item:230

The details of the items whose price is maximum is given below:

The item code is : 107


The Price is :750
PROGRAM NO.10 
/*Program to find out grade of the marks obtained by the student by using
class*/

//Preprocessor directive statements


#include<iostream.h>//For input and output
#include<conio.h>//For getch ( ) and clrscr ( )
#include<stdio.h>//For gets( ) function

//class definition
class stu
{
//Data member(s) declaration
int rollno;
char name[20],grade;
int eng,maths,physics,chemistry,comp_sc;
//Memeber function(s) prototype
public:
void getdata( );//to read the values for data members
void set_grade( );//to set the grade
void disp_result( );//to display the result
};//End of class definition

//Member function(s) definition


void stu::getdata( )
{
cout<<"\n Enter the rollno:";
cin>>rollno;
cout<<"\n Enter the name:";
gets(name);
cout<<"\n Enter the marks in english:";
cin>>eng;
cout<<"\n Enter the marks in maths:";
cin>>maths;
cout<<"\n Enter the marks in physics:";
cin>>physics;
cout<<"\n Enter the marks in chemistry:";
cin>>chemistry;
cout<<"\n Enter the marks in comp sc.:";
cin>>comp_sc;
}

void stu::set_grade( )
{
//Type declaration statements(s)
float total,per;
int counter=0;
//Apply check to count number of subjects in which student is fail
if(eng<33)
counter=counter+1;
if(maths<33)
counter=counter+1;
if(physics<33)
counter=counter+1;
if(chemistry<33)
counter=counter+1;
if(comp_sc<33)
counter=counter+1;
//Set the grade
if(counter==0)
{
//Pass
grade='p';
//Now calculate the total
total=eng+maths+physics+chemistry+comp_sc;
per=(total/500)*100;
if(per>=80)
grade='A';
if((per>=60)&&(per<80))
grade='B';
if(per<60)
grade='C';
}
else
{
if(counter>2)
//fail
grade='f';
else
//compartment
grade='c';
}
}

void stu::disp_result( )
{
cout<<"\n Student's details with their Grade is given below:";
cout<<"\n Rollno is:"<<rollno;
cout<<"\n Name is:"<<name;
cout<<"\n Marks in english is:"<<eng;
cout<<"\n Marks in maths is:"<<maths;
cout<<"\n Marks in physics is:"<<physics;
cout<<"\n Marks is chemistry is:"<<chemistry;
cout<<"\n Marks in comp sc is:"<<comp_sc;
cout<<"\n Grade is:"<<grade;
}

//main( ) function
void main( )
{
clrscr ( );//to clear the screen
//Declare the object of class stu
stu student;
//Now call the member function in appropriate order
student.getdata( );
student.set_grade( );
student.disp_result( );
getch ( );
}
Output:-
Enter the rollno:2

Enter the name:Aayushi Rathore

Enter the marks in english:99

Enter the marks in maths:98

Enter the marks in physics:98

Enter the marks in chemistry:99

Enter the marks in comp sc.:99

Student's Details with their Grade is given below:


Rollno is:2
Name is:Aayushi Rathore
Marks in english is:99
Marks in maths is:98
Marks in physics is:98
Marks is chemistry is:99
Marks in comp sc is:99
Grade is:A
PROGRAM NO.11 
/*Program to separate integers and fractional parts from a number by using
class*/

//Preprocessor directive statements


#include<iostream.h>//for input and output
#include<conio.h>//for getch ( ) and clrscr ( )

//Class definition
class sepif
{
clrscr ( );//to clear the screen
//data members
int int_part;
float number,frac_part;
//member function(s)
public:
void getnumber( )//to read the number
{
cout<<"\n Enter the number:";
cin>>number;
}
void sep_parts( )//to separate integers and fractional parts
{
int_part=number;
frac_part=number-int_part;
}
void show_part( )//to show the result
{
cout<<"\n The number entered by the user is:"<<number;
cout<<"\n The integer part is:"<<int_part;
cout<<"\n The fractional part is:"<<frac_part;
}
};//End of class definition

//Main( ) function
void main( )
{
clrscr ( );//To clear the screen
//Declare the object
sepif sif;
/*Now call the member function(s) in appropriate manner*/
sif.getnumber( );
sif.sep_parts( );
sif.show_part( );
getch ( );
}
Output:-
Enter the number:25.458

The number entered by the user is:25.458


The integer part is:25
The fractional part is:0.458
DATA FILE
HANDLING
•TEXT FILE
•BINARY FILE
PROGRAM NO.12 

/*Program to count number of 'to' in an existing text file*/

//Preprocessor directive statements


#include<fstream.h>//For data file handling
#include<conio.h>//For getch( ) and clrscr( )
#include<stdlib.h>//For exit( ) function
#include<stdio.h>//For gets( ) function
#include<ctype.h>//For isalpha( ) function
#include<string.h>//For strcmpi() function

//main( ) function
void main ( )
{
clrscr ( );//To clear the screen
//Type declaration statement(s)
char filen[20],str[40];
int count=0;
//Now declare the input stream
ifstream fin;
//Now accept the file name from the user
cout<<"\n Enter the file name:";
gets(filen);
/*Now establish the connection of the file with input stream fin by using
open( ) function*/
fin.open(filen);
//*Now check whether there is internal problem while handling the file*/
if(!fin)
{
cout<<"\n Unable to open the file:";
exit(1);
}
/*Now execute the loop to read the characters from file till the end of
file is encountered*/
while(!fin.eof( ))
{
//Read the characters and store it in str
fin>>str;
/*Now compare str with word "to" */
if(strcmpi(str,"to")==0)
{
//Increment the count variable by 1
count+=1;
}
}//End of while loop
//Now remove the connection of file from input stream by using close( )
function
fin.close( );
//Now display the result
cout<<"\n The word -to- exists "<<count<<"times in the file "<<filen;
getch ( );
}//End of main( )
Output:-
Enter the file name:try.dat

The word -to- exists 2 times in the file try.dat


PROGRAM NO.13 

/*Program to create another file from an existing file having only those words
of an existing text file which starts from "T/t"*/

//Preprocessor directive sattements


#include<fstream.h>//For data file handling
#include<conio.h>//For getch( ) and clrscr( )
#include<stdlib.h>//For exit( ) function
#include<stdio.h>//For gets( ) function

//main( ) function
void main( )
{
clrscr ( );//To clear the screen
//Type declaration statement(s)
char sfilen[20],dfilen[20],str[40];
//Now declare the input stream
ifstream fin;
//Now declare the output stream
ofstream fout;
//Now accept the source file name from the user
cout<<"\n Enter the source file name:";
gets(sfilen);
/*Now establish the connection of file with input stream by using open( )
function*/
fin.open(sfilen);
/*Now check the internal problem while handling the file*/
if(!fin)
{
cout<<"\n Unable to open the file:";
exit(1);
}
//Now accept the destination file name
cout<<"\n Enter the destination file name:";
gets(dfilen);
//Now establish the connection of file with output stream by using open( )
function
fout.open(dfilen);
//Now check the internal problem while handling the file
if(!fout)
{
cout<<"\n Unable to create the file:";
exit(1);
}
/*Now execute the loop for reading the characters from source file and
writing only those words which starts from 'T' into the destination file*/
while(!fin.eof( ))
{
if(fin.eof())
break;
/*Now read the characters from the source file and store it in str*/
fin>>str;
//Now Apply check on str and write those words which starts from
'T/t' into the destination file .
if((str[0]=='t')||(str[0]=='T'))
fout<<str<<' ';

}//End of while loop


/*Now remove the connection of files with stream(input/output)*/
fin.close( );
fout.close( );
getch ( );
}//End of main( )
Output:-
Enter the source file name:try.dat

Enter the destination file name:try1.dat

Note:We can verify this by executing the type command at dos shell.
PROGRAM NO.14 

/*Program to calculate size of an existing file*/

//Preprocessor directive statements


#include<fstream.h>//For file handling
#include<conio.h>//For getch ( ) and clrscr ( )
#include<stdlib.h>//For exit( ) function
#include<stdio.h>//For gets( ) function

//main( ) function
void main( )
{
clrscr( );//To clear the screen
//Type declaration statement(s)
char filen[20],ch;
int size=0;
//Now declare the input stream
ifstream fin;
//Now accept the file from the user
cout<<"\n Enter the file name(existing):";
gets(filen);
//Now open the file by using open( ) function
fin.open(filen);
/*Now check whether there is internal problem while handling the file*/
if(!fin)
{
cout<<"\n Unable to open the file:";
exit(1);
}
//Now move the get pointer to the end of the file.
fin.seekg(0,ios::end);
//Now use tellg() function to determine the size of the file
size=fin.tellg();
//Now display the size of the file.
cout<<"\n The size of file: "<<filen<<" is: "<<size;
//Now close the file
fin.close( );
getch ( );
}//End of main( )
Output:-
Enter the file name(existing):try.dat

The size of file: student.dat is: 14


PROGRAM NO.15 
/*Program to count(No of alphabets , No of Upper case letters , No of lower case
letters , No of digits , No of Spaces , No of lines ) in an existing text file
according to user's choice*/

//Preprocessor Directive statement(s)


#include<fstream.h>//For data file handling
#include<conio.h>//For getch() and clrscr()
#include<stdio.h>//For gets() function
#include<stdlib.h>//For exit() function
#include<ctype.h>//For function related with character

//Function prototype(s)
int count_Text(char[] , int);//To count in a text File.

//Declaration of Global variable


int choice;

//main() function
void main()
{
clrscr();
//Type declaration Statement
int choice,count;
char fname[20];
//Now display the message for taking the file name from user
cout<<"\n enter the file Name:";
gets(fname);
//Now display the options
while(choice!=0)
{
clrscr();
cout<<"\n What Do You Want To Count :\n";
cout<<"\n 1. No Of Alphabets.";
cout<<"\n 2. No Of Upper Case Letters.";
cout<<"\n 3. No of Lowercase Letters." ;
cout<<"\n 4. No of Digits.";
cout<<"\n 5. No of Spaces.";
cout<<"\n 6. No of Lines.";
cout<<"\n 0. Exit";
cout<<"\n\n Enter your choice:";
cin>>choice;
//Now call the function count by passing the file name and choice
count = count_Text(fname , choice);
cout<<"\n Existence of Required Ones is : "<<count;
}//End of While
}//End of main()

//Function Definition
int count_Text(char fname[] , int choice)
{
//Type declaration statement
int count = 0;
char ch;
//Declare the stream.
ifstream fin(fname);
//Now check whether there is internal problem while handling the file
if(!fin)
{
cout<<"\n Unable to open the file :";
getch();
exit(1);
}
//Now read from the file and perform desired operations
switch(choice)
{
case 1:
//Set the Get Pointer at Byte No 0
fin.seekg(0);
while(fin)
{
fin.get(ch);
if(isalpha(ch))
count++;
}
break;
case 2:
//Set the Get Pointer at Byte No 0
fin.seekg(0);
while(fin)
{
fin.get(ch);
if(isupper(ch))
count++;
}
break;
case 3:
//Set the Get Pointer at Byte No 0
fin.seekg(0);
while(fin)
{
fin.get(ch);
if(islower(ch))
count++;
}
break;
case 4:
//Set the Get Pointer at Byte No 0
fin.seekg(0);
while(fin)
{
fin.get(ch);
if(isdigit(ch))
count++;
}
break;
case 5 :
//Set the Get Pointer at Byte No 0
fin.seekg(0);
while(fin)
{
fin.get(ch);
if(ch==' ')
count++;
}
break;

case 6:
//Set the Get Pointer at Byte No 0
fin.seekg(0);
while(fin)
{
fin.get(ch);
if(ch=='\n')
count++;
}
break;
case 0:
//Now Remove the connection
fin.close();
exit(1);

}//End of switch statement


return count;
}//End of function definition
Output:
Enter The File Name : myfile.txt

What Do You Want To Count :

1. No Of Alphabets.
2. No Of Upper Case Letters.
3. No of Lowercase Letters.
4. No of Digits.
5. No of Spaces.
6. No of Lines.
0. Exit

Enter your choice:1


Existence of Required Ones is : 25

What Do You Want To Count :

1. No Of Alphabets.
2. No Of Upper Case Letters.
3. No of Lowercase Letters.
4. No of Digits.
5. No of Spaces.
6. No of Lines.
0. Exit

Enter your choice:2


Existence of Required Ones is : 5

What Do You Want To Count :

1. No Of Alphabets.
2. No Of Upper Case Letters.
3. No of Lowercase Letters.
4. No of Digits.
5. No of Spaces.
6. No of Lines.
0. Exit

Enter your choice:3


Existence of Required Ones is : 20

What Do You Want To Count :

1. No Of Alphabets.
2. No Of Upper Case Letters.
3. No of Lowercase Letters.
4. No of Digits.
5. No of Spaces.
6. No of Lines.
0. Exit
Enter your choice:4
Existence of Required Ones is : 2

What Do You Want To Count :

1. No Of Alphabets.
2. No Of Upper Case Letters.
3. No of Lowercase Letters.
4. No of Digits.
5. No of Spaces.
6. No of Lines.
0. Exit

Enter your choice:5


Existence of Required Ones is : 7

What Do You Want To Count :


1. No Of Alphabets.
2. No Of Upper Case Letters.
3. No of Lowercase Letters.
4. No of Digits.
5. No of Spaces.
6. No of Lines.
0. Exit

Enter your choice:6

Existence of Required Ones is : 2


PROGRAM NO.16 
/*Program to first create the binary file and then display a particular
record(when records are implemented by using objects)*/

//Preprocessor directive statements


#include<fstream.h>//For Data file handling
#include<conio.h>//For getch() and clrscr()
#include<stdlib.h>//For exit() function
#include<stdio.h>//For gets() function

//Class definition
class stu
{
//Declaration part
//Data members
int rollno,marks;
char name[20];
//Member functions
public:
void getdata( )
{
cout<<"\n Enter the rollno:";
cin>>rollno;
cout<<"\n Enter the name:";
gets(name);
cout<<"\n Enter the marks:";
cin>>marks;
}
int getrno( )
{
return(rollno);
}
void disp_data( )
{
cout<<"\n Name of the student is:"<<name;
cout<<"\n Rollno of the student is:"<<rollno;
cout<<"\n Marks of the student is:"<<marks;
}
};

//main( ) function
int main( )
{
clrscr( ); //To clear the screen
//Type declaration statements
stu student;
char ch='y',found='n';
int rno;
/*Now declare the fstream object as well as establish the connection of
file with output stream*/
fstream finout("student.dat",ios::in|ios::out|ios::binary);
if(!finout)
{
cout<<"\n Unable to create the file:";
getch( );
exit(1);
}
while((ch=='y')||(ch=='Y'))
{
student.getdata();
finout.write((char*)&student,sizeof(student));
cout<<"\n Do you want more:";
cout<<"\n Enter y for yes........n for no:";
cin>>ch;
}
//Now remove the connection of file with output stream.
finout.close( );
cout<<"\n Enter the rollno to be searched:";
cin>>rno;
//Now again establish the connection of file with the fstream object.
finout.open("student.dat",ios::in|ios::binary);
finout.seekg(0);
finout.read((char*)&student,sizeof(student));
while(!finout.eof( ))
{
if(finout.eof( ))
break;
if(student.getrno( )==rno)
{
student.disp_data( );
found='y';
}finout.read((char*)&student,sizeof(student));
}
if(found=='n')
cout<<"\n Record does not exist:";
finout.close( );
getch( );
return 0;
}
Output:-
Enter the rollno:2

Enter the name:ABC

Enter the marks:99

Do you want more:


Enter y for yes........n for no:y

Enter the rollno:4

Enter the name:X

Enter the marks:98

Do you want more:


Enter y for yes........n for no:n

Enter the rollno to be searched:2

Name of the student is:ABC


Rollno of the student is:2
Marks of the student is:99
PROGRAM NO.17 

/*Program to create another binary file from the existing binary file by writing
only those records into new one from the existing one which will satisfy some
condition*/

//Preprocessor Directive Statment


#include<fstream.h>//For input and output
#include<conio.h>//For getch() and clrscr()
#include<string.h> //For strcpy() function
#include<stdio.h>//For gets() function
#include<stdlib.h>//For exit() function

//Class definition
class stu
{
//Data Members
int rollno ,marks;
char name[20];
public :
//Member Function(s)
//Constructor
stu()
{
rollno = 0;
marks = 0;
strcpy(name , " ");
}
void getdata() //To take input from the user
{
cout<<"\nEnter The rollno:";
cin>>rollno;
cout<<"\n Enter The Name:";
gets(name);
cout<<"\n Enter the marks :";
cin>>marks;
}
void dispdata() //To display the data
{
cout<<"\n The rollno is :"<<rollno;
cout<<"\n The name is :"<<name;
cout<<"\n The Marks is :"<<marks;
}
int ret_marks()//To return marks
{
return marks;
}
};

//main() Function
void main()
{
//Type declaration statement(s)
char sfile[20] , dfile[20];

//Declare the stream


fstream fbin , fbout;

//Declare the object


stu student;

//Now Enter the file names:


cout<<"\n Enter the Source file name :";
gets(sfile);
//Now check the internal problem
if(!fbin)
{
cout<<"\n unable to open the file :";
getch();
exit(1);
}
cout<<"\n Enter the Destination file Name :";
gets(dfile);
//Now check the internal problem
if(!fbout)
{
cout<<"\n unable to open the file :";
getch();
exit(1);
}

fbin.read((char*)&student , sizeof(student));
while(fbin)
{
if(fbin.eof())
break;
if(student.ret_marks()>=75)
fbout.write((char*)&student , sizeof(student));
fbin.read((char*)&student , sizeof(student));
}
//Now remove the connection of files with the stream
fbin.close();
fbout.close();
}
Output :
Enter The Source File Name : student.dat
Enter the Destination File name : s1.dat
DEFAULT
ARGUMENTS
PROGRAM NO.18 
/*Program to calculate the Simple Interest by making a function having default
arguments*/

//Preprocessor directive statements


#include<iostream.h>//For input and output
#include<conio.h>//For getch ( ) and clrscr ( )

//Function(s) prototype
void sinterest(float,int=2,float=0.08);

//main( ) function
void main( )
{
clrscr ( );//To clear the screen
cout<<"\n Case 1:(Principal Amount):";
//Now call the function sinterest( ) with one value(argument)
sinterest(2000);
cout<<"\n Case 2:(Principal Amount,Time):";
//Now call the function sinterest with two values(argument)
sinterest(3000,3);
cout<<"\n Case 3:(Principal Amount,Time ,Rate):";
//Now call the function sinterest with three values(argument)
sinterest(2000,3,0.09);
getch ( );
}//End of main( ) function

//Function definition
void sinterest(float princ,int time,float rate)
{
cout<<"\n Principal amount is:"<<princ;
cout<<"\n Time is:"<<time;
cout<<"\n Rate is:"<<rate;
cout<<"\n The simple interest is:"<<(princ*time*rate);
}//End of function definition
Output:-
Case 1:(Principal Amount):
Principal amount is:2000
Time is:2
Rate is:0.08
The simple interest is:320

Case 2:(Principal Amount,Time):


Principal amount is:3000
Time is:3
Rate is:0.08
The simple interest is:720

Case 3:(Principal Amount,Time,Rate):


Principal amount is:2000
Time is:3
Rate is:0.09
The simple interest is:540
STACK
AND QUEUE
•STACK
•QUEUE
PROGRAM NO.19 

/*Program to perform push and pop operations in a dynamically alloacted stack*/

//Preprocessor directive statements


#include<iostream.h>//For input and output
#include<conio.h> //For getch() and clrscr()
#include<process.h>//For exit() function

//Structure definition
struct Node
{
int info;
Node *link;
};

//Class definition
class Stack
{
//Data Members
Node *Top;
//Member Function(s)
//Constructor
public:
Stack( )
{
Top = NULL;
}
void Stack_Push( ) ;
void Stack_Pop();
void Stack_Disp();
};//End of class Definition

//Member Function(s) definition

void Stack :: Stack_Push( )


{
Node *newptr;
//Now allocate the memory
newptr = new Node;
//Now check whether the memory is allocated or not
clrscr();
if(newptr == NULL)
{
cout<<"\n No Memory Is Available :";
getch();

}
else
{
//Display the messsage for taking the info part from the user
cout<<"\n Enter The Information Part :" ;
cin>>newptr->info;
/*Now maintain the link part , for this check whether the node being
created is first node or not.*/
if(Top == NULL)
{
//yes it is the first node
newptr->link = NULL;
Top = newptr;
}
else
{
newptr->link = Top;
Top = newptr;
}

}//End of outer Else


}//End of function definition

void Stack :: Stack_Pop()


{
Node *delptr=Top;
//Now check whether the stack is empty or not
clrscr();
if(Top==NULL)
{
cout<<"There Are No Element To Delete:";
getch();

}
else
{
cout<<"\n The Deleted element is :"<<delptr->info;
Top = Top->link;
//Now deallocate the memory.
delete delptr;
}

}//End of Function definition

void Stack::Stack_Disp()
{
Node *Disp=Top;
//Now check whether the Stack is Empty or Not.
clrscr();
if(Top == NULL)
{
cout<<"\n The Stack Is Empty:";
getch();

}
else
{
cout<<"\n The Stack Status is :";
while(Disp!=NULL)
{
cout<<'\n'<< Disp->info;
Disp = Disp ->link;
}
}

}//End of Function Definition

//main() function
void main()
{
clrscr();
//Type Declaration Statement
int choice;
//Declare the object of class Stack
Stack stk;
while(choice!=0)
{
clrscr();
cout<<"\n Stack's Options Are given Below :\n";
cout<<"\n 1.PUSH:";
cout<<"\n 2.POP:";
cout<<"\n 3.Display :";
cout<<"\n 0.Exit:";
cout<<"\n Enter Your Choice :";
cin>>choice;
switch(choice)
{
case 1:
stk.Stack_Push();
getch();
break;
case 2:
stk.Stack_Pop();
getch();
break;
case 3:
stk.Stack_Disp();
getch();
break;
case 0:
exit(1);

}//End of Switch Statement

}//end of While
}//End of main() function
Output :
Push :

Stack's Options Are given Below :

1.PUSH:
2.POP:
3.Display :
0.Exit:
Enter Your Choice :1

Enter The Information Part :10

Stack's Options Are given Below :

1.PUSH:
2.POP:
3.Display :
0.Exit:
Enter Your Choice :1

Enter The Information Part :20

Stack's Options Are given Below :

1.PUSH:
2.POP:
3.Display :
0.Exit:
Enter Your Choice :1

Enter The Information Part :30

Display :
Stack's Options Are given Below :

1.PUSH:
2.POP:
3.Display :
0.Exit:
Enter Your Choice :3

The Stack Status is :


30
20
10
POP :

Stack's Options Are given Below :

1.PUSH:
2.POP:
3.Display :
0.Exit:
Enter Your Choice :2

The Deleted element is :30

Exit:

Stack's Options Are given Below :

1.PUSH:
2.POP:
3.Display :
0.Exit:
Enter Your Choice :0
PROGRAM NO.20 

/*Program to perform insert and delete operations in a dynamically alloacted


queue*/

//Preprocessor directive statements


#include<iostream.h>//For input and output
#include<conio.h> //For getch() and clrscr()
#include<process.h>//For exit() function

//Structure definition
struct Node
{
int info;
Node *Front , Rear;
};

//class definition
class Queue
{
//Data Members
Node *Front , Rear;
//Member Function(s)
//Constructor
public:
Queue( )
{
Front = NULL;
Rear = NULL;
}
void Queue_Push( ) ;
void Queue_Pop();
void Queue_Disp();
};//End of class Definition

//Member Function(s) definition


void Queue :: Queue_Push( )
{
Node *newptr;
//Now allocate the memory
newptr = new Node;
//Now check whether the memory is allocated or not
clrscr();
if(newptr == NULL)
{
cout<<"\n No Memory Is Available :";

}
else
{
//Display the messsage for taking the info part from the user
cout<<"\n Enter The Information Part :" ;
cin>>newptr->info;
//Now Maintain the link part
newptr->link = NULL;
//Now check whether the node being created is first node or not.
if(Rear == NULL)
{
//yes it is the first node
Front=newptr;
Rear = newptr;
}
else
{
Rear->link = newptr;
Rear = newptr;
}

}//End of outer Else


}//End of function definition

void Queue :: Queue_Pop()


{
Node *delptr=Front;
//Now check whether the Queue is empty or not
clrscr();
if(Front==NULL)
{
cout<<"There Are No Element To Delete:";

}
else
{
cout<<"\n The Deleted element is :"<<delptr->info;
Front = Front->link;
//Now deallocate the memory.
delete delptr;
//Now check whether the queue is Empty or not.
If(front==NULL)
Rear=NULL;
}

}//End of Function definition

void Queue::Queue_Disp()
{
Node *Disp=Front;
//Now check whether the Queue is Empty or Not.
clrscr();
if(Front == NULL)
{
cout<<"\n The Queue Is Empty:";

}
else
{
cout<<"\n The Queue Status is :";
while(Disp!=NULL)
{
cout<<'\n'<< Disp->info;
Disp = Disp ->link;
}
}
}//end of Function Definition

//main() function
void main()
{
clrscr();
//Type Declaration Statement
int choice;
//Declare the object of class Queue
Queue qu;
while(choice!=0)
{
clrscr();
cout<<"\n Queue Options Are given Below :\n";
cout<<"\n 1.Insert:";
cout<<"\n 2.Delete:";
cout<<"\n 3.Display :";
cout<<"\n 0.Exit:";
cout<<"\n Enter Your Choice :";
cin>>choice;
switch(choice)
{
case 1:
qu.Queue_Push();
getch();
break;
case 2:
qu.Queue_Pop();
getch();
break;
case 3:
qu.Queue_Disp();
getch();
break;
case 0:
exit(1);

}//End of Switch Statement

}//end of While
}//End of main() Function
Output :
Insert:

Queue Options Are given Below :

1.Insert:
2.Delete:
3.Display :
0.Exit:
Enter Your Choice :1

Enter The Information Part :40

Queue Options Are given Below :

1.Insert:
2.Delete:
3.Display :
0.Exit:
Enter Your Choice :1

Enter The Information Part :50

Queue Options Are given Below :

1.Insert:
2.Delete:
3.Display :
0.Exit:
Enter Your Choice :1

Enter The Information Part :60

Display :
Queue Options Are given Below :

1.Insert:
2.Delete:
3.Display :
0.Exit:
Enter Your Choice :3

The Queue Status is :


40
50
60
Delete :

Queue Options Are given Below :

1.Insert:
2.Delete:
3.Display :
0.Exit:
Enter Your Choice :2

The Deleted element is :40

Exit:

Queue Options Are given Below :

1.Insert:
2.Delete:
3.Display :
0.Exit:
Enter Your Choice :0
SQL
COMMANDS
• CREATE TABLE
• ALTER TABLE
• INSERT INTO
• SELECT
• UPDATE
• ORDER BY
• IN
• BETWEEN
• COUNT
• MIN
• MAX
• LIKE
• DELETE
• DROP TABLE

SQL COMMANDS

 Create Table:

1.Q Create a table Student containing following fields :


Name : String
Rollno: Numeric(Integer)(set it as primary key)
Marks:Numeric(Integer)

Ans : SQL> Create Table Student(Name char(20) , Rollno number Primary Key ,
Marks number);

Output: Table created.

 Alter Table

2.Q Alter Student to add new column Dob which is of date type.

Ans: SQL> Alter Table Student Add(Dob date);

Output: Table altered.

 Insert Into

3.Q Insert the records into Student.

Ans: SQL> Insert Into Student Values('Geeta',652,100,'27-oct-80');

Output: 1 row created.


SQL> Insert Into Student Values('Mamta',654,200,'27-nov-80');

Output: 1 row created.

SQL> Insert Into Student Values('Ritu',656,300,'27-aug-80');

Output: 1 row created.

 Select(To display all the fields)

4.Q (i) To view all the records of Student Table.

Ans: SQL> Select * from Student;

Output: NAME ROLLNO MARKS DOB


---------------------- --------- --------- ------
Geeta 652 100 27-OCT-80
Mamta 654 200 27-NOV-80
Ritu 656 300 27-AUG-80

 Select(To display particular fields)

(ii) To View only Rollno and Name of all the students of Student Table.

Ans: SQL> Select Rollno , Name from Student;

Output: ROLLNO NAME


--------- --------------------
652 Geeta
654 Mamta
656 Ritu

 Select(to display only those records which fulfill the particular condition)

(iii) To View Only Records of those students whose marks are greater than 200.

Ans: SQL> Select * from Student where Marks > 200;

Output: NAME ROLLNO MARKS DOB


-------------------- --------- --------- ---------
Ritu 656 300 27-AUG-80
 Select(With more than one condition)

(iv) To view records of only those Students whose Marks is Greater than 100 and
date of birth is ’27-Nov-80’.

Ans: SQL> Select * from Student where (Marks > 100) and (Dob ='27-nov-80');

Output: NAME ROLLNO MARKS DOB


-------------------- --------- --------- ---------
Mamta 654 200 27-NOV-80

 Select(with condition involving Date type data)

(v) To view record of only those student whose Dob is ’27-oct-80’

Ans: SQL> Select Name from Student where Dob = '27-oct-80';

Output: NAME
--------------------
Geeta

 Update

5.Q Update the marks of those students whose rollno is 654 from 200 to 250.

Ans: SQL> Update Student Set Marks = 250 where Rollno = 654;

Output:1 row updated.

We can verify it by using the following command.

SQL> Select * from student;


NAME ROLLNO MARKS DOB
-------------------- --------- --------- ---------
Geeta 652 100 27-OCT-80
Mamta 654 250 27-NOV-80
Ritu 656 300 27-AUG-80

 Order By

6.Q To view all the records of students sorted by the name in descending order.
Ans: SQL> Select * from Student Order By Name desc;

Output:NAME ROLLNO MARKS DOB


-------------------- --------- --------- ---------
Ritu 656 300 27-AUG-80
Mamta 654 250 27-NOV-80
Geeta 652 100 27-OCT-80

 Order By (With date Type Data)

7.Q To view only name of all the students sorted according to date of birth in ascending
order.

Ans: SQL> Select Name from Student Order By Dob;

Output: NAME
--------------------
Ritu
Geeta
Mamta

 IN

8.Q To view records of only those students whose name is either geeta or mamta

Ans: SQL>Select * from Student where Name In('Geeta' , 'Mamta');

Output:NAME ROLLNO MARKS DOB


-------------------- --------- --------- ---------
Geeta 652 100 27-OCT-80
Mamta 654 250 27-NOV-80

 Between

9.Q To view name and date of birth of those students whose dob lies between ’27-Oct-80’
and
’27-aug-81’.

Ans: SQL> Select Name , Dob from Student where Dob between '27-oct-80' and '27-nov-
80'
Output:NAME DOB
-------------------- ---------
Geeta 27-OCT-80
Mamta 27-NOV-80

 Count

10.Q To count number of students who have got marks greater than 200.

Ans: SQL> Select Count(*) from Student Where Marks > 200;

Output:COUNT(*)
------------------
2

 Min

11.Q To calculate the minimum amongst marks of the students.

Ans: SQL> Select Min(Marks) from Student;

Output:MIN(MARKS)
-------------------
100

 Max

12.Q To calculate the maximum amongst marks of the students .

Ans: SQL> Select Max(Marks) from Student;

Output:MAX(MARKS)
-------------------
300

 Like

13.Q To view Name and Roll no of those students whose Name has ‘t’ character within it.

Ans: SQL> Select Name , Rollno from Student where Name like '%t%'

Output: NAME ROLLNO


-------------------- ---------
Geeta 652
Mamta 654
Ritu 656

 Delete

14.Q To delete the record of those students whose name starts with ‘g’ character.

Ans: SQL> Delete from Student where Name like 'g%';

Output:1 row deleted.

We can verify it with the help of following command.

SQL> Select * from Student;

NAME ROLLNO MARKS DOB


-------------------- --------- --------- ---------
Mamta 654 250 27-NOV-80
Ritu 656 300 27-AUG-80

 Drop Table

15.Q To delete the Student table

Ans: SQL>Drop Table Student;

Output:Table dropped

You might also like