Sie sind auf Seite 1von 58

1//PROGRAM TO MAKE A HUT USING THE COUT STTEMENT ONLY #include<iostream.h> #include<conio.

h> void main( ) { clrscr( ); cout<<" /\\-----------------------------------------|"<<"\n"; cout<<" / \\ |"<<"\n"; cout<<" / \\ |"<<"\n"; cout<<" / \\ |"<<"\n"; cout<<" / \\ |"<<"\n"; cout<<" / \\ |"<<"\n"; cout<<" / \\------------------------------------|"<<"\n"; cout<<" | | ___ |"<<"\n"; cout<<" | | | | |"<<"\n"; cout<<" | | |___| |"<<"\n"; cout<<" | | |"<<"\n"; cout<<" | __ | |"<<"\n"; cout<<" | | | | |"<<"\n"; cout<<" |__| |_|_________________________|"<<"\n"; getch( ); }

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

Name: Sourabh Vyas

Enrolment Number: 03624302010

2/*MENUE DRIVEN CALCULATOR */ #include<iostream.h> #include<conio.h> #include<process.h> void main( ) { clrscr( ); int ch; int n1,n2,res; do { cout<<"\n"<<"Enter 1st numbers="; cin>>n1; cout<<"\n"<<"Enter 2nd number="; cin>>n2; cout<<"\n"<<"\n"<<"**MENU**"<<"\n"; cout<<"1 ADD"<<"\n"; cout<<"2 SUBSTRACT"<<"\n"; cout<<"3 MULTIPLY"<<"\n"; cout<<"4 DIVISION"<<"\n"; cout<<"5 EXIT"<<"\n"; cout<<"Enter choice:"; cin>>ch; switch(ch) { case 1:res=n1+n2;cout<<"\n"<<"Result="<<res;break; case 2:res=n1-n2;cout<<"\n"<<"Result="<<res;break; case 3:res=n1*n2;cout<<"\n"<<"Result="<<res;break; case 4:res=n1/n2;cout<<"\n"<<"Result="<<res;break; case 5:exit(0); default:cout<<"\n"<<"Invalid choice"; } } while(ch>=1 && ch<=5); getch( ); }
3

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

Name: Sourabh Vyas

Enrolment Number: 03624302010

3 Program.Converting into lower case & upper case. /*LOWER CASE */ #include<iostream.h> #include<conio.h> void main() { char ch; clrscr(); cout<<" ENTER ANY CHARACTER"; cin>>ch; if((ch>='A')&&(ch<='Z')) { ch=ch+32; cout<<" CHARACTER IN LOWER CASE is -"<<ch; } Else { ch=ch-32; cout<<"\n CHARACTER IN UPPER CASE is - "<<ch; } getch(); }

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

Name: Sourabh Vyas

Enrolment Number: 03624302010

4 Program.Displaying the Students grade. /* PROGRAM TO PRINT GRADE*/ #include<iostream.h> #include<conio.h> void main() { float marks,num; clrscr(); cout<<"\n ENTER YOUR MARKS "<<endl; cin>>marks; if(marks>=80) cout<<"GRADE A+\n"<<endl; else if(marks<80 && marks>=70) cout<<"GRADE A\n"; else if(marks<70 && marks>=60) cout<<"GRADE B\n"; else if(marks<60 && marks>=50) cout<<"GRADE C\n"; else cout<<"GRADE D"; getch(); }

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

Name: Sourabh Vyas

Enrolment Number: 03624302010

