Download as pdf or txt
Download as pdf or txt
You are on page 1of 84

OOPS PROGRAM FILE

Submitted byNaman Garg 3rd SEM ECE B1 Group-3 UE105050

Naman Garg

3rd SEM ECE

UE105050

PROGRAM-1
Q- Program to input a value and display WELL DONE that many times. #include<iostream.h> #include<conio.h> void main() {clrscr(); int n; //no. of times to print

cout<<"Enter number of times to print"; cin>>n; for(int i=1;i<=n;i++) cout<<"WELL DONE\n"; getch(); }

Naman Garg

3rd SEM ECE

UE105050

4 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

5 PROGRAM-2
Q- Program to find larger of 2 nos. #include<iostream.h> #include<conio.h> void main() {clrscr(); int a,b; cout<<"Enter two nos. "; cin>>a>>b; if(a>b) cout<<"First no. "<<a<<" is larger"; else if(a<b) cout<<"Second no. "<<b<<" is larger"; else cout<<"Both nos are equal"; getch(); }

Naman Garg

3rd SEM ECE

UE105050

6 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

7 PROGRAM-3
Q- Program to print the pattern 1 22 333 4444 ............

#include<iostream.h> #include<conio.h> void main() {clrscr(); int n; //Stores no. of rows to print

cout<<"Enter no. of rows "; cin>>n; for(int i=0;i<=n;i++) { for(int j=0;j<i;j++) cout<<i<<"\t"; cout<<"\n"; } getch(); }

Naman Garg

3rd SEM ECE

UE105050

8 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

9 PROGRAM-4
Q- The rates for electricity consumption are For the first 100 units - 60p per unit For next 200 units Beyond 300 units - 80p per unit - 90p per unit

All users are charged min of Rs50. If the total amt is more than Rs300 then an additional surcharge of 15% is added. WAP a program to read names and units consumed and print out the total charges. #include<iostream.h> #include<conio.h> #include<stdio.h> void main() {clrscr(); char name[30]; float units,charge=50.0; cout<<"Enter name of user: "; gets(name); cout<<"Enter units consumed: "; cin>>units; if(units<=100) charge=charge+0.6*units; else if(units<=200) charge=charge+0.8*units; else charge=charge+0.9*units;
Naman Garg 3rd SEM ECE UE105050

10

if(charge>300.0) charge=charge+.15*charge;

cout<<"Name puts(name);

: ";

cout<<"Charges : "<<charge; getch(); }

Naman Garg

3rd SEM ECE

UE105050

11 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

12 PROGRAM-5
Q- WAP to find sum, transpose and product of 2 matrix. #include<iostream.h> #include<conio.h> #include<process.h> class matrix { int a[10][10], b[10][10], c[10][10]; int m,n,p,q; int ch; public: inline void initialise(); void menu(); void choose(); void size1(); void size2(); void input1(); void input2(); void print_c(); void sum(); void transpose(); void product(); }; void matrix::initialise() { } m=n=p=q=0; //Initialise sizes to zero //Prints main menu //Chooses option from main menu //Inputs and checks size of 1st matrix //Inputs and checks size of 2nd matrix //Inputs matrix 1 //Inputs matrix 2 //Prints output matrix //Calc sum of 2 matrix //Calc transpose of matrix //Calc prod of matrices // size of 2 matrix // choice

Naman Garg

3rd SEM ECE

UE105050

13

