cs assignment(2)

42
1 C++ Programs I) # include <iostreaem.h> #include <conio.h> # include <stdlib.h> void main() { int i; cout<<´Enter The Octal Numberµ; cin>>i; char num[10]; itoa(i,num,2); cout<<´\nThe Binary Equivalent Is: µ<<num; getch(); } II) #include<iostream.h> #include<conio.h> #include<string.h> void main() {intop,a.b.c; clrscr( ); cout<<"\t\t Claculator Menu"; cout<<"\n 1. Addition\n 2.Subtraction \n 3.Multipilcat ion\n4.division\n5.exit"; cout,,"]\Enter the option"; cin>>op; switch(op); {case 1; cout<<"\n enter values of a,b"; cin>>a>>b; c=a+b; cout<<"addition is:"<<c; break; case 2: cout<<"\n Enter the values of a and b";

Upload: palaash-bhatnagar

Post on 06-Apr-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 1/42

1

C++ Programs

I) # include <iostreaem.h>

#include <conio.h># include <stdlib.h>void main() {int i;cout<<´Enter The Octal Numberµ;cin>>i;char num[10];itoa(i,num,2);

cout<<´\nThe Binary Equivalent Is: µ<<num;getch();}

II) #include<iostream.h>#include<conio.h>#include<string.h>

void main() {intop,a.b.c;clrscr();cout<<"\t\t Claculator Menu";cout<<"\n 1. Addition\n 2.Subtraction \n 3.Multipilcation\n4.division\n5.exit";cout,,"]\Enter the option";cin>>op;switch(op);{case 1;

cout<<"\n enter values of a,b";cin>>a>>b;c=a+b;cout<<"addition is:"<<c;break;case 2:cout<<"\n Enter the values of a and b";

Page 2: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 2/42

cin>>a>>b;c=a-b;cout<<"substraction is:"<<c;break;

case 3:cout<<\n Enter the valus of a,b;cin>>a>>b;c=a*b;cout<<"multiplication is:<<c;break;case 4;cout<<"enter the values of a,b";cin>>a>>b;c=a/b;

cout<<"division is"<<c;break;default :cout<<\n sorry wrong choice";}getch();return 0;}

III) # include <iostream.h># include<conio.h>#include <math.h>intx,n;

void PRINT_SERIS() {float sum=0;inti,a=3;

cout<<µ1 µ;for(i=2;i<=n;i++) {if(i%2==0) cout<<µ - µ<<pow(x,a)/a;else

Page 3: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 3/42

cout<<µ +µ<<pow(n,a)/a;a+=2;}}

voidmain();cout<<"enter the limit of series";cin>>n;cout<<"/n/n";cout<<"enter the value of x:";cin>>x;cout<<"/n/n";PRINT_SERIS();

getch();}

IV) #include<iostream.h>#include<conio.h>#include,string.h>void TRANSPOSE (int A[3],[3]) {cout<<\n Transpose of a given matrix is ;" ;for(i=0;i,3;i++);int A[3][3];B[3][3];i,j[cout<<"\n";for(j=0;j<3;j++) { b [i][j]<<" " ;}}}

void main() {cout<<"enter elements of matrix";for(i=0;i<3;i++) for(j=0;j,3:j++) cin>>a[i][j];cout<<"given matrix is:";

Page 4: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 4/42

for(i=0;i<3;i++) { cout<<"\n";for(j=0;j<3;j++) cout<<A[i][j]<<" ";

}

V) #include <iostream.h>#include <conio.h>void main() {intarr[10],a,i,j,k,b[100],c[100;cout<<"\nenter 10 elements";

for (int i=0;i<10;i++);{cin>>arr[i];

}cout<<"enter the number ";cin>>a;for (int i=0;i<10;i++) {ifarr[i]=0;} j=0;k=0;for (int i=0;i<10;i++) {if (arr[i]==0) 

{b[j]=arr[i];cout<<"b[j]; j++;

}elseif(arr[i]!=0) 

{c[k]=arr[i];

k++;}

} j=0;for (i=k;i<10;i++) {b[i]=c[j];

Page 5: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 5/42

 j++;}for (int i=0;i<10;i++) {

cout<<"b[j]<<" ";}getch();}

VI) # include <iostream.h>#include <conio.h>

