Vous êtes sur la page 1sur 42

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";
1

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

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:";

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++; } else if(arr[i]!=0) {c[k]=arr[i]; k++; } } j=0; for (i=k;i<10;i++) {b[i]=c[j];

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];

list[j]=temp; } else if (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) \n To enter the name of the capital and view the country's name, per capita income"; cin>>x; while(ch=='y'||ch=='Y')

{if(x!=1||x!=2) {cout<<"Invalid entry"; exit(0); } else if(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; } } } else if(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)

#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; else if(dis<40) return 1; else return -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; } else if(tot>t2) {t2=tot; top2=S.roll; } } f3.close(); fstream f4("Student.dat",ios::binary);

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();

fstream f2("Student.dat",ios::binary); int x=0; while(x<5) {f2.read((char*)S, sizeof(S)); dis=dist(); if(dis==0) y++; else if(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; }

10

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();

11

}; 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();

12

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 yes void 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);

13

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; }

14

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;

15

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()

16

{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:

17

"<<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 a new 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 contents of 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 {

18

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");

19

cout<<"Enter ur choice\n1:Add a record of a student\n2:Search and display record of a student"; cout<<"\n3:Display records of all the students\n4:Delete record of a student\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)

20

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)

21

#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; } };

22

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"; } else fout.write((char *)&so,sizeofso); } if(flag) { fout.write((char *)&sn,sizeofsn); cout<<"New record added successfully"; } } fin.close(); fout.close();

23

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 ;

24

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; else temp->next=top; top=temp; } int pop(node *top); { if(top==NULL) { cout<<"stack is empty"; return -1;

25

} else intval=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 {

26

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; }

27

} 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
28

1.

Create a table named TEACHER with the following structure. IdNo Name Age Department Salary Sex number char(15) number char(15) number char(1) Primary Key Not Null

Soln. SQL> CREATE TABLE Teacher ( Name Age Department Salary Sex ); IdNo integer char(15) number, char(15), integer, char(1) Primary Key, Not Null

OUTPUT: One table created

29

2.

Insert the following records into the table.

IDNo. 1001 1005 1007 1008

Name Jugal Sandeep Shivani Shikha

Age 24 22 34 23

Department Salary Computer Maths Computer Maths 25,000 30,000 32,000 25,000

Sex M M F 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
30

WHERE Department=Computer;

OUTPUT: IDNo. 1001 1007 Name Jugal Shivani Age 24 34 Department Salary Computer Computer 25,000 32,000 Sex M 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 Shikha Age 23 Salary 25,000

31

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 Sandeep Age Salary 22 30,000

6.

Count the number of teachers with age>23.

Soln. SQL>SELECT Count(*)

32

FROM Teacher WHERE age>23;

OUTPUT: Count(*) 2

33

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

34

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:

35

SUM(Salary) 75000

36

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

Soln. SQL>SELECT Department, SUM(SALARY) FROM Teacher GROUP BY Department;

OUTPUT: Department Computer Maths SUM(SALARY) 57,000 55,000

12. S;

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

Soln. SQL> SELECT Name, Department FROM Teachers WHERE Name LIKE S%;

37

OUTPUT: Name Sandeep Shivani Shikha Department Maths Computer 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 Shivani Sandeep Department Computer Maths Salary 32,000 30,000

38

Jugal Shikha

Computer Maths

25,000 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 Jugal Shivani New Salary 37,500 48,000

39

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. type)

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

Soln. SQL> ALTER TABLE Teachers ADD (no_of_dependent integer);

OUTPUT: Table Altered

40

17. Soln.

Modify the column named - Name to store 25 characters.

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

41

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

42

Vous aimerez peut-être aussi