void matrix::menu() { clrscr(); initialise(); cout<<"\t\tMENU"; cout<<"\n1. Sum of 2 matrix"; cout<<"\n2. Transpose of a matrix"; cout<<"\n3. Product of 2 matrix"; cout<<"\n9. Exit"; cout<<"\n\nEnter choice: "; choose(); } void matrix::choose() { cin>>ch; switch(ch) { case 1: sum(); case 2: transpose(); case 3: product(); case 9: exit(0); default: { cout<<"INVALID CHOICE. TRY AGAIN"; cout<<"\n\nPress any key to continue "; getch(); menu();
Naman Garg 3rd SEM ECE UE105050

14
} } } void matrix::size1() { cout<<"\nEnter size of matrix 1:"; cout<<"\nEnter no. of rows(max 10): "; cin>>m; cout<<"Enter no. of columns(max 10): "; cin>>n; if(m>10||n>10) { cout<<"\nINVALID SIZE"; cout<<"Enter order less than 10x10"; size1(); } } void matrix::size2() { cout<<"\nEnter size of matrix 2:"; cout<<"\nEnter no. of rows(max 10): "; cin>>p; cout<<"Enter no. of columns(max 10): "; cin>>q; if(p>10||q>10) { cout<<"\nINVALID SIZE"; cout<<"Enter order less than 10x10";
Naman Garg 3rd SEM ECE UE105050

15
size2(); } } void matrix::input1() { cout<<"\nEnter matrix 1 (row wise):\n"; for(int i=0;i<m;i++) for(int j=0;j<n;j++) cin>>a[i][j]; } void matrix::input2() { cout<<"\nEnter matrix 2 (row wise):\n"; for(int i=0;i<p;i++) for(int j=0;j<q;j++) cin>>b[i][j]; } void matrix::print_c() { for(int i=0;i<m;i++) { for(int j=0;j<q;j++) cout<<c[i][j]<<"\t"; cout<<"\n"; } cout<<"\n\nPress any key to continue ";
Naman Garg 3rd SEM ECE UE105050

16
getch(); menu(); } void matrix::sum() { size1(); size2(); if(m!=p||n!=q) { cout<<"ORDER OF MATRICES DO NOT MATCH"; cout<<"\nPress any key to continue "; getch(); menu(); } else { input1(); input2(); for(int i=0;i<m;i++) for(int j=0;j<n;j++) c[i][j]=a[i][j]+b[i][j]; cout<<"\n\nSum of matrices is:\n"; print_c(); } } void matrix::transpose() {
Naman Garg 3rd SEM ECE UE105050

17
size1(); input1(); for(int i=0;i<m;i++) for(int j=0;j<n;j++) c[j][i]=a[i][j]; q=m; m=n; cout<<"Transpose of matrices is:\n"; print_c(); } void matrix::product() { size1(); size2(); if(n!=p) { cout<<"\nMatrix cant be multipiled. Try again "; cout<<"\nPress any key to continue"; getch(); menu(); } else { input1(); input2(); for(int i=0;i<m;i++) for(int j=0;j<q;j++)
Naman Garg 3rd SEM ECE UE105050

18
{ c[i][j]=0; for(int k=0;k<n;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } cout<<"\nThe product of matrix is:\n"; print_c(); } }

void main() { clrscr(); matrix a; a.menu(); }

Naman Garg

3rd SEM ECE

UE105050

19 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

20

Naman Garg

3rd SEM ECE

UE105050

21 PROGRAM-6
Q- WAP power() to raise a number m to a power n. The function takes double value for m and int value for n. Use default value of 2 for n. #include<iostream.h> #include<conio.h> double power(double m, int n=2) { double p=1; for(int i=1;i<=n;i++) p=p*m; return p; } void main() {clrscr(); double p,m; int n; cout<<"Enter no. "; cin>>m; cout<<"Enter power "; cin>>n; p=power(m,n); cout<<m<<" ^ "<<n<<" = "<<p; getch(); }
Naman Garg 3rd SEM ECE UE105050

22 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

23 PROGRAM-7
Q- WAP power() to raise a number m to a power n. The function takes double value for m and int value for n. Use default value of 2 for n. Also take int value of m and use function overloading. #include<iostream.h> #include<conio.h> double power(double m, int n=2) { double p=1; for(int i=1;i<=n;i++) p=p*m; return p; } long power(int m, int n=2) { long p=1; for(int i=1;i<=n;i++) p=p*m; return p; } void main() {clrscr(); double p,m; int n;

Naman Garg

3rd SEM ECE

UE105050

24

cout<<"Enter no. "; cin>>m; cout<<"Enter power "; cin>>n; p=power(m,n); cout<<m<<" ^ "<<n<<" = "<<p; getch(); }

Naman Garg

3rd SEM ECE

UE105050

25 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