#include <string.h>struct telephone{charfname[10];charlname[10];inttelno;};void main() {clrscr();telephone list[5];inti,j;telephone temp;for (i=0;i<5;i++) cout<<"enter data for first name and last name";cout<,endl;cin>>list[i].fname;cin>>list[i].lname;cout<<"enter the telephonenumber:-";cin>>list[i].teleno;}

for (i=0;i<5;i++) {

for(j=0;j<5;j++) {if ((strcmp(list[i].lname,list[j].lname))>0) 

{temp=list[i];list[i]=list[j];

Page 6: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 6/42

list[j]=temp;}

elseif (strcmp(list[i].lname,list[j].lname)==0&&strcmp(list[i].fname,list[j].fname)>0) 

{temp=list[i];list[i]=list[j];list[j]=temp;

}}}cout<<"data fnmae and lname in ascending order :-\n\n";cou<<endl;for (i=0;i<j;j++) 

{cout<<list[i].lname<<"\t"<<list[i].fname<<"\t"<<list[i].telno;}getch();}

VII) #include<fstream.h>#include<conio.n>#include<string.h>#include<ctype.h>struct country{charcountrynm[20];char capital[20];floatper_capita_in;}C;void main() {fstream f1("Countries.dat";,ios::binary);char con[20],ch='y';

intx,y;cout<<"To enter a country's name and to view its capital, per capita income (Press 1 ) \nTo enter the name of the capital and view the country's name, per capita income";cin>>x;while(ch=='y'||ch=='Y') 

Page 7: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 7/42

{if(x!=1||x!=2) {cout<<"Invalid entry";exit(0);}

elseif(x==1) {while(f1.eof()) {cout<<"Please enter the country's name:";cin.getline(con,20);f1.read((char*)&C,sizeof(C)); y=strcmp(C.countrynm,con);if(y==0) {cout<<"Capital:"<<C.capital;

cout<<"Per capita income:"<<C.per_capita_in;}}}elseif(x==2) {while(!f1.eof()) {cout<<"Enter the capitals name:";cin.getline(con,20);f1.read((char*)C, sizeof(c)); y=strcmp(c.capital,con);if(y==0) {cout<<"country's name:"<<C.countrynm;cout<<"\n Per capita income:"<<C.per_capita_in;}}}cout<<"would you wish to continue(Y/N):";cin>>ch;

}getch();}

VIII) 

Page 8: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 8/42

#include<fstream.h>#include<conio.h>struct stud{int roll;

floateng,maths,CS;

intdist() {float tot=eng+maths+CS;float dis=tot/300*100;if(dis>75) return 0;elseif(dis<40) 

return 1;elsereturn -1;}void top() {ifstream f3("Student.dat",ios::binary);int top1=top2;float t1=t2=0;float tot;while(!f3.eof()) {f3.read(char*)S,sizeof(S));tot=S.eng+S.maths+S.CS;if(tot>t1) {t1=tot;top1=S.roll;}elseif(tot>t2) {t2=tot;

top2=S.roll;}}f3.close();fstream f4("Student.dat",ios::binary);

Page 9: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 9/42

while(!f4.eof()) {f4.read((char*)&S, sizeof(S)) if(S.roll==top1) {cout<<"The student who scored the highest is"<<top1;

cout<<"The marks obtained in english,maths,computer science are"<<S.eng<<""<<S.maths<<" "<<S.CS<<"respectively";cout<<"\n the total marks obtained="<<t1;}if(S.roll==top2) {cout<<"The student who scored the highest is"<<top2;cout<<"The marks obtained in english,maths,computer science are"<<S.eng<<""<<S.maths<<" "<<S.CS<<"respectively";cout<<"\n the total marks obtained="<<t2;

}}}