5 Program.Generating electricity Bill. /*ELECTRICITY BILL*/ #include<iostream.h> #include<conio.h> void main() { int MR=24,UNITS,NUM; clrscr(); cout<<"\tENTER THE UNITS CONSUMED"<<endl; cin>>UNITS; cout<<"\t IF THE UNITS <=100, UNITS <=200, UNITS <=300, U NITS>300 PRESS 1,2,3,4 RESP."<<endl; cin>>NUM; cout<<"BILL GENERATED "; switch(NUM) { case 1: if(UNITS<=100) cout<<UNITS*2.40+MR<<endl; break; case 2: if(UNITS<=200) cout<<100*2.40+(UNITS-100)*3.00+MR<<endl; break; case 3: if(UNITS<=300) cout<<100*2.40+100*3.00+(UNITS-200)*3.5+MR<<endl; break; case 4: if(UNITS>300) cout<<UNITS*4.00+MR<<endl; break; default: cout<<"ENTER THE VALID ENTIRES"; } getch(); }

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

10

Name: Sourabh Vyas

Enrolment Number: 03624302010

6// WAP to print days using switch case #include<iostream.h> #include<conio.h> void main() { int day; cout<<"\n enter day number "; cin>>day; switch(day) { case 1:cout<<"Monday"; break; case 2: cout<<"Tuesday"; break; case 3: cout<<"Wednesday"; break; case 4: cout<<"Thursday"; break; case 5: cout<<"Friday"; break; case 6: cout<<"Saturday"; break; case 7: cout<<"Sunday"; break; default: cout<<"Invalid day nnumber"; } getch(); }

11

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

12

Name: Sourabh Vyas

Enrolment Number: 03624302010

7//wap to generate star series

* *** ***** *******

#include<iostream.h> #include<conio.h> void main() { clrscr(); int i,j,k,n; cout<<"Enter number"; cin>>n; for(i=1;i<=n;i++) { for(j=(2*n-1);j>i;j--) { cout<<" "; } for(k=1;k<=(2*i-1);k++) { cout<<"*"; } cout<<endl; } getch(); }

13

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

14

Name: Sourabh Vyas

Enrolment Number: 03624302010

8// wAP to generate star series * *** ***** ******* #include<iostream.h> #include<conio.h> void main() { clrscr(); int i,j,n; cout<<"Enter number"; cin>>n; for(i=1;i<=n;i++) { for(j=1;j<=(2*i-1);j++) { cout<<"*"; } cout<<"\n"; } getch(); }

15

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

16

Name: Sourabh Vyas

Enrolment Number: 03624302010

9//PROGRAM TO PRINT SPECIFIC PATTERN #include<iostream.h> #include<conio.h> void main( ) { clrscr( ); for(int i=0;i<5;i++) { for(int j=0;j<=i;j++) { cout<<"*"; } cout<<"\n"; } for(int k=0;k<4;k++) { for(int m=4;m>k;m--) { cout<<"*"; } cout<<"\n"; } getch( ); }

17

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

18

Name: Sourabh Vyas

Enrolment Number: 03624302010

10// WAP to generate the series

* *** ***** *** *

#include<iostream.h> #include<conio.h> void main() { clrscr(); int i,j,k,n; cout<<"Enter number"; cin>>n; for(i=1;i<=n;i++) { for(j=(2*n-1);j>i;j--) { cout<<" "; } for(k=1;k<=(2*i-1);k++) { cout<<"*"; } cout<<endl; } for(i=(n-1);i>=1;i--) { for(j=(2*n-1);j>i;j--) { cout<<" "; } for(k=(2*i-1);k>=1;k--) { cout<<"*"; } cout<<endl; }
19

Name: Sourabh Vyas

Enrolment Number: 03624302010

getch(); }

OUTPUT:

20

Name: Sourabh Vyas

Enrolment Number: 03624302010

11// program to swap two integers without using 3rd variable by reference. #include<iostream.h> #include<conio.h> void main() { int a=10,b=20,c; clrscr(); int & swap (int &,int &); cout<<"\n value before swapping are"<<"\n a="<<a<<"\t b="<<b; swap(a,b); cout<<"\n value after swapping ="<<"\n a="<<a<<"\t b="<<b; getch(); } int & swap (int &a,int &b) { a=(a+b); b=(a-b); a=(a-b); }

21

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

22

Name: Sourabh Vyas

