0% found this document useful (0 votes)
90 views52 pages

Computer Science: Practical File

The document contains the index for 20 C++ programs and SQL queries. It lists programs on topics like arrays, classes, files, sorting, and more. The student is submitting the programs to fulfill a practical assignment on C++ and MySQL. Each question provides the code for a program and its output.

Uploaded by

Rancho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views52 pages

Computer Science: Practical File

The document contains the index for 20 C++ programs and SQL queries. It lists programs on topics like arrays, classes, files, sorting, and more. The student is submitting the programs to fulfill a practical assignment on C++ and MySQL. Each question provides the code for a program and its output.

Uploaded by

Rancho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

COMPUTER SCIENCE

Practical File
On
C++ PROGRAMS
&
MY SQL

ROLL NO . :-
Class –

Submitted By :-

C++ PROGRAMS
INDEX
Q1.Enter name , age and salary and display it by using outside
the class member .

Q2. Program which gives the squares of all the odd numbers
stored in an array .

Q3.Program to store numbers in an array and allow user to


enter any number which he/she wants to find in array .(Binary
Search)

Q4.Program for copy constructor .

Q5.Enter numbers in an array in any order and it will give the


output of numbers in ascending order .

Q6. Enter any number from 2 digit to 5 digit and the output
will be the sum of all the distinguish digits of the numbers .

Q7.Enter rows and columns and the output will be the sum of
the diagonals .

Q8. Enter item no. , price and quantity in a class .

Q9.Enter any line or character in a file and press * to exit the


program .
Q10. Program will read the words which starts from vowels
stored in a file .

Q11. Enter employee no , name , age , salary and store it in a


file.
Q12. Deleting the information of employee by entering the
employee number .

Q13. Enter any number and its power and the output will the
power of the number .

Q14. Enter 3 strings and the output will show the longest
string and the shortest string .

Q.15 Enter any number and the output will be all the prime
numbers up to that number .

Q16. Selection sort

Q17. Using loop concept the program will give the output of
numbers making a right triangle .

Q18. Program for Stacks and Queue .

Q.19Enter two arrays and the output will be the merge of two
arrays .

Q20. Sequence sort .

Q.SQL QUERIES AND OUTPUT .


Q1.Enter name , age and salary and display it by using outside
the class member .
#include<iostream.h>
#include<conio.h>
class abc
{
//private:
char n[20];
int a;
float sal;
public:
void getdata(void)
{
cout<<"\nEnter name ";
cin>>n;
cout<<"\nEnter age ";
cin>>a;
cout<<"\nEnter salary ";
cin>>sal;
}
void putdata(void)
{
cout<<"\n\nNAME IS "<<n;
cout<<"\nAGE IS "<<a;
cout<<"\nSALARY IS "<<sal;
}
};
main()
{
abc p;
clrscr();
p.getdata();
p.putdata();
getch();
}
OUTPUT
Q2. Program which gives the squares of all the odd numbers
stored in an array .

#include<iostream.h>
#include<conio.h>
main()
{
int a[20],i,x;
clrscr();
cout<<"\nEnter no of elements ";
cin>>x;
for(i=0;i<x;i++)
{
cout<<"\nEnter no ";
cin>>a[i];
}
cout<<"\n\nArr1: ";
for(i=0;i<x;i++)
cout<<" "<<a[i];

cout<<"\n\nArr1: ";
for(i=0;i<x;i++)
{
if(a[i]%2==1)
a[i]=a[i]*a[i];
cout<<" "<<a[i];
}

getch();
}
OUTPUT
Q3.Program to store numbers in an array and allow user to
enter any number which he/she wants to find in array .(Binary
Search)

#include<iostream.h>
#include<conio.h>
main()
{
int a[20],n,i,no,low=0,high,mid,f=0;
clrscr();
cout<<"\nEnter no of elements ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter no ";
cin>>a[i];
}
cout<<"\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];

cout<<"\nEnter no to be search ";