void main() {int r;intdis,y=z=0;float e, m , c;fstream f1("Student.dat",ios::binary);for(int i=0; i<5; i++) {cout<<"Please enter the student's roll no.";cin>>r;cout<<"Please enter the marks obtained in english:";cin>>e;cout<<"Please enter the marks obtained in maths:";cin>>m;cout<<"Please enter the marks obtained in computer science:";cin>>c;S.roll=r;

S.eng=e;S.maths=m;S.CS=c;f1.write((char*)&S,sizeof(S));}f1.close();

Page 10: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 10/42

10

fstream f2("Student.dat",ios::binary);int x=0;while(x<5) {f2.read((char*)S, sizeof(S));

dis=dist();if(dis==0)  y++;elseif(dis==1) z++;}cout<<"The no. of students who passed with distinction is:"<<y;cout<<"\n The no. of students who failed is:"<<z;

f2.close();getch();

}

IX) # include <iostreaem.h>#include <conio.h>class string{Public:char string [10];chari,j,str 1 [10];void read();void display();void reverse();voidconcay();};void string ::read() 

{cout<<"enter the string/n";cin.getline (str,10);}void string ::display() {cout<<"the string is :-/n/n"<<str;}

Page 11: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 11/42

11

void string ::reverse() {read();for (i=9;i>0;i--) cout<<"/n"str[i]"/n/n";

void string ::concat() {read;cout"enter the string";cin.getline (str1,10);

 j=0;for (i=0;i<20;i++) {str[i]=str[j]; j++;}

display();}

void main() {clrscr():string s2;s2.reverse();s2.concat();getch();}

X) # include <iostream.h># include <conio.h># include <stdlib.h>class FLATS{Private:intReg_code;char name [50];

char type;float cost;Public:voidread_data();voiddisplay_data();intlucky_draw();

Page 12: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 12/42

12 

};void FLATS::read_data() {cout<<"enter the registration code/n";

cin>>regcode;cout<<"enter the name/n";cin>>name;cout<<"enter the type/n";cin>>type;cout<<"enter the cost";cin>>cost;}void FALTS ::display_data() 

{cout<<"the registration code is :-"<<regcode;cout<<'the name of yhe person is :-"<<name;cout<<"the type of the flat is :-"<<type;cout<<the cost of the flat is :-"<<cost;}int FLATS ::lucky_draw() {int d=random(10) return(d);}void main() {clrscr();FLAT d1[10];int i;for (i=0;i<3;i++) {d1[i].read_data();

}FLAT d2;int e=d2.lucky_draw();cout<<e;d1[e].display_data();d2[e].lucky_draw();

Page 13: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 13/42

13 

d2[e]display_data();getch();}

XI) # include <fstreaem.h>#include <conio.h>

class person{public:char name[25];

int age;};

classstudent:public person{introllno,avgmks;};

classgraduatestudent:public student{constint graduates=0,division=0,work=0,students=0;char subject[25];int employed;\\0 for not and 1 for yesvoid count() {students++;if(employed) work++;if(avgmks>=49) graduates++;if(avgmks>=60 &&avgmks<70) division++;

}}obj[100];

int main() {clrscr();int i=0,place=0;ifstream fin(´studdtls.datµ);

Page 14: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 14/42

14 

while(!fin.eof()) {fin.read((char*)&obj[i],sizeof(obj[i]);obj[i].count();

if(obj[place].avgmks<obj[i].avgmks) place=I;}cout<<´\t\t\t\tCOLLEGE REPORT\nµ;cout<<´Year :2011\nµ;cout<<´Number Of Working Graduates:µ<<obj[0].work;cout<<´\nDetails Of Top-Most Scorer\nµ;cout<<´ Name :µ<<obj.[place].name;cout<<´\n Age :µ<<obj[place].age;

cout<<´\n Subject :<<obj[place].subject;cout<<´\n Average Marks:<<obj[place].avgmks<<endl;

cout<<((obj[0].graduates-obj[0].work)*100)/obj[0].graduates<<´% of graduates are

non-working and µ<<(obj[0].devision*100)/obj[0].students<<´5 are first divisioners.µ;

getch();

return 0;}

XII) 