26 PROGRAM-8
Q- Create two classes DM and DB storing distances. DM stores dist in metre and cm while DB in feet and inches. WAP to read the values of class objects and add one obj of DM with other of DB. Use friend function to carry out this task. Take object storing result as DM object. #include<iostream.h> #include<conio.h> class DB; class DM { float metre,cm; public: void input() { } friend DM convert(DM,DB); void disp() { } }; class DB { float inch,feet; public:
Naman Garg 3rd SEM ECE UE105050

cout<<"Enter length in metre and cm :"; cin>>metre>>cm;

cout<<"Metre : "<<metre; cout<<"\nCentimetre : "<<cm;

27
void input() { } friend DM convert(DM m,DB f); }; DM convert(DM m,DB f) { DM conv; float total_inch=12*f.feet+f.inch; float cm=m.metre*100+m.cm; float total_cm=cm+(2.54*total_inch); conv.metre= (int) total_cm/100; conv.cm=(total_cm/100-conv.metre)*100; return conv; } void main() { clrscr(); DM m,conv; DB f; m.input(); f.input(); cout<<"Enter length in feets and inches :"; cin>>feet>>inch;

Naman Garg

3rd SEM ECE

UE105050

28
conv=convert(m,f); conv.disp(); getch(); }

Naman Garg

3rd SEM ECE

UE105050

29 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

30 PROGRAM-9
Q- Define and implement a class to store bank accounts. Include following data membersname of depositor account no. type of account balance member functionsassign initial values deposit an amount withdraw amount display name and balance #include<iostream.h> #include<conio.h> #include<stdio.h> #include<process.h> class account { char name[30]; long acc_no; char type; float balance; public: void menu(); void choice(); void assign(); void deposit(); void withdraw(); //s-saving, c-current, d-dormant

Naman Garg

3rd SEM ECE

UE105050

31
void disp(); }; void account::menu() { clrscr(); cout<<"\n\t\tMENU"; cout<<"\n1. Assign values"; cout<<"\n2. Deposit"; cout<<"\n3. Withdraw"; cout<<"\n4. Display status"; cout<<"\n9. Exit"; cout<<"\n\nEnter a choice: "; choice(); } void account::choice() { int ch; cin>>ch; A: if(ch==1) assign(); else if(ch==2) deposit(); else if(ch==3) withdraw(); else if(ch==4) disp(); else if(ch==9) exit(0);
Naman Garg 3rd SEM ECE UE105050

32
else { cout<<"INVALID CHOICE. ENTER AGAIN"; getch(); clrscr(); menu(); cin>>ch; goto A; } } void account::assign() { cout<<"Enter name "; gets(name); cout<<"Enter account no.: "; cin>>acc_no; cout<<"Enter account type(S/C/D) "; cin>>type; while((type!='s')||(type!='S')||(type!='c')||(type!='C')||(type!='d')||(typ e!='D')) { cout<<"INVALID OPTION. ENTER AGAIN"; cin>>type; } cout<<"Enter initial amount: "; cin>>balance; menu(); } void account::deposit() { float amt;
3rd SEM ECE UE105050

Naman Garg

33
cout<<"Enter amount to deposit: "; cin>>amt; balance=balance+amt; cout<<"\nNew balance: "<<balance; getch(); menu(); } void account::withdraw() { float amt; B: cout<<"\nEnter amount to withdraw or 0 to exit: "; cin>>amt; if(amt==0) menu(); if(amt>=balance) { } else { balance=balance-amt; cout<<"SUCCESSFUL"; cout<<"\nCurrent balance is Rs. "<<balance; menu(); } } void account::disp() { cout<<"Name: ";
3rd SEM ECE UE105050

cout<<"SUFFICIENT BALANCE NOT AVAILABLE. TRY AGAIN"; goto B;

Naman Garg

34
puts(name); cout<<"Account no. "<<acc_no; cout<<"\nAccount type: "<<type; cout<<"\nBalance: "<<balance; getch(); menu(); }

void main() { clrscr(); account a; a.menu(); }

Naman Garg

3rd SEM ECE

UE105050

35 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

36

Naman Garg

3rd SEM ECE

UE105050

37

Naman Garg

3rd SEM ECE

UE105050