cin>>no;

high=n-1;
while(low<=high && f==0)
{
mid=(low+high)/2;

if(a[mid]==no)
{
cout<<"\n\n"<<no<<" store at "<<mid+1;
f=1;
break;
}
else if(no>a[mid])
low=mid+1;
else
high=mid-1;
}
if(f==0)
cout<<"\n\n"<<no<<" does not exist";
getch();
}
OUTPUT
Q4.Program for copy constructor .

#include<iostream.h>
#include<conio.h>
#include<string.h>
class data
{
char n[20];
int age;
float sal;
public:
data(){}

data(char x[],int a,float k)


{
strcpy(n,x);
age=a;
sal=k;
}

data(data &p)
{
strcpy(n,p.n);
age=p.age;//+20;
sal=p.sal;//+1000;
}

display()
{
cout<<"\n\nNAME : "<<n;
cout<<"\n\nAGE : "<<age;
cout<<"\n\nSalary: "<<sal;

}
};
main()
{
clrscr();
data d("ajay",44,5000); //implicit caling
data d1(d);
data d2=d;
data d3;
d3=d2;
d.display();
d1.display();
d2.display();
d3.display();
getch();
}

OUTPUT

Q5.Enter numbers in an array in any order and it will give the


output of numbers in ascending order .(Bubble Sort)
#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],n,i,j,t;
clrscr();
cout<<"\nEnter no of elements in A ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter no ";
cin>>a[i];
}
cout<<"\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];

for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}

cout<<"\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];
getch();
}

OUTPUT
Q6. Enter any number from 2 digit to 5 digit and the output
will be the sum of all the distinguish digits of the numbers .

#include<iostream.h>
#include<conio.h>
main()
{
int a=0,b=0,c;
clrscr();
cout<<"\nEnter value for A ";
cin>>a;
while(a>0) // for(;a>0;) or for(i=a;i>0;i=i/10)
{
c=a%10;
b=b+c;
a=a/10;
}
cout<<"\nSum of digits "<<b;

getch();
}

OUTPUT

Q7.Enter rows and columns and the output will be the sum of
the diagonals .

#include<iostream.h>
#include<conio.h>
main()
{
int a[10][10],i,j,r,c;
clrscr();
cout<<"\nEnter no of rows ";
cin>>r;
cout<<"\nEnter no of Col ";
cin>>c;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
cout<<"\nEnter no ";
cin>>a[i][j];
}

int sum=0,sum1=0;
for(i=0;i<r;i++)
{
cout<<"\n";
for(j=0;j<c;j++)
{
cout<<" "<<a[i][j];
/// if(i==j) //&& a[i][j]>0)
// cout<<a[i][j];
// else
// cout<<" ";
//sum=sum+a[i][j];
/* if(i+j==r-1)// && a[i][j]>0)
cout<<a[i][j];
else
cout<<" "; */
if(i==j)
sum1=sum1+a[i][j];
}
}

for(i=0;i<r;i++)
{
cout<<"\n";
for(j=0;j<c;j++)
{
// cout<<" "<<a[i][j];
/* if(i==j) //&& a[i][j]>0)
cout<<a[i][j];
else
cout<<" ";*/
//sum=sum+a[i][j];
if(i+j==r-1)// && a[i][j]>0)
// cout<<a[i][j];
// else
// cout<<" ";
sum=sum+a[i][j];
}
}

cout<<"\n\nFirst diagonal sum "<<sum1;


cout<<"\n\nSecond diagonal sum "<<sum;

getch();
}

OUTPUT

Q8. Enter item no. , price and quantity in a class .

#include<iostream.h>
#include<conio.h>
class dd
{
char item[20];
int pr,qty;
public:
void getdata();
void putdata();
};
void dd::getdata()
{
cout<<"\nEnter item name ";
cin>>item;
cout<<"\nEnter price ";
cin>>pr;
cout<<"\nEnter quantity ";
cin>>qty;
}
void dd::putdata()
{
cout<<"\n\nItem name:"<<item;
cout<<"\n\nPrice: "<<pr;
cout<<"\n\nQty:"<<qty;
}