# include <iostreaem.h>#include <conio.h>

char* search(char* arr,charch) {char* pt;int flag=0;pt=arr;while(*ch!=·\0·) 

{if(*ch==ch) {

pt=ch;flag=1;break;}

Page 15: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 15/42

15 

pt++;}if(flag==0) pt=NULL;

returnpt;}

int main() {clrscr();char string[50],ch;cout<<µPlease Enter The Stringµ;cin.getline(string,50);cout<<µPlease Enter The Character To Searchµ;cin>>ch;

char* ptr;ptr=search(string,ch);getch();return 0;}

XIII) # include <iostreaem.h>#include <conio.h>void reverse(char arr[50],int size) {intsrt=-1;for(int end=0,end<size,end++) {if(arr[end]==· ¶ || arr[end]==·.·) {for(int i=end,i>srt,i--) cout<<arr[i];srt=end;}}}

int main() {

chararr[30];cout<<µPlease enter the string:-\nµ;cin.getline(arr,30);reverse(arr);getch();return 0;

Page 16: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 16/42

16 

}

XIV) 

# include <iostreaem.h>#include <conio.h>char* compress(char* ch);{chararr[];int i=0;while(*ch!=·\0·) {if(!ch[0]==· ¶ &&ch[1]==· ¶)) {arr[i]=*ch;i++;}

ch++;}returnarr;}

int main() {chararr[50];cout<<µPlease enter the string:-\nµ;cin.getline(arr,50);char* nwarr=compress(arr);while(*nwarr!=·\0·);{cout<<*nwarr;}getch();return 0;}

XV) #include<fstream.h>#include<conio.h>

structemp{char name[25];intempcode,basicsal;}obj;

void append() 

Page 17: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 17/42

17 

{ofstreamfout;fout.open("emp.txt",ios::binary);charch='y';while(ch=='Y' || ch=='y') 

{cout<< "\nEnter The Name: ";cin.getline(obj.name,25);cout<<"Enter The Employee code: ";cin>>obj.empcode;cout<<"Enter The Salary: ";cin>>obj.basicsal;fout.write((char*)&obj,sizeof(obj));cout<<"Do You Want To Enter A New Record?(y/n)";

cin>>ch;}fout.close();}

void search() {ifstream fin("emp.txt",ios::binary);fin.seekg(0);int code;cout<<"Enter The Employee code: ";cin>>code;while(!fin.eof()) {fin.read((char*)&obj,sizeof(obj));if(obj.empcode==code) {cout<<"\nname: "<<obj.name<<"\nemployee code: "<<obj.empcode<<"\nbasicsalary:"<<obj.basicsal;break;}}}

void display() 

{ifstream fin;fin.open("emp.txt",ios::binary);fin.seekg(0);while(!fin.eof()) {fin.read((char*)&obj,sizeof(obj));cout<<"\nname: "<<obj.name<<"\nemployee code: "<<obj.empcode<<"\nbasicsalary:

Page 18: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 18/42

18 

"<<obj.basicsal;}}

voidcreatenew() 

{}//dontknow how to do the 1st point

int main() {int i;cout<<"Welcome User\n\n";while(i!=5) {cout<<"Enter The Serial Number To Enter The Written Operation\n1.To create anew file and store some records into it.\n\n2.To append some records in the existing

file.\n3.To search and display details of a given student.\n4.To display the contentsof the file.\n5.To exit the program.\n";cin>>i;

switch(i) {case 1:createnew();

break;case 2:append();

break;case 3:search();

break;case 4:display();

break;case 5:break;default:cout<<"invalid entry.program terminated.";}}getch();return 0;}

XVI) #include<fstream.h>#include<conio.h>class STUDENTS{

Page 19: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 19/42

19 

intRollno;char name[25];float Marks;

voidgetdata() {cout<<"Enter the name";cin.getline(name,25);cout<<"Enter the roll no";cin>>Rollno;cout<<"Enter the marks";cin>>Marks;}

intgetrno() {returnRollno;}

voidputdata() {cout<<"\nname:"<<name;cout<<"\nrollno:"<<Rollno;cout<<"\nmarks:"<<Marks;}};

