Program - 1: Write A Program To Input Day Number of A Week and Display The Corresponding Day Name
Program - 1: Write A Program To Input Day Number of A Week and Display The Corresponding Day Name
#include <iostream.h>
#include <conio.h>
void main()
{
int day;
cout << "Enter a number between 1 to 7: ";
cin >> day;
switch(day)
{
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Number entered is invalid" << endl;
}
getch();
}
OUTPUT:
2
Program - 2: Write a program to implement the concept of multiple inheritance.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class person
{
char name[21];
int age;
public:
void indata()
{
cout<<"\nEnter the name of Student: " ;
gets(name);
cout<<"\nEnter the age : ";
cin>>age;
}
void outdata();
};
void person::outdata()
{
cout<<"\n\n";
for(int i=0; i<79; i++)
cout<<"-";
cout<<"\nName of the student is: "<<name;
cout<<"\nAge of the student is : "<<age;
}
class game
{
char game_name[20];
public:
void input()
{
cout<<"\nEnter the game name : ";
cin.get();
cin.getline(game_name,20);
}
void output()
{
cout<<"\n\nGame opted by the student is : "<<game_name;
}
};
class student: public person, public game
{
float Tmarks;
int rollno;
public:
char calgrade()
{
if(Tmarks>90)
return 'A';
else if(Tmarks>80&&Tmarks<=90)
return 'B';
else if(Tmarks>70&&Tmarks<=80)
return 'C';
else if(Tmarks>60&&Tmarks<=70)
3
return 'D';
else
return 'E';
}
void enter()
{
indata();
cout<<"\n\nEnter the roll number: "; cin>>rollno;
input();
cout<<"\n\nEnter total marks (out of 100) : ";
cin>>Tmarks;
}
void display()
{
outdata();
cout<<"\n\nRoll number : "<<rollno;
output();
cout<<"\n\nTotal marks are : "<<Tmarks;
cout<<"\n\nGrade = "<<calgrade();
}
};
void main()
{
clrscr();
student A;
A.enter();
cout<"The details of the student are : ";
A.display();
getch();
}
OUTPUT:
4
Program - 3: Write a program to implement the concept of multi-level inheritance.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class person
{
char name[21];
int age;
public:
void indata()
{
cout<<"\nEnter the name of Student: " ;
gets(name);
cout<<"\nEnter the age : ";
cin>>age;
}
void outdata();
};
void person::outdata()
{
cout<<"\n\n";
for(int i=0; i<79; i++)
cout<<"-";
cout<<"\n\nName of the student is: "<<name;
cout<<"\n\nAge of the student is : "<<age;
}
class game : public person
{
char game_name[20];
public:
void input()
{
cout<<"\nEnter the game name : ";
cin.get();cin.getline(game_name,20);
}
void output()
{
cout<<"\n\nGame opted by the student is : "<<game_name;
}
};
class student: public game
{
float Tmarks;
int rollno;
public:
char calgrade()
{
if(Tmarks>90)
return 'A';
else if(Tmarks>80&&Tmarks<=90)
return 'B';
else if(Tmarks>70&&Tmarks<=80)
return 'C';
else if(Tmarks>60&&Tmarks<=70)
return 'D';
5
else
return 'E';
}
void enter()
{
indata();
cout<<"\nEnter the roll number: "; cin>>rollno;
input();
cout<<"\nEnter total marks (out of 100) : ";
cin>>Tmarks;
}
void display()
{
outdata();
cout<<"\n\nRoll number : "<<rollno;
output();
cout<<"\n\nTotal marks are : "<<Tmarks;
cout<<"\n\nGrade = "<<calgrade();
}
};
void main()
{
clrscr();
student A;
A.enter();
cout<<"The details of the student are :";
A.display();
getch();
}
OUTPUT:
6
Program - 4: Write a program to illustrate working of function overloading.
#include<iostream.h>
#include<math.h>
#include<conio.h>
float area(int p)
{
return p*p;
}
float area(float p)
{
return 3.14*p*p;
}
float area(float p,float q)
{
return p*q;
}
float area(int p, int q)
{
return (0.5*p*q);
}
float area(float p,float q, float r)
{
float s;
s=(p+q+r)/2;
return sqrt(s*(s-p)*(s-q)*(s-r));
}
void main()
{
clrscr();
int p,q,ch;
float a,b,r;
char choice;
do
{
cout<<"\n\nChoose from the following : ";
cout<<"\n\n1. Area of square ";
cout<<"\n\n2. Area of circle ";
cout<<"\n\n3. Area of rectangle ";
cout<<"\n\n4. Area of right triangle ";
cout<<"\n\n5. Area of triangle ";
cout<<"\n\nEnter your choice : "; cin>>ch;
switch(ch)
{
case 1: cout<<"\n\nEnter side of square : ";
cin>>p;
cout<<"\n\narea of square is : "<<area(p);
break;
case 2: cout<<"\n\nEnter radius of circle : ";
cin>>a;
cout<<"\n\narea of circle is : "<<area(a);
break;
case 3: cout<<"\n\nEnter length of rectangle : ";
cin>>a;
cout<<"\n\nEnter breadth of rectangle : ";
7
cin>>b;
cout<<"\n\narea of rectangle is : "<<area(a,b);
break;
case 4: cout<<"\n\nEnter base and altitude of right triangle : ";
cin>>p;
cin>>q;
cout<<"\n\narea of right triangle is : "<<area(q,p);
break;
case 5: cout<<"\n\nEnter sides of triangle : ";
cin>>a;
cin>>b;
cin>>r;
cout<<"\n\narea of triangle is : "<<area(a,b,r);
break;
}
cout<<"\n\nWant to choose again : ";
cin>>choice;
}
while(choice=='y'||choice=='Y');
getch();
}
OUTPUT:
8
Program - 5: Write a program to demonstrate the concept of array of objects.
#include<iostream.h>
#include<conio.h>
class rec
{
private:
int I;
int b;
public:
rec(int a,int c)
{
I=a;
b=c;
}
void put()
{
cout<<"Area is : "<<I*b <<endl;
}
};
void main()
{
clrscr();
rec obj[3]={rec(3,6),rec(2,5),rec(5,5)};
cout<<"Displaying Areas of Rectangles : \n";
for(int i=0;i<3;i++)
{
obj[i].put();
}
getch();
}
OUTPUT:
9
Program - 6: Write a program to demonstrate the concept of Constructor overloading.
#include <iostream.h>
#include <conio.h>
class Area
{
private:
int length;
int breadth;
public:
// Constructor
Area(): length(5), breadth(2){ }
void GetLength()
{
cout << "Enter length and breadth respectively: ";
cin >> length >> breadth;
}
int AreaCalculation()
{
return (length * breadth);
}
void DisplayArea(int temp)
{
cout << "Area: " << temp;
}
};
void main()
{
clrscr();
Area A1, A2;
int temp;
A1.GetLength();
temp = A1.AreaCalculation();
A1.DisplayArea(temp);
cout << endl << "Default Area when value is not taken from user" << endl;
temp = A2.AreaCalculation();
A2.DisplayArea(temp);
getch();
}
OUTPUT:
10
Program - 7: Write a program to demonstrate the concept of Copy constructor.
#include <iostream.h>
#include <conio.h>
class copy
{
private:
int X;
int Y;
public:
copy (int a, int b);
copy (const copy &d);
void Display();
};
copy:: copy(int a, int b)
{
X = a;
Y = b;
}
copy:: copy(const copy &d)
{
X = d.X;
Y = d.Y;
}
void copy:: Display()
{
cout << endl << "X: " << X;
cout << endl << "Y: " << Y << endl;
}
int main()
{
copy d1(10,20) ;
cout << endl <<"D1 Object: " << endl;
cout << "Value after initialization : " ;
d1.Display();
copy d2 = copy(d1);
cout << endl << "D2 Object: " << endl;
cout << "Value after initialization : ";
d2.Display();
getch();
}
OUTPUT:
11
Program - 8: Write a program to create a single file and display its contents.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
system("cls");
ofstream fout("student", ios::out);
char name[30], ch;
float marks =0.0;
for(int i=0;i<5;i++) //Loop to get 5 records
{
cout<<"Student "<<(i+1)<<":\tName: ";
cin.get(name,30);
cout<<"\t\tMarks: ";
cin>>marks;
cin.get(ch); //to empty input buffer write to the file
fout<<name<<"\n"<<marks<<"\n";
}
fout.close(); //disconnect student file from fout
ifstream fin("student", ios::in); //connect student file to input stream fin
fin.seekg(0); //To bring file pointer at the file beginning
cout<<"\n";
for(i=0;i<5;i++) //Display records
{
fin.get(name,30); //read name from file student
fin.get(ch);
fin>>marks; //read marks from file student
fin.get(ch);
cout<<"Student Name: "<<name;
cout<<"\tMarks: "<<marks<<"\n";
}
fin.close(); //disconnect student file from fin stream
getch();
}
OUTPUT:
12
Program - 9: Write a program to find the sum of even/odd numbers using recursion.
#include<iostream.h>
#include<conio.h>
int sum_even(int );
int sum_odd(int );
int sum=0;
int num=0;
void main()
{
clrscr();
int n;
int s_e, s_o;
cout<<"\n\nEnter the number upto which you want the sum of even/odd numbers : ";
cin>>n;
cout<<"\n\n The list of integers is : \n\n";
for(int l=1; l<=n; l++)
cout<<l<<"\n";
s_e=sum_even(n);
s_o=sum_odd(n);
cout<<"\n\nThe sum of even numbers upto "<<n<<" = "<<s_e;
cout<<"\n\nThe sum of odd numbers upto "<<n<<" = "<<s_o;
getch();
}
int sum_even(int n)
{
if(num%2==0)
sum=sum+num;
13
OUTPUT:
14
Program - 10: Write a program using pointers to find the smallest and the largest element in a
dynamically created array.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *array, smallest, largest, n;
cout<<"\n\nEnter the number of elements : ";
cin>>n;
array=new[n];
while(i!=n)
{
cout<<array[i]<<" ";
i++;
}
smallest=array[0];
largest=array[0];
getch();
15
OUTPUT:
16
Program - 11: Write a program to swap two integers using pointers.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
swap_using_pointers(&a,&b);
getch();
OUTPUT:
17
Program - 12: Write a program to implement stack as an array.
#include<iostream.h>
#include<conio.h>
#include<process.h>
class stack
{
int num[10],top,max;
public:
stack()
{
top=-1;
max=9;
}
void push(int n)
{
if(top==max)
cout<<"\nStack Overflow,Push not Possible";
else
{
top++;
num[top]=n;
}
}
void pop()
{
if(top==-1)
cout<<"\nStack Underflow,Stack does not exist";
else
{
int temp=num[top];
top--;
cout<<"\nDeleted Number is:"<<temp;
}
}
void traverse();
}st;
void stack::traverse()
{
if(top==-1)
cout<<"\nStack Underflow";
else
for(int i=top;i>=0;i--)
cout<<num[i]<<" ";
}
void main()
{
clrscr();
int ch,no;
menu:
cout<<"\nMenu\n1.Push\n2.Pop\n3.Traverse\n4.Exit\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter number:";
18
cin>>no;
st.push(no);
break;
case 2:
st.pop();
break;
case 3:
st.traverse();
break;
case 4:
getch();
exit(0);
default:
cout<<"\nWrong Choice!Please Re-enter";
}
goto menu;
}
OUTPUT:
19
Program - 13: Write a program to implement stack as a Linked List.
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
class stack
{
int rno;
char n[42];
float per;
public:
stack *next;
void getd()
{
cout<<"\nEnter Roll No:";
cin>>rno;
cout<<"Enter Name:";
gets(n);
cout<<"Enter Percentage:";
cin>>per;
next=NULL;
}
void showd()
{
cout<<"\nRoll No:"<<rno<<"\nName:"<<n<<"\nPercentage:"<<per;
}
};
stack *newptr,*top=NULL,*ptr;
void main()
{
clrscr();
int ch;
menu:
cout<<"\n\nMenu\n1.Push\n2.Pop\n3.Traverse\n4.Exit\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
newptr=new stack;
if(!newptr)
{
cout<<"\n\t\aMemory Allocation Error!";
getch();
exit(0);
}
newptr->getd();
if(top==NULL)
top=newptr;
else
{
newptr->next=top;
top=newptr;
}
break;
case 2:
20
if(top==NULL)
cout<<"\n\aStack Underflow,no element";
else
{
ptr=top;
top=top->next;
cout<<"Deleted Element:";
ptr->showd();
delete ptr;
}
break;
case 3:
ptr=top;
if(top==NULL)
cout<<"\nStack Does Not Exist";
else
while(ptr!=NULL)
{
ptr->showd();
ptr=ptr->next;
}
break;
case 4:
getch();
exit(0);
default:
cout<<"\nWrong Choice Entered,Please Re-Enter";
}
goto menu;
}
OUTPUT:
21
Program - 14: Write a program to implement Queue as an array.
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{const int max=10;
int ch,num,front=-1,rear=-1;
int queue[max];
clrscr();
menu:
cout<<"\n\nMenu\n1.Push\n2.Pop\n3.Traverse\n4.Exit\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the number:";
cin>>num;
if(rear==max-1)
cout<<"\n\aQueue overflow";
else
{
if(front==-1)
{
front++;
queue[front]=num;
rear=front;
}
else
{
rear++;
queue[rear]=num;
}
}
break;
case 2:
if(front==-1)
cout<<"\nQueue does not exist";
else if(rear==front)
{
num=queue[front];
front=-1;
rear=-1;
cout<<"\nDeleted element:"<<num;
}
else
{
num=queue[front];
front++;
cout<<"\nDeleted Element:"<<num;
}
break;
case 3:
if(front==-1)
cout<<"\nQueue does not exist";
else
22
for(int i=front;i<=rear;i++)
cout<<queue[i]<<" ";
break;
case 4:
getch();
exit(0);
default:
cout<<"Wrong Choice!Please Re-enter:";
}
goto menu;
}
OUTPUT:
23
Program - 15: Write a program to implement Queue as a Linked List.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
class queue
{
int rno;
char n[15];
float per;
public:
queue *next;
void getd()
{
cout<<"\nEnter Roll No:";
cin>>rno;
cout<<"Enter Name:";
gets(n);
cout<<"Enter Percentage:";
cin>>per;
next=NULL;
}
void showd()
{
cout<<"\nRoll No:"<<rno<<"\nName:"<<n<<"\nPercentage:"<<per;
}
};
queue *newptr=NULL,*ptr=NULL,*front=NULL,*rear=NULL;
void main()
{
clrscr();
int ch;
menu:
cout<<"\n\nMenu\n1.Push\n2.Pop\n3.Traverse\n4.Exit\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
newptr=new queue;
if(!newptr)
{
cout<<"\n\t\aMemory Allocation Error!";
getch();
exit(0);
}
newptr->getd();
if(front==NULL)
{
front=newptr;
rear=newptr;
}
else
{
rear->next=newptr;
rear=rear->next;
24
rear->next=front;
}
break;
case 2:
if(front==NULL)
cout<<"Queue underflow (does not exist)";
else
{
ptr=front;
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
{
front=front->next;
rear->next=front;
}
cout<<"\nDeleted Element:";
ptr->showd();
delete ptr;
}
break;
case 3:
if(front==NULL)
cout<<"Queue does not exist";
else
{
ptr=front;
while(ptr->next!=front)
{
ptr->showd();
ptr=ptr->next;
}
ptr->showd();
}
break;
case 4:
getch();
exit(0);
default:
cout<<"\nWrong Choice Entered,Please Re-enter";
}
goto menu;
}
25
OUTPUT:
26
Program - 16: Write a program to implement Queue as a circular array.
#include<iostream.h>
#include<conio.h>
#include<process.h>
const int max=5;
void main()
{
clrscr();
int q[max],front=-1,rear=-1,ch,num;
menu:
cout<<"\n\nMenu\n1.Push\n2.Pop\n3.Traverse\n4.Exit\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
if(front==(rear+1)%max)
cout<<"Queue Overflow";
else if(front==-1&&rear==-1)
{
front=0;
rear=0;
cout<<"Enter the number:";
cin>>num;
q[front]=num;
}
else
{
rear=(rear+1)%max;
cout<<"\nEnter the number:";
cin>>num;
q[rear]=num;
}
break;
case 2:
if(front==-1&&rear==-1)
cout<<"\nQueue does not exist";
else if(front==rear)
{
num=q[front];
cout<<"\nThe deleted number:"<<num;
front=-1;
rear=-1;
}
else
{
num=q[front];
cout<<"\nThe deleted number is:"<<num;
front=(front+1)%max;
}
break;
case 3:
if(front==-1&&rear==-1)
cout<<"\nQueue does not exist";
else
{
27
for(int i=front;i!=rear;i=(i+1)%max)
cout<<q[i]<<" ";
cout<<q[rear]<<endl;
}
break;
case 4:
getch();
exit(0);
default:
cout<<"Wrong Choice,Please Re-Enter:";
}
goto menu;
}
OUTPUT:
28
Program - 17: Write a program to illustrate the concept of structure.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
cout<<"Enter consumer number:";
cin>>a.no;
cout<<"Enter consumer name:";
gets(a.name);
cout<<"Enter no. of units consumed:";
cin>>a.unit;
if(a.unit<=100)
a.bill=a.unit*0.4;
if(a.unit>100 && a.unit<=200)
a.bill=(a.unit-100)*0.5+40;
if(a.unit>200 && a.unit<=300)
a.bill=(a.unit-200)*0.75+90;
if(a.unit>300)
a.bill=(a.unit-300)*1+165;
cout<<"\nBill";
cout<<"\nConsumer No:"<<a.no;
cout<<"\nName:"<<a.name;
cout<<"\nUnits consumed:"<<a.unit;
cout<<"\nNet amount=Rs"<<a.bill;
getch();
}
OUTPUT:
29
Program - 18: Write a program to add any two matrices using multi-dimensional array.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
cout << "Enter number of rows (between 1 and 100): ";
cin >> r;
cout << "Enter number of columns (between 1 and 100): ";
cin >> c;
cout << endl << "Enter elements of 1st matrix: " << endl;
// Storing elements of first matrix entered by user.
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}
// Storing elements of second matrix entered by user.
cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
// Adding Two matrices
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
// Displaying the resultant sum matrix.
cout << endl << "Sum of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if(j == c - 1)
cout << endl;
}
getch();
}
OUTPUT:
30
Program - 19: Write a program to find transpose of a matrix using multi-dimensional array.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a[10][10], trans[10][10], r, c, i, j;
cout << "Enter rows and columns of matrix: ";
cin >> r >> c;
// Storing element of matrix entered by user in array a[][].
cout << endl << "Enter elements of matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter elements a" << i + 1 << j + 1 << ": ";
cin >> a[i][j];
}
// Displaying the matrix a[][]
cout << endl << "Entered Matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << " " << a[i][j];
if(j == c - 1)
cout << endl << endl;
}
// Finding transpose of matrix a[][] and storing it in array trans[][].
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
trans[j][i]=a[i][j];
}
// Displaying the transpose,i.e, Displaying array trans[][].
cout << endl << "Transpose of Matrix: " << endl;
for(i = 0; i < c; ++i)
for(j = 0; j < r; ++j)
{
cout << " " << trans[i][j];
if(j == r - 1)
cout << endl << endl;
}
getch();
}
OUTPUT:
31
Program - 20: Write a program to multiply any two matrices using multi-dimensional array.
#include <iostream.h>
#include <conio.h>
void main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
// If column of first matrix in not equal to row of second matrix,
// ask the user to enter the size of matrix again.
while (c1!=r2)
{
cout << "Error! column of first matrix not equal to row of second.";
cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
}
// Storing elements of first matrix.
cout << endl << "Enter elements of matrix 1:" << endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c1; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}
// Storing elements of second matrix.
cout << endl << "Enter elements of matrix 2:" << endl;
for(i = 0; i < r2; ++i)
for(j = 0; j < c2; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
// Initializing elements of matrix mult to 0.
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
mult[i][j]=0;
}
// Multiplying matrix a and b and storing in array mult.
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
for(k = 0; k < c1; ++k)
{
mult[i][j] += a[i][k] * b[k][j];
}
// Displaying the multiplication of two matrix.
cout << endl << "Output Matrix: " << endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
32
cout << " " << mult[i][j];
if(j == c2-1)
cout << endl;
}
getch();
}
OUTPUT:
33
Program - 21: Write a program to show significance of tellp, tellg, seekp and seekg.
#include <iostream.h>
#include<conio.h>
#include <fstream.h>
int main()
{
clrscr();
fstream st; // Creating object of fstream class
st.open("Data.txt",ios::out); // Creating new file
34
OUTPUT:
35
Program - 22: Write a program to demonstrate the searching operation in binary file.
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
class student
{
int rollno;
char name[20];
char branch[3];
float marks;
char grade;
public:
void getdata()
{
cout<<"Rollno: ";
cin>>rollno;
cout<<"Name: ";
cin>>name;
cout<<"Subject: ";
cin>>branch;
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';
}
}
void putdata()
{
cout<<"Rollno: "<<rollno<<"\tName: "<<name<<"\n";
cout<<"Marks: "<<marks<<"\tGrade: "<<grade<<"\n";
}
int getrno()
{
36
return rollno;
}
}stud1;
void main()
{
clrscr();
clrscr();
int rno;
long pos;
char found='f';
fio.seekg(0);
while(!fio.eof())
{
pos=fio.tellg();
fio.read((char *)&stud1, sizeof(stud1));
if(stud1.getrno() == rno)
{
stud1.putdata();
fio.seekg(pos);
found='t';
break;
}
}
if(found=='f')
{
cout<<"\nRecord not found in the file..!!\n";
cout<<"Press any key to exit...\n";
getch();
exit(2);
}
fio.close();
getch();
}
37
OUTPUT:
38
Program - 23: Write a menu driven program for selection sort, bubble sort, insertion sort.
#include<iostream.h>
void bubblesort(int arr[],int m)
{ int temp,ctr=0;
for(int i=0;i<m;++i)
{ for(int j=0;j<((m-1)-i);++j)
{
if (arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
void selectionsort(int arr[],int m)
{
int pos=0,small,temp;
for(int i=0;i<m;++i)
{
small=arr[i];
pos=i;
for(int j=i+1;j<m;++j)
{
if(small>arr[j])
{
small=arr[j];
pos=j;
} }
temp=arr[i];
arr[i]=arr[pos];
arr[pos]=temp; } }
void insertionsort(int arr[],int m)
{
int temp,j;
for(int i=1;i<m;++i)
{
temp=arr[i];
j=i-1;
while(temp<arr[j] && j>=0)
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
}
}
int main()
{
clrscr();
int arr[30],m,x;
char ch='y';
do
{
39
cout<<"Enter Size of the array::";
cin>>m;
cout<<"Enter elements of Array::";
for(int i=0;i<m;++i)
{
cin>>arr[i];
}
cout<<"Select the sorting Method:";
cout<<"\n1)Bubble Sort. \n2)Selection Sort. \n3)Insertion Sort.";
cout<<"\n Enter ur choice::";
cin>>x;
switch(x)
{
case 1:
{ cout<<"Sorting using Bubble sort";
bubblesort(arr,m);
break;
}
case 2:
{ cout<<"Sorting Using Selection Sort";
selectionsort(arr,m);
break;
}
case 3:
{ cout<<"Sorting Using insertion Sort";
insertionsort(arr,m);
break;
}
default:
cout<<"Wrong Selection";
}
cout<<"\nSorted array is as follows::\n";
for(x=0;x<m;++x)
cout<<"\t"<<arr[x];
cout<<"\n Do u want to continue::";
cin>>ch;
}while(ch=='y' || ch=='Y');
}
40
OUTPUT:
41
Program - 24: Write an SQL command for the queries for the questions relation SHOP shown below:
(i) Show the names of all shops which are in the South area and cust-percent < 75.
(ii) To display number of shops in each city.
(iii) To display list of all the shops with sale > 300000 in ascending order of Shop_Name.
(iv) To display Shop_name,Area and Rating for only thos shops whose sale is between 350000 and
400000 (including both 350000 and 400000).
(v) To count the no. shops whose rating is A.
OUTPUT:
42
Program - 25: Write an SQL command for the queries for the questions relation EMPDETS and LEAVE
shown below:
TABLE: EMPDETS
OUTPUT:
(i) SELECT Emp_name
FROM EMPDETS
ORDER BY Salary desc;
(ii) SELECT count(*)
FROM EMPDETS
WHERE Salary>15000 AND Experience>5;
OUTPUT:
44
Program - 27: Write an SQL command for the queries for the questions given based on a relation TRAIN
shown below:
(iii) SELECT *
FROM TRAIN
ORDER BY Train_No.;
(v) SELECT *
FROM TRAIN
GROUP BY Class
45
Program - 28: Write an SQL command for the queries for the questions relation ITEM and CUSTOMER
shown below:
TABLE: ITEM
TABLE: CUSTOMER
(i) SELECT *
FROM CUSTOMER
WHERE CITY = 'DELHI';
(ii) SELECT *
FROM ITEM
WHERE PRICE BETWEEN 35000 AND 55000;
46
Program - 29: Write an SQL command for the queries for the questions relation SENDER and RECIPIENT
shown below:
TABLE: SENDER
TABLE: RECIPIENT
(iii) SELECT *
FROM RECIPIENT
ORDER BY RECNAME;
47