main()
{
dd a1,o1,k1;
clrscr();
a1.getdata();
o1.getdata();
k1.getdata();

a1.putdata();
o1.putdata();
k1.putdata();
getch();
}

OUTPUT
Q9.Enter any line or character in a file and press * to exit the
program .
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
main()
{
char ch;
clrscr();
ofstream f1("emp"); //implicit
//ofstream f1;
//f1.open("emp",ios::out); //explicit
cout<<"\nEnter char ";
while(1) //infinity
{
ch=getche(); // to input a single charactor by keybord.
if(ch=='*')
break;
if(ch==13)
{
cout<<"\n";
f1<<'\n';
}
f1<<ch;
}
f1.close();
//getch();
}

OUTPUT
Q10. Program will read the words which starts from vowels
stored in a file .
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
main()
{
char ch[80];
int cnt=0;
clrscr();
ifstream f1("emp");
ofstream f2("temp");
while(!f1.eof())
{
f1>>ch;
cout<<ch<<"\n";
if(ch[0]=='a' || ch[0]=='e' || ch[0]=='o' || ch[0]=='i' || ch[0]=='u')
f2<<"\n"<<ch;
}
f1.close();
f2.close();

cout<<"\n\nName start with vowels \n\n";


f1.open("temp",ios::in);
while(f1) //while(!f1.eof())
{
f1>>ch;
cout<<"\n"<<ch;
if(f1.eof())
break;

}
f1.close();

getch();
}

OUTPUT
Q11. Enter employee no , name , age , salary and store it in a
file.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
int row=5,col=2;
class abc
{
private:
int empno;
char n[20];
int age;
float sal;
public:
void getdata()
{
char ch;
cin.get(ch); // To empty buffer
cout<<"\nEnter employee no ";
cin>>empno;
cout<<"\nEnter name ";
cin>>n;
cout<<"\nEnter age ";
cin>>age;
cout<<"\nEnter salary ";
cin>>sal;
}
void putdata()
{
// gotoxy(col,row);
cout<<"\n"<<empno;
// gotoxy(col+10,row);
cout<<"\t"<<n;
// gotoxy(col+25,row);
cout<<"\t"<<age;
// gotoxy(col+35,row);
cout<<"\t"<<sal;
// row++;
}
};
main()
{
clrscr();
abc p,p1;
fstream f1("emp5.txt",ios::in|ios::out|ios::binary);
int i;
for(i=0;i<3;i++)
{
p.getdata();
f1.write((char *)&p,sizeof(p));
}
cout<<"\n\nEMPNO\tNAME\tAGE\tSALARY\n";
f1.seekg(0);
//f1.clear();
//clrscr();
for(i=0;i<3;i++)
{

f1.read((char*)&p1,sizeof(p1));
p1.putdata();
}
f1.close();
getch();
}

OUTPUT
Q12. Deleting the information of employee by entering the
employee number .

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
class abc
{
private:
int empno;
char n[20];
int age;
float sal;
public:
void getdata()
{
cout<<"\nEnter employee no ";
cin>>empno;
cout<<"\nEnter name ";
cin>>n;
cout<<"\nEnter age ";
cin>>age;
cout<<"\nEnter salary ";
cin>>sal;
}
void putdata()
{
cout<<"\n\n"<<empno<<"\t"<<n<<"\t"<<age<<"\t"<<sal;
}
int get_empno()
{
return empno;
}
};
main()
{
abc p,p1;
clrscr();
ifstream f1("emp5.txt",ios::in|ios::binary);
ofstream f2("temp",ios::out|ios::binary);

int i,emp;
cout<<"\n\nNAME\tAGE\tSALARY";
f1.clear();
f1.seekg(0);
//for(i=0;i<4;i++)
while(!f1.eof())
{
f1.read((char*)&p1,sizeof(p1));
if(f1.eof())
break;
p1.putdata();
}

cout<<"\nEnter employee no ";


cin>>emp;
f1.clear();
f1.seekg(0);

//while(f1)
while(!f1.eof())
{
f1.read((char*)&p1,sizeof(p1));
if(f1.eof())
break;
if(emp!=p1.get_empno())
f2.write((char*)&p1,sizeof(p1));
}
f1.close();
f2.close();
remove("emp5.txt");
rename("temp","emp5.txt");

f1.open("emp5.txt",ios::in|ios::binary);
cout<<"\n\nNAME\tAGE\tSALARY";
f1.seekg(0);
//for(i=0;i<3;i++)
while(!f1.eof())
{
f1.read((char*)&p1,sizeof(p1));
if(f1.eof())
break;
p1.putdata();
}
f1.close();
getch();
}
OUTPUT