void main() {int x=1;while(x) {

int f=1;STUDENTS s,ch;ofstreamfout;fout.open("STUD.DAT",ios::app);ifstream fin;fin.open("STUD.DAT");

Page 20: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 20/42

20

cout<<"Enter ur choice\n1:Add a record of a student\n2:Search and display recordof a student";cout<<"\n3:Display records of all the students\n4:Delete record of astudent\n5:Exit\n"

cin.get(ch);switch(ch) {case 1:cout<<"Enter the new record:\n"s.getdata();fout.write((char *)&s,sizeofs);fin.close();fout.close();

break;case 2:intrno;cout<<"Enter the roll no of the student whose data u want to see:\n";cin>>rno;while(!fin.eof) {fin.read((char *)&s,sizeofs);if(s.getrno==rno) {s.putdata() f=0;}}if(f) cout<<"no record found";fin.close();fout.close();break;case 3:while (!fin.eof()) 

{fin.read((char *)&s,sizeofs);s.putdata;f=0;}if(f) 

Page 21: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 21/42

21

cout<<"No record to disp";fin.close();fout.close();break;

case 4:ofstream f2;f2.open("temp.dat");intrno;cout<<"Enter the roll no of the student whose data u want to delete:\n";cin>>rno;while(!fin.eof) {fin.read((char *)&s,sizeofs);if(s.getrno!=rno) 

{f2.write((char *)&s,sizeofs);f=0;}}if(f) cout<<"No record found and thus no record deleted"fin.close();fout.close();f2.close();remove("STUD.DAT");rename("temp.dat","STUD.DAT");break;case 5:fin.close();fout.close();x=0;break;default:

cout<<"Invalid input";}}getch();}

XVII) 

Page 22: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 22/42

22 

#include<fstream.h>#include<conio.h>class student{

introll_no;char name[25];char class[4];float marks;char grade;

voidgetdata() {cout<<"Enter the name";

cin.getline(name,25);cout<<"Enter the roll no";

cin>>roll_no;cout<<"Enter the class";cin.getline(class,4);cout<<"Enter the marks";cin>>marks;cout<<"Enter the grade";cin>>grade;}

intgetrno() {returnroll_no;}

voidputdata() {cout<<"\nname:"<<name;

cout<<"\nrollno:"<<roll_no;cout<<"\nclass:"<<class;cout<<"\nmarks:"<<marks;cout<<"\ngrade:"<<grade;}};

Page 23: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 23/42

23 

void main() {studentsn,so;ofstreamfout;

ifstream fin;fin.open("student.dat");fout.open("temp.dat");cout<<"Enter the no of recods u want to enter:";intch,flag=1;cin>>ch;if(!fin) cout<<"No file found named as student";else{

for(int i=0;i<ch;i++) {

cout<<"Enter the new record:";sn.getdata;while(!fin.eof()) {fin.read((char*)&so,sizeof(so));if(sn.getrno<so.getrno) {fout.write((char *)&sn,sizeofsn);fout.write((char *)&so,sizeofso);flag=0;cout<<"New record added successfully";}elsefout.write((char *)&so,sizeofso);}if(flag) {

fout.write((char *)&sn,sizeofsn);cout<<"New record added successfully";}}fin.close();fout.close();

Page 24: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 24/42

24 

remove("student.dat");rename("temp.dat","student.dat");getch();}

}

XVIII) #include<iostream.h>#include<conio.h>void sort() {int n;

intarr[5];cout<<"Enter the size of arr";cin>>n;cout<<"enter the array";for(int i=0;i<n;i++) {cin>>arr[i];}int temp;int j;for(i=0;i<n;i++) for(int k=0;k<n-j-1;k++) {if(arr[k]>arr[k+1]) {temp=arr[k];arr[k]=arr[k+1];arr[k=1]=temp;}}

}