Enrolment Number: 03624302010

12// WAP FOR TWO VERSION OF POWER() FUNCTION TO RAISE A NUMBER M TO A POWER N THE FUNCTION TAKES A FLOAT VALUE FOR M & INT FOR N IN ONE VERSION AND AN INT VALUE FOR BOTH M & N IN OTHER VERSION AND RETURNTYPE OF FUNCTION SHOULD BE DOUBLE USE A DEFAULT VALUE OF 2 FOR N TO MAKE FUNCTION TO CALCULATE SQUARE WHEN THIS ARGUMENT IS OMITTED WRITE A MAIN() THAT CALLS BOTH FUNCTION // #include<iostream.h> #include<math.h> #include<conio.h> double power(float m,int n=2) { return pow(m,n); } double power(int m,int n=2) { return pow(m,n); } void main( ) { clrscr( ); int a,p1,p2; float b; double v1,v2; cout<<"\nEnter Interger Value : "; cin>>a; cout<<"\nEnter Real Value : "; cin>>b; cout<<"\nWant To Enter Power : "; char ch; cin>>ch; if(ch=='y'||ch=='Y') { cout<<"\nEnter Power For Interger : ";
23

Name: Sourabh Vyas

Enrolment Number: 03624302010

cin>>p1; cout<<"\nEnter Power For Real : "; cin>>p2; v1=power(a,p1); v2=power(b,p2); } Else { v1=power(a); v2=power(b); } cout<<"\nThe Value Of Interger Is : "<<v1; cout<<"\nThe Value Of Real Is : "<<v2; getch( ); }

24

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

25

Name: Sourabh Vyas

Enrolment Number: 03624302010

13//PROGRAM TO CREATE A CLASS EMPLOYEE// #include<iostream.h> #include<conio.h> class employee { int empno; char ename[20]; float basic,hra,da; float netpay; public: void havedata( ) { cout<<"Enter employee code: "; cin>>empno; cout<<"\n Enter employee name: "; cin>>ename; cout<<"\n Enter basic pay: "; cin>>basic; cout<<"\n Enter hra: "; cin>>hra; cout<<"\n Enter da: "; cin>>da; } float calculate( ) { float t; t=basic+hra+da; return t; } }; void main( ) { employee E1; clrscr( ); E1.havedata( ); float Netpay;
26

Name: Sourabh Vyas

Enrolment Number: 03624302010

Netpay=E1.calculate(); cout<<"\n Netpay is :Rs "<<Netpay; getch(); }

OUTPUT:

27

Name: Sourabh Vyas

Enrolment Number: 03624302010

14// Declare a class student to stimulate result prepration system for 10 students, enter marks in 3 subjects and calculate their average and display grades. #include<iostream.h> #include<conio.h> #include<stdio.h> class student { char name[15]; int rollno; float mark1,mark2,mark3; float average; public: void getdata() { cout<<"\n enter name of student "; gets(name); cout<<"\n enter rollno "; cin>>rollno; } void display() { cout<<"\n enter marks in maths : "; cin>>mark1; cout<<"\n enter mmarks in C++ : "; cin>>mark2; cout<<"\n enter marks in VB : "; cin>>mark3; calculate(); } void calculate() { cout<<"average marks are :"; average=(mark1+mark2+mark3)/3 ; cout<<average; if(average<50) cout<<"\n GRADE F";
28

Name: Sourabh Vyas

Enrolment Number: 03624302010

else if(average>=50&&average<65) cout<<"\n GRADE D"; else if(average>=65&&average<75) cout<<"\n GRADE C"; else if(average>=75&&average<90) cout<<"\n GRADE B"; else if(average>=90) cout<<"\n GRADE A"; } }; void main() { clrscr(); student s1,s2,s3,s4,s5,s6,s7,s8,s9,s10; s1.getdata(); s1.display(); s2.getdata(); s2.display(); s3.getdata(); s3.display(); s4.getdata(); s4.display(); s5.getdata(); s5.display(); s6.getdata(); s6.display(); s7.getdata(); s7.display(); s8.getdata(); s8.display(); s9.getdata(); s9.display(); s10.getdata(); s10.display(); getch(); }
29

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