Q13. Enter any number and its power and the output will the
power of the number .

#include<iostream.h>
#include<conio.h>
#include<math.h>
#define CUBE(a,b) pow(a,b)
//a>b?a:b
main()
{
int x,y,z;
clrscr();
cout<<"\nEnter base value ";
cin>>x;
cout<<"\nEnter power ";
cin>>y;

z=CUBE(x,y);
cout<<"\n"<<z;
getch();
}

OUTPUT

Q14. Enter 3 strings and the output will show the longest
string and the shortest string .

#include<iostream.h>
#include<conio.h>
#include<string.h>
main()
{
char n[20],n1[20],n2[20];
int l1,l2,l3;
clrscr();
cout<<"\nEnter String1 ";
cin>>n;
cout<<"\nEnter String2 ";
cin>>n1;
cout<<"\nEnter String3 ";
cin>>n2;
l1=strlen(n);
l2=strlen(n1);
l3=strlen(n2);
(l1>l2 && l1>l3) ?cout<<"\nLong: "<<n:(l2>l1 && l2>l3) ? cout<<"\nLong: "<<n1
:cout<<"\nLong: "<<n2;
(l1<l2 && l1<l3)?cout<<"\nshort:"<<n:(l2<l1 && l2<l3)?
cout<<"\nshort:"<<n1:cout<<"\nshort:"<<n2;

getch();
}

OUTPUT
Q.15 Enter any number and the output will be all the prime
numbers up to that number .

#include<iostream.h>
#include<conio.h>
main()
{
int n,i,j,p;
clrscr();
cout<<"\nEnter no of N ";
cin>>n;
for(i=1;i<=n;i++)
{
p=0;
for(j=2;j<=i/2;j++)
if(i%j==0)
p=1;

if(p==0)
cout<<" "<<i;
}
getch();
}

OUTPUT

Q16. Selection sort


#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],n,i,j,t,min,p;
clrscr();
cout<<"\nEnter no of elements in A ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter no ";
cin>>a[i];
}
cout<<"\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];

for(i=0;i<n;i++)
{
min=a[i];
p=i;
for(j=i;j<n;j++)
{
if(a[j]<min)
{
min=a[j];
p=j;
}
}
t=a[i];
a[i]=a[p];
a[p]=t;
}

cout<<"\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];
getch();
}

OUTPUT
Q17. Using loop concept the program will give the output of
numbers making a right triangle .

#include<iostream.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
cout<<"\n\n";
for(j=1;j<=i;j++)
cout<<" "<<i;
}
getch();
}

OUTPUT

Q18. Program for Stacks and Queue .

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct queue
{
int x;
queue *next;
}*front=NULL,*rear;

void insert(int);
void delnode();
void display();