void main() { inti,k;intarr[50];int n ;

Page 25: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 25/42

25 

sort();cout<<"arr in ascorde";for(i=0;i<n;i++) cout<<arr[i]<<" ";

cout<<"array in desc order";for(i=n-1;i>=0;i--) cout<<arr[i]<<" ";getch();}

XIX) #include<iostream.h>

#include<conio.h>struct *node{intdta;node *next;}*ne,*n,*temp,*top;createnew *node(int x) {temp=new node;temp->dta=x;temp->next=NULL;return temp;}void push(node *temp) {if(top==NULL) top=temp;elsetemp->next=top;top=temp;}

int pop(node *top);{if(top==NULL) {cout<<"stack is empty";return -1;

Page 26: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 26/42

26 

}elseintval=top->dta;returnval;

}void display(node * temp) {while(temp!=NULL) {cout<<temp->dta<<endl;temp=temp->next;}}void main() 

{top=NULL;int info;cout<<"Enter the country";cin>>info;n=createnode(info);push(n);display(top);while(top!=Null);{int info=pop(top);if(info==-1) cout<<"underflow";cout<<"The country name popped is"<<info;top=top->next;}}

XX) #include<iostream.h>#include<string.h>#include<conio.h>struct node{

Page 27: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 27/42

27 

char name[15];node *next;}*ptr,*pt;node *createnode(char elem[]) 

{*ptr=new ptr;strcpy(ptr->name , elem);ptr->next=NULL;returnptr;}void insert(node *pt) { front==0;if(front==NULL) 

front=rear=pt;else{rear->next=pt;rear=pt;}}void delete() {if(front==NULL) cout<<"Queue overflow";else{node *temp;temp=front;front=front->next;delete temp;}}

void display(front) {while(ptr!=NULL) {cout<<ptr->name;ptr=ptr->next;}

Page 28: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 28/42

28 

}void main() {char data[20];node *front=NULL;

node *rear=NULL;charch='y';while(ch=='y'||ch=='Y') {cout<<"Enter the name of the cities ";cin.getline(data,20);temp=createnode(data);insert(temp);cout<<"Do u want to enter more values: ";cin>>ch;

}Cout<<"deletion in progress....";delete();cout<<"The updated string is: ";display(front);getch();}

SQL Commands

Page 29: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 29/42

29 

1. Create a table named TEACHER with the following structure.

IdNo number Primary Key

Name char(15) Not Null

Age number

Department char(15) 

Salary number

Sex char(1) 

Soln.

SQL> CREATE TABLE Teacher

( IdNo integer Primary Key,

Name char(15) Not Null

Age number,

Department char(15),

Salary integer,

Sex char(1) 

);

OUTPUT:

One table created

Page 30: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 30/42

30

2. Insert the following records into the table.

IDNo. Name Age  Department Salary Sex 

1001 Jugal 24 Computer 25,000 M

1005 Sandeep 22 Maths 30,000 M

1007 Shivani 34 Computer 32,000 F

1008 Shikha 23 Maths 25,000 F

Soln.

SQL> Insert into Teacher ( IdNo, Name, Age, Department, Salary, Sex) 

VALUES (&IdNo, &Name, &Age, &Department, &Salary, &Sex);

3. Show all information about the teacher of Computer department.

Soln.

SQL>SELECT IdNo, Name, Age, Department, Salary, Sex

FROM Teacher

Page 31: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 31/42

31

WHERE Department=·Computer·;

OUTPUT:

IDNo. Name Age  Department Salary Sex 

1001 Jugal 24 Computer 25,000 M

1007 Shivani 34 Computer 32,000 F

4. List Name, Age and Salary of all female teachers who are in Maths

department.

Soln.

SQL>SELECT Name, Age, Salary

FROM Teacher

WHERE Department=·Maths· AND Sex=·F·;

OUTPUT:

Name Age Salary 

Shikha 23 25,000

Page 32: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 32/42