30

Name: Sourabh Vyas

Enrolment Number: 03624302010

15// create a class time with 3 member fun. Hour,Min,Sec. WAP in which 1 constructor should initialize these data members to 0 nd another should initialize them to some fixed value. A member fun. Should display it in format 11:59:59. The final member fun. Should add 2 objects of type time and one that isnt initiailised. Then it should add 2 initialized values together and saving the result in 3rd time variable. Finally it should display the value of this third variable. #include<iostream.h> #include<conio.h> #inclue<stdio.h> class time { int hrs; int min; int sec; public: time() { hrs=0; min=0; sec=0; } time(int x, int y, int z) { hrs=x; min=y; sec=z; } void puttime() { cout<<hrs<<":"<<min<<":"<<sec<<"\n"; } void sum(time,time); }; void time::sum(time t1, time t2) { sec= t1.sec+t2.sec;
31

Name: Sourabh Vyas

Enrolment Number: 03624302010

min=sec/60; sec=sec%60; min=min+t1.min+t2.min; hrs=min/60; min=min%60; hrs=hrs+t1.hrs+t2.hrs; } void main() { clrscr(); time t1(1,30,40); time t2(3,10,50); time t3; t3.sum(t1,t2); t1.puttime(); t2.puttime(); t3.puttime(); getch(); }

32

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

33

Name: Sourabh Vyas

Enrolment Number: 03624302010

16//PROGRAM TO CREATE A CLASS DISTANCE AND OVERLOAD PRE INCREMENT ++OPERATOR TO INCREMENT DISTANCE AND RETURN THE RESULT TO MAIN( ) #include<iostream.h> #include<conio.h> class distance { int feet,inch; public: distance( ) { feet = 0; inch = 0; } distance(int f,int i) { feet = f; inch = i; } void display( ) { cout<<feet<<"ft"; cout<<" "<<inch<<"inch"; } void operator++( ) { ++feet; ++inch; if(inch>12) { feet=feet+(inch/12); inch=inch%12; } } }; void main( )
34

Name: Sourabh Vyas

Enrolment Number: 03624302010

{ clrscr( ); distance d1; d1 = distance(12,8); cout<<"\n The Distance is : "; d1.display( ); ++d1; cout<<"\n The Distance after pre increment is : "; d1.display( ); getch( ); }

35

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

36

Name: Sourabh Vyas

Enrolment Number: 03624302010

17//PROGRAM TO OVERLOAD BINARY OPERATOR + TO ADD TWO STRINGS// #include<string.h> #include<iostream.h> #include<iostream.h> #include<conio.h> class string { char *p; int length; public: string( ) { length=0; p=0; } string(const char *s) { length=strlen(s); p=new char[length+1]; strcpy(p,s); } friend void show(const string s); friend string operator+(const string &s,const string &t); }; void show(const string s) { cout<<s.p; } string operator+(const string &s,const string &t) { string temp; temp.length=s.length+t.length; temp.p=new char[temp.length+1]; strcpy(temp.p,s.p); strcat(temp.p,t.p);
37

Name: Sourabh Vyas

Enrolment Number: 03624302010

return(temp); } void main( ) { clrscr( ); string s1="New"; string s2="Delhi"; string s3="York"; string t1,t2,t3; t1=s1; t2=s2; t3=s1+s2; cout<<"\nt1= ";show(t1); cout<<"\nt2= ";show(t2); cout<<"\n"; cout<<"\n t3= ";show(t3); cout<<"\n\n"; getch( ); }

38

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

39

Name: Sourabh Vyas

Enrolment Number: 03624302010