void main()
{
int a,ch;
clrscr();
do
{
cout<<"\nEnter 1 for Insert";
cout<<"\nEnter 2 for Delete";
cout<<"\nEnter 3 for display";
cout<<"\nEnter 4 for exit";
cout<<"\n\nEnter your choice ";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nEnter no for insert ";
cin>>a;
insert(a);
break;
case 2: delnode();
break;
case 3: display();
break;
case 4: exit(0);
}
}
while(1);
getch();
}
void insert(int no) // char str[]
{
queue *ptr;
ptr=new queue; // strcpy(ptr->n,str)
// ptr=(struct queue*)malloc(sizeof(struct queue));
ptr->x=no;
ptr->next=NULL;
if(front==NULL)
{
front=ptr;
rear=ptr;
}
else
{
rear->next=ptr;
rear=ptr;
}
}
void delnode()
{
int p;
queue *ptr;
if(front==NULL)
{
cout<<"\n\nQueue is Empty";
return;
}
p=front->x; //strcpy(p,front->n)
ptr=front;
front=front->next;
delete ptr;
cout<<"\n\ndeleted element "<<p<<"\n";
}

void display()
{
queue *ptr;
cout<<"\nQueue now:- \n";
for(ptr=front;ptr!=NULL;ptr=ptr->next)
cout<<" "<<ptr->x;
}

OUTPUT
Q.19Enter two arrays and the output will be the merge of two
arrays .
# include <iostream.h>
# include <conio.h>
main()
{
int arr1[100],arr2[100],arr3[100],c,i=0,j=0,k=0,size1,size2;
clrscr();
cout<<"\n enter no of element of arr1 ";
cin>>size1;
for (i=0;i<size1;i++)
{
cout<<"\n enter no. ";
cin>>arr1[i];
}
cout<<"\n enter no of element of arr2 ";
cin>>size2;
for (i=0;i<size2;i++)
{
cout<<"\n enter no. ";
cin>>arr2[i];
}
cout<<"\n arr1 ";
for (i=0;i<size1;i++)
cout<<" "<<arr1[i];

cout<<"\n arr2 ";


for (i=0;i<size2;i++)
cout<<" "<<arr2[i];

i=0;j=0;k=0;
while (i<size1 && j<size2)
{
if (arr1[i]<arr2[j])
arr3[k++]=arr1[i++];

else if (arr2[j]<arr1[i])
arr3[k++]=arr2[j++];

else
i++;
}
while (i<size1)
arr3[k++]=arr1[i++];

while (j<size2)
arr3[k++]=arr2[j++];
cout<<"\n arr3 ";

for (i=0;i<k;i++)
cout<<" "<<arr3[i];

getch();
}

OUTPUT

Q20.Sequence Sort (Insertion Sort)

#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],n,i,j,t;
clrscr();
cout<<"\nEnter no of elements in A ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter no ";
cin>>a[i];
}
cout<<"\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];

for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}

cout<<"\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];
getch();
}

OUTPUT
My SQL
School homework

QUERIES
Ques 1. mysql> select ename ,sal,dept_no from empl
where commission is null ;
Ques 2. mysql> select ename ,sal,dept_no from empl where
commission is not null;

Ques 3. mysql> select * from empl where sal between


1000 and 2500;
Ques 4. mysql> select ename from empl where ename like
‘a%’ ;

Ques 5 . mysql> select concat (ename,dept_no)as “name


dept” from empl;
Ques 6 . mysql> select lower(ename) “empl name” from
empl;

Ques 7 . mysql> select upper(ename) “empl name” from


empl;
Ques 8 . mysql> select substr(job,1,5) from empl where
empno=8888 or empno=8900;

Ques 9 . mysql> select job,instr (job,’le’)as “le in ename”


from empl;
Ques 10 . mysql> select ename, length (ename)as “name
length” from empl where empno <=8888 ;
OUTPUT QUESTIONS
Ques 1 . mysql> select left(‘Informatices practices’,5) ;

Ques 2 . mysql> select right (‘Informatices practices’,5);

Ques 3 . mysql> select mid(‘Informatices practices’,6,9);


Ques 4 . mysql> select ltrim(‘ akshit’);

Ques 5 . mysql> select rtrim(‘akshit ‘);

You might also like