38 PROGRAM-10
Q- Create a class string. Use overloaded operator == to compare two strings. #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> class string { char str[30]; public: string operator==(string); void getstr(); void disp(); }; void string::getstr() { } void string::disp() { } string string::operator==(string s1) { cout<<"String is: "; puts(str); cout<<"Enter string: "; gets(str);

Naman Garg

3rd SEM ECE

UE105050

39
if(strcmp(str,s1.str)==0) cout<<"Strings entered are equal"; else if(strcmp(str,s1.str)<0) cout<<"First strings is smaller"; else cout<<"Second string is smaller"; } void main() { clrscr(); string s1, s2; s1.getstr(); s2.getstr(); s1==s2; getch(); }

Naman Garg

3rd SEM ECE

UE105050

40 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

41 PROGRAM-11
Q- Create a class FLOAT containing one data member. Overload all four arithematic operators so that they operate on objects of FLOAT.

#include<iostream.h> #include<conio.h> class FLOAT { float f; public: void get() { } void disp() { } FLOAT operator+(FLOAT); FLOAT operator-(FLOAT); FLOAT operator*(FLOAT); FLOAT operator/(FLOAT); }; FLOAT FLOAT::operator+(FLOAT f1) { FLOAT f3; f3.f=f+f1.f;
Naman Garg 3rd SEM ECE UE105050

cout<<"Enter value: "; cin>>f;

cout<<" value is "<<f;

42
return f3; } FLOAT FLOAT::operator-(FLOAT f1) { FLOAT f3; f3.f=f-f1.f; return f3; } FLOAT FLOAT::operator*(FLOAT f1) { FLOAT f3; f3.f=f*f1.f; return f3; } FLOAT FLOAT::operator/(FLOAT f1) { FLOAT f3; f3.f=f/f1.f; return f3; } void main() { clrscr(); FLOAT f1,f2,f3; f1.get();
Naman Garg 3rd SEM ECE UE105050

43
f2.get(); f3=f1+f2; cout<<"\nsum "; f3.disp(); f3=f1-f2; cout<<"\ndiffrence "; f3.disp(); f3=f1*f2; cout<<"\nproduct "; f3.disp(); f3=f1/f2; cout<<"\ndivision "; f3.disp(); getch(); }

Naman Garg

3rd SEM ECE

UE105050

44 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

45 PROGRAM-12
Q- Create a base class 'shape'. Use this class to store two double type values that could be used to compute the area of figures. Derive two specific classes rectangle and triangle from base class shape. Add to the base class, a member function get_data() to initialise base class data member and another function disp_area() to display and compute area of figures. Make disp_area() as virtual function and redefine in derived class to suit their requirements. WAP to accept dimensions interactively and display the area. Treat two inputs as length and breadth for rectangle and base and height for triangle. #include<iostream.h> #include<conio.h> #include<process.h> void choose(); class shape { protected: double a,b; double ar; public: void get_data() { } virtual void disp_area() { } cout<<"\narea is "<<ar; cout<<"Enter two dimensions "; cin>>a>>b; //two sides

Naman Garg

3rd SEM ECE

UE105050

46
}; class triangle : public shape { public: void disp_area() { get_data(); ar=a*b/2; cout<<"area of triangle is "<<ar; getch(); choose(); } }; class rectangle : public shape { public: void disp_area() { get_data(); ar=a*b; cout<<"area of rectangle is "<<ar; getch(); choose(); } }; void menu() { clrscr(); cout<<"\tMENU"; cout<<"\nCalculate area of"; cout<<"\n1. Rectangle"; cout<<"\n2. Triangle";
Naman Garg 3rd SEM ECE UE105050

47
cout<<"\n9. Exit"; cout<<"\n\nEnter choice "; } void choose() { menu(); int ch; cin>>ch; switch (ch) { case 1: { } case 2: { } case 9: exit(0); default: { cout<<"INVALID CHOICE"; cout<<"\nEnter again"; cout<<"\nPress any key to continue"; getch(); choose(); } } } triangle t; t.disp_area(); rectangle r; r.disp_area();

Naman Garg

3rd SEM ECE

UE105050

48

void main() { clrscr(); choose(); getch(); }

Naman Garg