18//WAP to create base class car containing following: Protected:carname,price Public: member functions to iput & print out the data Create derived class maruti containing following: Private: registrationfee Public: readdata(),showdata() In main function make object of maruti class and ask the user to fill the data and display it. #include<iostream.h> #include<conio.h> #include<stdio.h> class car { protected: char carname[20]; int price; public: void getdata() { cout<<"\n enter car name "; gets(carname); cout<<"\n enter price of car "; cin>>price; } void display() { cout<<"\n Name of car is :"; cout<<carname; cout<<"\n Price of car is :"; cout<<price; } }; class maruti:public car { private: int registrationfee;
40

Name: Sourabh Vyas

Enrolment Number: 03624302010

public: void readdata() { getdata(); cout<<"\n enter Registration fee "; cin>>registrationfee; } void showdata() { display(); cout<<"\n Registration Fee is :"; cout<<registrationfee; } }; void main() { clrscr(); maruti M; M.readdata(); M.showdata(); getch(); }

41

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

42

Name: Sourabh Vyas

Enrolment Number: 03624302010

19// An organization wishes to create adatabase of its employees. The database is divided into nos of classes. STAFF -> empno,name,basic CLERK -> grade OFFICEER -> desig. Specify all classes nd define fun. To create the database nd retrieve the infor. When required. #include<iostream.h> #include<conio.h> #include<stdio.h> class staff { protected: int empno; char name[25]; float basic; public: void getdata1() { cout<<"\n enter Employee no's "; cin>>empno; cout<<"\n enter name "; gets(name); cout<<"\n Enter basic "; cin>>basic; } void display1() { cout<<"\n Employee no's Is :"; cout<<empno; cout<<"\n Name of Employee is :"; cout<<name; cout<<"\n basic salary of employee is "; cout<<basic; } }; class clerk:public staff
43

Name: Sourabh Vyas

Enrolment Number: 03624302010

{ char grade[20]; public: void getdata() { cout<<"\n enter grade"; gets(grade); } void display() { cout<<"\n Grade of clerk is :"; cout<<grade; } }; class officer : public staff { char desig[20]; public: void getdata() { cout<<"\n enter designation "; gets(desig); } void display() { cout<<"\n Designation Is :"; cout<<desig; } }; void main() { clrscr(); clerk c; officer o; c.getdata1(); c.getdata(); c.display1();
44

Name: Sourabh Vyas

Enrolment Number: 03624302010

c.display(); o.getdata1(); o.getdata(); o.display1(); o.display(); getch(); }

OUTPUT:

45

Name: Sourabh Vyas

Enrolment Number: 03624302010

20// the class Master derives information. From both Account and Admin Classes which in turn derive information from the Class Person. Define all these 4 classes and WAP to create ,update and display the information contained in master objects. #include<iostream.h> #include<conio.h> #include<stdio.h> class person { protected: char name[20]; int code; public: voidgetdata() { cout<<"\n enter the name:"; gets(name); cout<<"\n enter the code:"; cin>>code; } void display() { cout<<"\n the name is :"<<name; cout<<"\n the code is :"<<code; } void update() { getdata(); } }; classaccount:public virtual person { protected: float pay; public: voidgetdata()
46

Name: Sourabh Vyas

Enrolment Number: 03624302010

{ cout<<"\n enter the pay"; cin>>pay; } void display() { cout<<"\n pay is :"<<pay; } void update() { getdata(); } }; classadmin:public virtual person { protected: charexp[15]; public: voidgetdata() { cout<<"\n enter your experience"; gets(exp); } void display() { cout<<"\n experience is of :"<<exp; } void update() { getdata(); } }; classmaster:public account, public admin { public: voidgetdata() {
47

Name: Sourabh Vyas

Enrolment Number: 03624302010

person::getdata(); account::getdata(); admin::getdata(); } void display() { person::display(); account::display(); admin::display(); } void update() { person::update(); account::update(); admin::update(); } }; void main() { clrscr(); master m; m.getdata(); m.display(); m.update(); m.display(); getch(); }