32 

5. Display Name, Salary and Age for male teachers who get a Salary of more than

25000.

Soln.

SQL>SELECT Name, Age, Salary

FROM Teacher

WHERE Salary>25000 AND Sex=·M·;

OUTPUT:

Name Age Salary 

Sandeep 22 30,000

6. Count the number of teachers with age>23.

Soln.

SQL>SELECT Count(*) 

Page 33: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 33/42

33 

FROM Teacher

WHERE age>23;

OUTPUT:

Count(*) 

2

Page 34: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 34/42

34 

7. Find the number of different departments mentioned in the table.

Soln.

SQL> Select COUNT(distinct department) from TEACHER;

OUTPUT:

Count(*) 

2

8. Find the age of the oldest female teacher.

Soln.

SQL> Select MAX(Age) from TEACHER where Sex="F";

OUTPUT:

MAX(Age) 

34

Page 35: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 35/42

35 

9. Find the average age of male teachers.

Soln.

SQL> Select AVG(Age) from TEACHER where Sex="M";

OUTPUT:

AVG(Age) 

23

10. Find the sum of Salary given to the teachers of computer department.

Soln.

SQL> Select SUM(Salary) 

FROM TEACHER

WHERE Department =·Computer·;

OUTPUT:

Page 36: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 36/42

36 

SUM(Salary) 

75000

Page 37: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 37/42

37 

11. Display Department and the total Salary given to each department.

Soln.

SQL>SELECT Department, SUM(SALARY) 

FROM Teacher

GROUP BY Department;

OUTPUT:

Department SUM(SALARY) 

Computer 57,000

Maths 55,000

12. Find the name, department of all teachers whose name begin with the latter

¶S·;

Soln.

SQL> SELECT Name, Department

FROM Teachers

WHERE Name LIKE ¶S%·;

Page 38: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 38/42

38 

OUTPUT:

Name  Department 

Sandeep Maths

Shivani Computer

Shikha Maths

13. Display Name, Salary and Department of all teachers in descending order of their

Salary.

Soln.

SQL> SELECT Name, Department, Salary

FROM Teachers

ORDER BY Salary DESC;

OUTPUT:

Name  Department Salary 

Shivani Computer 32,000

Sandeep Maths 30,000

Page 39: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 39/42

39 

Jugal Computer 25,000

Shikha Maths 25,000

14. Display Name and Salary of Computer Teachers with a 0.5% hike in their

salary.

Soln.

SQL> SELECT Name, Salary+Salary*0.5 AS ¶New Salary·

FROM Teachers

WHERE Department=·Computer·;

OUTPUT:

Name  New Salary 

Jugal 37,500

Shivani 48,000

Page 40: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 40/42

40

15. Rectify the Name of the Teacher to ¶Juggal· whose IDNo is 1001

Soln. 

SQL> UPDATE Teachers

SET Name=·Juggal·

WHERE IDNo=1001;

OUTPUT:

1 row updated

16. Modify the table Teacher to add a column named no_of_dependent (integer

type) 

Soln. 

SQL> ALTER TABLE Teachers

ADD (no_of_dependent integer);

OUTPUT:

Table Altered

Page 41: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 41/42

41

17. Modify the column named - Name to store 25 characters.

Soln. 

SQL> ALTER TABLE Teachers

MODIFY (Name char(25));

OUTPUT:

Table Altered

18. Delete the record of the Teacher having IDNo as 1007

Soln. 

SQL> DELETE FROM Teachers

WHERE IDNo=1007;

OUTPUT:

1 row deleted

Page 42: CS Assignment(2)

8/3/2019 CS Assignment(2)

http://slidepdf.com/reader/full/cs-assignment2 42/42

19. Write a query to display the system Date.

Soln. 

SQL> SELECT SYSDATE

FROM Dual;

OUTPUT:

06-Nov-01

20. Delete the table Teacher

Soln. 

SQL> DELETE FROM Teachers

SQL> DROP TABLE Teachers

OUTPUT:

Table dropped