3rd SEM ECE

UE105050

49 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

50

Naman Garg

3rd SEM ECE

UE105050

51 PROGRAM-13
Q- Create a base class 'shape'. Use this class to store two double type values that could be used to compute the area of figures. Derive two specific classes rectangle and triangle from base class shape. Add to the base class, a member function get_data() to initialise base class data member and another function disp_area() to display and compute area of figures. Make disp_area() as virtual function and redefine in derived class to suit their requirements. WAP to accept dimensions interactively and display the area. Treat two inputs as length and breadth for rectangle and base and height for triangle Extend the program to disp area of circle. Create another derived class named circle computing area of circle. #include<iostream.h> #include<conio.h> #include<process.h> void choose(); class shape { protected: double a,b; double ar; public: void get_data(double d1,double d2=0.0) { } virtual void disp_area() {
Naman Garg

//two sides

a=d1; b=d2;

cout<<"\narea is "<<ar; }
3rd SEM ECE UE105050

52
}; class triangle : public shape { public: void disp_area() { ar=a*b/2; cout<<"area of triangle is "<<ar; getch(); choose(); } }; class rectangle : public shape { public: void disp_area() { ar=a*b; cout<<"area of rectangle is "<<ar; getch(); choose(); } }; class circle : public shape { public: void disp_area() { ar=a*a*3.14; cout<<"area of circle is "<<ar; getch(); choose(); } };
Naman Garg 3rd SEM ECE UE105050

53
void menu() { clrscr(); cout<<"\tMENU"; cout<<"\nCalculate area of"; cout<<"\n1. Rectangle"; cout<<"\n2. Triangle"; cout<<"\n3. Circle"; cout<<"\n9. Exit"; cout<<"\n\nEnter choice "; } void choose() { menu(); int ch; cin>>ch; switch (ch) { case 1: { double d1,d2; rectangle r; cout<<"Enter dimensions of rectangle"; cin>>d1>>d2; r.get_data(d1,d2); r.disp_area(); } case 2: { double d1,d2; triangle t; cout<<"Enter base and height of triangle"; cin>>d1>>d2;
Naman Garg 3rd SEM ECE UE105050

54
t.get_data(d1,d2); t.disp_area(); } case 3: { double d1; circle c; cout<<"Enter radius of circle"; cin>>d1; c.get_data(d1); c.disp_area(); } case 9: exit(0); default: { cout<<"INVALID CHOICE"; cout<<"\nEnter again"; cout<<"\nPress any key to continue"; getch(); choose(); } } } void main() { clrscr(); choose(); getch(); }

Naman Garg

3rd SEM ECE

UE105050

55 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

56 PROGRAM-14
Q- WAP to read list containing item name, code, and cost and produce a column wise output. The name is left justified, code and cost are right justified cost having precision of two digits. #include<iostream.h> #include<iomanip.h> #include<conio.h> #include<stdio.h> class item { int code; char name[15]; float cost; public: void get_data(); void display(); }; void item::get_data() { cout<<"Enter item code "; cin>>code; cout<<"Enter item name "; gets(name); cout<<"Enter item cost "; cin>>cost; } void item::display() {
Naman Garg 3rd SEM ECE UE105050

57
cout<<setw(20)<<setiosflags(ios::left)<<name <<setw(7)<<setiosflags(ios::right)<<code <<setw(14)<<setiosflags(ios::right) <<setiosflags(ios::showpoint)<<setprecision(2)<<cost <<endl; } void main() { clrscr(); item a[3]; cout<<"Enter details...\n"; for(int i=0;i<3;i++) { } cout<<"\nThe details are as follows.\n\n"; cout<<setw(20)<<setiosflags(ios::left)<<"NAME" <<setw(7)<<setiosflags(ios::right)<<"CODE" <<setw(14)<<setiosflags(ios::right)<<"COST" <<endl; cout<<setw(50)<<setfill('-')<<" "<<setfill(' ')<<endl; for(i=0;i<3;i++) a[i].display(); cout<<setw(50)<<setfill('-')<<" "<<endl; getch(); }
Naman Garg 3rd SEM ECE UE105050