48

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

49

Name: Sourabh Vyas

Enrolment Number: 03624302010

21//Write a program that reads a text file and create another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space. Print both the file. #include<fstream.h> #include<conio.h> #include<string.h> void main( ) { clrscr( ); ifstream fin("text.txt"); fin.open("text.txt",ios::in); ofstream fout("AAA.txt"); fout.open("AAA.txt",ios::out); fin.seekg(0); char c; int flag; while(fin) { fin.get(c); if (!c==' ') { fout.put(c); flag=0; } else if(c==' ' && flag==0) { fout.put(c); flag=1; } } fin.close( ); fout.close( ); cout<<"\t\n\n All text file reading is done......"; getch( ); }
50

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

51

Name: Sourabh Vyas

Enrolment Number: 03624302010

22//A file contain a list of telephone numbers in the following manner: John 25551123 Ahmed 2653123 The names contain only one word. Write a program to read the file and output the list in two columns. Use a class object to store each set of data? #include<fstream.h> #include<string.h> #include<conio.h> class telephone { char name[30]; long tno; public: void display( ); void input( ); }; void telephone::input( ) { cout<<"Enter the name and the telephone number:"; cin>>name>>tno; } void telephone::display( ) { cout<<name<<"\t\t"<<tno<<"\n"; } void main( ) { clrscr( ); telephone t; char name[30]; long tno; fstream file; file.open("tel.dat",ios::out); char ch; do {
52

Name: Sourabh Vyas

Enrolment Number: 03624302010

t.input( ); file.write((char *)&t,sizeof(t)); cout<<"\nPress y"; cin>>ch; } while(ch=='y' || ch=='Y'); file.close( ); file.open("tel.dat",ios::in); file.seekg(0); cout<<"\n\n"<<name<<"\t\t"<<"\n"<<tno<<"\n\n"; while(file.read((char *)&t,sizeof(t))) { t.display( ); } file.close( ); getch( ); }

53

Name: Sourabh Vyas

Enrolment Number: 03624302010

OUTPUT:

54

Name: Sourabh Vyas

Enrolment Number: 03624302010

23WRITE A FUNCTION TEMPLATE FOR FINDING THE MINIMUM VALUE CONTAINED IN AN ARRAY. #include<iostream.h> #include<conio.h> template<class T> T minimum(T a[], int n) { T min=a[0]; for(int i=0;i<n;i++) { if(min>a[i]) { min=a[i]; } } return min; } void main() { clrscr(); intarr[5]; cout<<"\n enter the integer elements of array:"; for(int i=0;i<5;i++) { cin>>arr[i]; } cout<<"\n minimum value is:"; cout<<minimum(arr,5); float arr1[5]; cout<<"\n enter the float elements of array:"; for(i=0;i<5;i++) { cin>>arr1[i]; } cout<<"\n minimum value is:"; cout<<minimum(arr1,5);
55

Name: Sourabh Vyas

Enrolment Number: 03624302010

getch();
}

OUTPUT:

56

Name: Sourabh Vyas

Enrolment Number: 03624302010

27// WAP to generate the series. ABCDEFGFEDCBA ABCDEF FEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A #include<iostream.h> #include<conio.h> void main() { int i,j,k,n; char a; for(i=0;i<1;i++) { for(a=65;a<=71;a++) { cout<<a; } for(k=1;k<i;k++) { cout<<" "; } for(a=70;a>=65;a--) { cout<<a; } cout<<"\n"; } for(i=2;i<=13;i++) { for(a=65;a<=72-i;a++) { cout<<a; }
57

Name: Sourabh Vyas

Enrolment Number: 03624302010

for(k=2;k<2*i-1;k++) { cout<<" "; } for(a=72-i;a>=65;a--) { cout<<a; } cout<<"\n"; } getch(); }

OUTPUT:

58

Name: Sourabh Vyas

Enrolment Number: 03624302010

Das könnte Ihnen auch gefallen