cout<<"\nItem "<<i+1<<endl; a[i].get_data();

58 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

59 PROGRAM-15
Q- WAP to read list containing item name, code, and cost and produce a column wise output. The name is left justified, code and cost are right justified cost having precision of two digits. Fill unused spaces with hyphen. #include<iostream.h> #include<iomanip.h> #include<conio.h> #include<stdio.h> class item { int code; char name[15]; float cost; public: void get_data(); void display(); }; void item::get_data() { cout<<"Enter item code "; cin>>code; cout<<"Enter item name "; gets(name); cout<<"Enter item cost "; cin>>cost; } void item::display() {
Naman Garg 3rd SEM ECE UE105050

60
cout<<setw(20)<<setiosflags(ios::left)<<name <<setw(7)<<setiosflags(ios::right)<<code <<setw(14)<<setiosflags(ios::right) <<setiosflags(ios::showpoint)<<setprecision(2)<<cost <<endl; } void main() { clrscr(); item a[3]; cout<<"Enter details...\n"; for(int i=0;i<3;i++) { } cout<<"\nThe details are as follows.\n\n"; cout<<setw(20)<<setiosflags(ios::left)<<"NAME" <<setw(7)<<setiosflags(ios::right)<<"CODE" <<setw(14)<<setiosflags(ios::right)<<"COST" <<endl; cout<<setw(50)<<setfill('-')<<" "<<endl; for(i=0;i<3;i++) a[i].display(); cout<<setw(50)<<setfill('-')<<" "<<endl; getch(); }
Naman Garg 3rd SEM ECE UE105050

cout<<"\nItem "<<i+1<<endl; a[i].get_data();

61 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

62 PROGRAM-16
Q- A file phone.txt contains list of names and phone nos. WAP to read file and output the list in two columns. The names should be left justified and phone nos write justified. #include<iostream.h> #include<conio.h> #include<fstream.h> #include<iomanip.h> class list { char name[20]; long phone; public: void get(); void put(); void read_file(); void write_file(); }; void list::get() { cout<<"\nEnter name: "; cin>>name; cout<<"Enter phone no: "; cin>>phone; write_file(); } void list::put() { read_file();
3rd SEM ECE UE105050

Naman Garg

63
cout<<endl <<setw(15)<<setiosflags(ios::left)<<name <<setw(10)<<setiosflags(ios::right)<<phone; } void list::write_file() { fstream f; f.open("phone.txt",ios::out,ios::app); f.write((char*)&this, sizeof(list)); f.close(); } void list::read_file() { fstream f; f.open("phone.txt",ios::in); f.read((char*)&this, sizeof(list)); f.close(); } void main() { clrscr(); list l[3]; cout<<"Enter details.."; for(int i=0;i<3;i++) { } cout<<"\nMember "<<i+1; l[i].get();

Naman Garg

3rd SEM ECE

UE105050

64
cout<<"\n\nDetails are\n\n"; cout<<setw(15)<<setiosflags(ios::left)<<"NAME" <<setw(10)<<setiosflags(ios::right)<<"PHONE" <<endl; for(i=0;i<3;i++) l[i].put();

getch(); }

Naman Garg

3rd SEM ECE

UE105050

65 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

66 PROGRAM-17
Q- WAP to count no. of words in a string. #include<iostream.h> #include<conio.h> #include<stdio.h> int words(char str[100]) { int word=0; for(int i=0;str[i]!='\0';i++) if(str[i]==' '&&str[i-1]!=' '&&str[i+1]!='\0') word++; return (word+1); } void main() {clrscr(); char str[100]; int word; cout<<"Enter a string: " ; gets(str); cout<<"The given string contains "<<words(str)<<" words in it"; getch();}

Naman Garg

3rd SEM ECE

UE105050

67 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

68 PROGRAM-18
Q- WAP to check if string is palindrome or not. #include<iostream.h> #include<conio.h> #include<stdio.h> int palindrome(char str[30]) { int len,i,j; for(len=0;str[len]!='\0';len++); for(i=0,j=len-1;i<=len/2;i++,j--) if(str[i]!=str[j]) return 0; return 1; } void main() { clrscr(); char str[30]; int res=0; cout<<"Enter string: "; gets(str); res=palindrome(str);

Naman Garg

3rd SEM ECE

UE105050

69

if(res==1) cout<<"String entered is palindrome"; else cout<<"string entered is not a palindrome"; getch(); }

Naman Garg

3rd SEM ECE

UE105050

70 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

71 PROGRAM-19
Q- Write a program to demonstrate use of constructors. #include<iostream.h> #include<string.h> #include<conio.h> class String { int length; public: char *name; String() { name=new char[1]; } String(char *a) { length=strlen(a); name=new char[length+1]; strcpy(name,a); } void concatenate(char *a, char*b) { length=strlen(a)+strlen(b); name=new char[length+1]; strcpy(name,a); strcat(name,b); }

Naman Garg

3rd SEM ECE

UE105050

72
void display() { cout<<"The string is: "<<name<<endl; } }; void main() { clrscr(); String s2("Well done."); s2.display(); String s3; s3.concatenate("New"," Delhi"); s3.display(); getch(); }

Naman Garg

3rd SEM ECE

UE105050

73 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

74 PROGRAM-20
Q- WAP to find max and min value in array. #include<iostream.h> #include<conio.h> void main() {clrscr(); int i,n,max,min; int list[100]; max=min=list[0]; cout<<"How many numbers: "; cin>>n; for(i=0;i<n;i++) { cout<<"\nEnter "<<i+1<<" no. : "; cin>>list[i]; if(list[i]>max) max=list[i]; else if(list[i]<min) min=list[i]; } cout<<"\nMaximum : "<<max<<"\nMinimum : "<<min; getch(); }

Naman Garg

3rd SEM ECE

UE105050

75 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

76 PROGRAM-21
Q- Write a program to print Fibonacci series #include<iostream.h> #include<conio.h> void main() { clrscr(); unsigned long ctr,c,a,b,n; cout<<"Upto how many numbers u want the Fibonacci series"; cin>>n; a=0; b=1; cout<<"\Fibonacci series :"; cout<<"\n"<<a<<"\t "<<b; for(ctr=2;ctr<n;ctr++) { c=a+b; cout<<"\t "<<c; a=b; b=c; } getch(); }

Naman Garg

3rd SEM ECE

UE105050

77 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

78 PROGRAM-22
Q- WAP to toggle case of characters in a string. #include<iostream.h> #include<conio.h> #include<stdio.h> void strtoggle(char str[]) { int i; for(i=0;str[i]!='\0';i++) { if(str[i]>='a' && str[i]<='z') str[i]-=32; else if(str[i]>='A' && str[i]<='Z') str[i]+=32; } cout<<"\nToggled string: "<<str; } void main() {clrscr(); char str[50]; cout<<"\nEnter a string: "; gets(str); strtoggle(str); getch(); }

Naman Garg

3rd SEM ECE

UE105050

79 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

80 PROGRAM-23
Q- WAP to sort the cities entered by the user. #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { clrscr(); char arr[15][30], temp[30]; int no; cout<<"Eenter no of cities: "; cin>>no; for(int i=0;i<no;i++) gets(arr[i]); for(i=0;i<no;i++) for(int j=i;j<no;j++) { if(strcmpi(arr[i],arr[j])>0) { strcpy(temp,arr[i]); strcpy(arr[i],arr[j]); strcpy(arr[j],temp);

Naman Garg

3rd SEM ECE

UE105050

81
} } cout<<\nSorted list\n\n; for(i=0;i<no;i++) puts(arr[i]); getch(); }

Naman Garg

3rd SEM ECE

UE105050

82 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

83 PROGRAM-24
Q- Program to check for password entered by user. #include<iostream.h> #include<conio.h> void main() { clrscr(); cout<<"Enter Password :"; char str[]="test"; char ch; int fl=0; for(int i=0;i<4;i++) { ch=getch(); cout<<'*'; if(str[i]!=ch) fl=1; } if(fl==0) cout<<"\nAccepted"; else cout<<"\nRefused"; getch(); }

Naman Garg

3rd SEM ECE

UE105050

84 OUTPUT

Naman Garg

3rd SEM ECE

UE105050

You might also like