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

COMPUTER SCIENCE AND ENGINNERING

SOME RANDOM LAB ASSIGNMENTS

MIGHT HELP YOU

1. Write a C program to take Elements from a 3x3 matrix from a user and print I) Sum of the Diagonal matrix II) Sum of the lower triangle Elements III) Sum of the upper triangle Elements //PROGRAM TO SUM DIAGONAL ELEMENTS, UPPER TRIAGLE AND LOWER TRIANGLE ELEMENTS OF //A MATRIX #include<stdio.h> int matrixDiagonal(int a[10][10],int); int lowerTriangle(int a[10][10],int); int upperTriangle(int a[10][10],int); int main()

{ int a[10][10]; int choice; int size; int i,j; do { printf("\nMATRIX OPERATION PROGRAM"); printf("\n1.Enter the matrix "); printf("\n2.Sum of diagonal elements "); printf("\n3.Sum of Upper triangle elements "); printf("\n4.Sum of Lower Triangle elements "); printf("\n0.Exit\nChoice: "); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the size of the matrix (maximum 10): "); scanf("%d",&size); printf("\nEnter the Matrix: \n"); for(i=0;i<size;i++) { printf("Row %d : ",i+1); for(j=0;j<size;j++) { scanf("%d",&a[i][j]); } } break; case 2: printf("\nSum of Diagonal elements : %d\n",matrixDiagonal(a,size)); break; case 3: printf("\nSum of Lower Triangle elements : %d\n",lowerTriangle(a,size)); break; case 4: printf("\nSum if Upper Triangle elements : %d\n",upperTriangle(a,size)); break; case 0: printf("\nTHANK YOU\n"); break; default:printf("\nInvalid Choice, please re-enter\n\n"); break; } }while(choice); return 0; }

int matrixDiagonal(int a[10][10],int size) { int i; int sum=0; for(i=0;i<size;i++) { sum+=a[i][i]; } return sum; } int lowerTriangle(int a[10][10],int size) { int i,j; int sum=0; for(i=1;i<size;i++) { for(j=0;j<i;j++) { sum+=a[i][j]; } } return sum; } int upperTriangle(int a[10][10],int size) { int i,j; int sum=0; for(i=0;i<size;i++) { for(j=i+1;j<size;j++) { sum+=a[i][j]; } } return sum; } OUTPUT: MATRIX OPERATION PROGRAM 1.Enter the matrix 2.Sum of diagonal elements 3.Sum of Upper triangle elements 4.Sum of Lower Triangle elements 0.Exit

Choice: 1 Enter the size of the matrix (maximum 10): 3 Enter the Matrix: Row 1 : 1 2 3 Row 2 : 4 5 6 Row 3 : 7 8 9 MATRIX OPERATION PROGRAM 1.Enter the matrix 2.Sum of diagonal elements 3.Sum of Upper triangle elements 4.Sum of Lower Triangle elements 0.Exit Choice: 2 Sum of Diagonal elements : 15 MATRIX OPERATION PROGRAM 1.Enter the matrix 2.Sum of diagonal elements 3.Sum of Upper triangle elements 4.Sum of Lower Triangle elements 0.Exit Choice: 3 Sum of Lower Triangle elements : 19 MATRIX OPERATION PROGRAM 1.Enter the matrix 2.Sum of diagonal elements 3.Sum of Upper triangle elements 4.Sum of Lower Triangle elements 0.Exit Choice: 4 Sum if Upper Triangle elements : 11 MATRIX OPERATION PROGRAM 1.Enter the matrix 2.Sum of diagonal elements 3.Sum of Upper triangle elements 4.Sum of Lower Triangle elements 0.Exit Choice: 0 THANK YOU

2. Write a C program to take a number from user and tell whether it is a palindrome or not. //PROGRAMM TO CHECK WHETHER A NUMBER IS PALINDROME OR NOT #include<stdio.h> void palindrome(int); int main() { int number;

int choice; do { printf("\nPALINDROME CHECKING PROGRAM"); printf("\n1.Enter a Number\n0.Exit \nchoice: "); scanf("%d",&choice); switch(choice) { case 1: printf("\nEnter the number: "); scanf("%d",&number); palindrome(number); break; case 0: printf("\nTHANK YOU\n"); break; default:printf("\nInvalid Choice, please re-enter\n\n"); break; } }while(choice); return 0; } void palindrome(int x) { int newNum=0,counter=0; int temp=x,powTen=1; int i; while(temp) { temp=temp/10; counter++; }

temp=x; for(i=1;i<counter;i++) { powTen*=10; }

while(temp) { newNum+=((temp%10)*powTen); temp=temp/10; powTen/=10; } if(x==newNum) { printf("\nThis Number is a palindrome\n"); } else { printf("\nThis number is not a Palindrome\n"); } }

OUTPUT: PALINDROME CHECKING PROGRAM 1.Enter a Number 0.Exit choice: 1 Enter the number: 12421 This Number is a palindrome

PALINDROME CHECKING PROGRAM 1.Enter a Number 0.Exit choice: 1 Enter the number: 21332 This number is not a Palindrome PALINDROME CHECKING PROGRAM 1.Enter a Number 0.Exit choice: 0 THANK YOU

3. Write a program in C to take a string and print whether it is a Palindrome or not. //PROGRAM TO CHECK WHETHER A STRING IS PALINDROME OR NOT #include<stdio.h> #include<string.h> void palindrome();

int main() { int choice; do { printf("\nPALINDROME CHECKING PROGRAM"); printf("\n1.Enter a String\n0.Exit \nchoice: "); scanf("%d",&choice); switch(choice) { case 1: palindrome(); break; case 0: printf("\nTHANK YOU\n"); break; default:printf("\nInvalid Choice, please re-enter\n\n"); break; } }while(choice); return 0; } void palindrome() { int counter=0; int i; char a[10]="\0",b[10]="\0"; printf("\nEnter the string: "); scanf("%s",a); counter=strlen(a); for(i=0;i<counter;i++) { b[i]=a[counter-i-1]; } if(!strcmp(a,b)) { printf("\nIt is a palindrome\n"); }

else { printf("\nIt is not a palindrome\n"); } }

OUTPUT: PALINDROME CHECKING PROGRAM 1.Enter a String 0.Exit choice: 1 Enter the string: malayalam It is a palindrome PALINDROME CHECKING PROGRAM 1.Enter a String 0.Exit choice: 1 Enter the string: computer It is not a palindrome PALINDROME CHECKING PROGRAM 1.Enter a String 0.Exit choice: 0 THANK YOU

4. Write a program in C using structure and union to store the information of an employee of an organisation The details of the employees should be in the format of: Name Age

Grade if Grade =1 then ask for Card No and hobby if Grade = 2 then ask for Vehicle No and Distance from the office //A PROGRAM TO STORE INFORMATION ABOUT AN EMPLOYEE //USING STRUCTURE AND UNION #include<stdio.h> struct employee { char name[10]; int age; int grade; union grade0 { struct grade1 { int cardNo; char hobby[10]; }x; struct grade2 { int vehicleNo; float distOffice; }y; }z; }; typedef struct employee employee;

int main() { employee emp[10]; int choice; int i,num; do { printf("\n\nREAD AND WRITE FILES\n"); printf("\n1.Enter information\n2.Read information"); printf("\n0Exit\nChoice:"); scanf("%d",&choice); switch(choice) {

case 1: printf("\nEnter the number of employee: "); scanf("%d",&num); for(i=0;i<num;i++) { printf("\nEmployee %d:\n",i+1); printf("\nName: "); scanf("%s",emp[i].name); printf("Age: "); scanf("%d",&emp[i].age); printf("Grade: "); scanf("%d",&emp[i].grade); if(emp[i].grade==1) { printf("\nCard No: "); scanf("%d",&emp[i].z.x.cardNo); printf("Hobby: "); scanf("%s",emp[i].z.x.hobby); } else if(emp[i].grade==2) { printf("\nVehicle No: "); scanf("%d",&emp[i].z.y.vehicleNo); printf("Distance from office: "); scanf("%f",&emp[i].z.y.distOffice); } } break; case 2: for(i=0;i<num;i++) { printf("\n\nEmployee %d:\n",i+1); printf("\nName: %s",emp[i].name); printf("\nAge: %d",emp[i].age); printf("\nGrade: %d",emp[i].grade); if(emp[i].grade==1) { printf("\nCard No: %d",emp[i].z.x.cardNo); printf("\nHobby: %s",emp[i].z.x.hobby); } else if(emp[i].grade==2) { printf("\nVehicle No: %d",emp[i].z.y.vehicleNo); printf("\nDistance from office: %f",emp[i].z.y.distOffice); } } break;

case 0: printf("\nTHANK YOU\n");

break; default:printf("\nInvalid choice"); break; } }while(choice); return 0; }

OUTPUT: READ AND WRITE FILES

1.Enter information 2.Read information 0.Exit Choice:1 Enter the number of employee: 3 Employee 1: Name: John Age: 29 Grade: 1 Card No: 12332 Hobby: Philately Employee 2: Name: Sue Age: 26 Grade: 2 Vehicle No: 18293 Distance from office: 12.00 Employee 3: Name: Amy Age: 34 Grade: 2 Vehicle No: 12332 Distance from office: 1.05 READ AND WRITE FILES 1.Enter information 2.Read information 0.Exit Choice:2 Employee 1: Name: John Age: 29 Grade: 1 Card No: 12332 Hobby: Philately Employee 2:

Name: Sue Age: 26 Grade: 2 Vehicle No: 18293 Distance from office: 12.000000 Employee 3: Name: Amy Age: 34 Grade: 2 Vehicle No: 12332 Distance from office: 1.050000 READ AND WRITE FILES 1.Enter information 2.Read information 0.Exit Choice:0 THANK YOU

5. Write a program in C to either take an integer and or two characters from the user If the user enters an integer then print 2 characters whose ascii value is obtained

by dividing the binary equivalent of the number in 8 bits and 8 bits. Else if the user enters 2 characters then print the integer formed by binary equivalent combination of ascii value of 2 characters. #include<stdio.h> #include<math.h> int main() { int choice,integer; int d,k,c=0,a[16]; int binEq1=0,binEq2=0; int g[16],e,p,v,x,u=0,n1=0,n2=0,i; char ch1,ch2; do { printf("\nWELCOME"); printf("\n1.Enter Integers\n2.Enter 2 characters\n0.Exit\nChoice: "); scanf("%d",&choice); if(choice!=1&&choice!=2&&choice) { printf("Invalid input."); } switch(choice) { case 1: printf("Enter the integer:"); scanf("%d",&integer); k=integer; d=integer; while(k>0) { k=k/2; c++; } for(i=0;i<=(c-1);i++) { a[i]=d%2; d=d/2; } for(i=c;i<16;i++) { a[i]=0; } for(i=0;i<8;i++) { binEq1=binEq1+(a[i]*pow(2,i)); }

printf("%c\n",binEq1); for(i=8;i<16;i++) { binEq2=binEq2+(a[i]*pow(2,(i-8))); } printf("%c\n",binEq2); break; case 2: printf("\nEnter Character 1: "); scanf("%c",&ch1); printf("\nEnter Character 2: "); scanf("%c",&ch2); e=ch1; p=ch1; v=ch2; x=ch2; while(e>0) { e=e/2; n1++; } while(x>0) { x=x/2; n2++; } for(i=0;i<n1;i++) { g[i]=p%2; p=p/2; } for(i=n1;i<8;i++) { g[i]=0; } for(i=8;i<=(7+n2);i++) { g[i]=v%2; v=v/2; }

for(i=(8+n2);i<16;i++) { g[i]=0; } for(i=0;i<16;i++) { u=u+(g[i]*pow(2,i)); } printf("Required integer is:\n"); printf("%d",u); break; } }while(choice); return(0); }

OUTPUT: WELCOME 1.Enter Integers 2.Enter 2 characters 0.Exit Choice: 1 Enter the integer:12321 ! 0 WELCOME 1.Enter Integers 2.Enter 2 characters 0.Exit Choice: 2 Enter Character 1: Enter Character 2: 2 Required integer is: 12810 WELCOME 1.Enter Integers 2.Enter 2 characters 0.Exit Choice: 0

6. Write a menu driven program in C to: <i> Open a file in 'w' mode <ii>Write data in file <iii>Open file in read mode and print the contents of the file #include<stdio.h> int main() { FILE *file; int counter=0; int choice; char fileName[20]; char string[50]; char c; do { printf("\nREAD AND WRITE FILES\n"); printf("\n1.Read a file\n2.Write a file\nChoice:"); scanf("%d",&choice); switch(choice) { case 1: printf("\nName of File : "); scanf("%s",fileName); file=fopen(fileName,"r"); c=fgetc(file); do { printf("%c",c); c=fgetc(file); }while(c!=EOF); fclose(file); break; case 2: printf("\nName of File : "); scanf("%s",fileName); file=fopen(fileName,"w"); printf("\nEnter the string to be written:\n "); scanf("%s",string); counter=0; while(string[counter]!='\0') { putc(string[counter],file); counter++; } fclose(file);

break;

case 0: printf("\nTHANK YOU\n"); break; default:printf("\nInvalid choice"); break; } }while(choice); return 0; }

OUTPUT: READ AND WRITE FILES 1.Read a file 2.Write a file Choice:2 Name of File : abc.txt Enter the string to be written: hello READ AND WRITE FILES 1.Read a file 2.Write a file Choice:1 Name of File : abc.txt hello READ AND WRITE FILES 1.Read a file 2.Write a file Choice:0 THANK YOU

7. Write a program to count the number of lines and characters in a file. #include<stdio.h> void count(); int main() { int choice; do { printf("\nCOUNT THE NUMBER OF LINES IN A FILE\n"); printf("\n1.Count the number of lines\n0.Exit\nChoice:"); scanf("%d",&choice); switch(choice) { case 1: count(); break; case 0: printf("\nTHANK YOU\n"); break; default:printf("\nInvalid choice"); break; } }while(choice); return 0; } void count() { int counter=0,character=0; FILE *file; char c; char fileName[25];

printf("\nName of File : "); scanf("%s",fileName);

file=fopen(fileName,"r");

c=fgetc(file); do { c=fgetc(file); character++; if(c=='\n') { counter++; } }while(c!=EOF); printf("\nThe Number of newlines: %d and characters in the program: %d \n",counter,character); fclose(file); } OUTPUT: COUNT THE NUMBER OF LINES IN A FILE 1.Count the number of lines 0.Exit Choice:1 Name of File : def.txt The Number of newlines: 5 and characters in the program: 49 COUNT THE NUMBER OF LINES IN A FILE 1.Count the number of lines 0.Exit Choice:0 THANK YOU

8. Write a Program in C to compare two files #include<stdio.h> void compare(); int main() { int choice; do { printf("\nCOMPARE TWO FILES\n"); printf("\n1.Compare two files\n0.Exit\nChoice:"); scanf("%d",&choice); switch(choice) { case 1: compare(); break; case 0: printf("\nTHANK YOU\n"); break; default:printf("\nInvalid choice"); break; } }while(choice); return 0; } void compare() { int flag; FILE *file1,*file2; char c1,c2; char fileName1[25],fileName2[25];

printf("\nName of File 1: ");

scanf("%s",fileName1); printf("\nName of File 2: "); scanf("%s",fileName2);

file1=fopen(fileName1,"r"); file2=fopen(fileName2,"r"); if(file1==NULL||file2==NULL) { printf("\nFiles not found"); fclose(file1); fclose(file2); return; } c1=fgetc(file1); c2=fgetc(file2); do { c1=fgetc(file1); c2=fgetc(file2); if(c1!=c2) { flag=0; break; } }while(c1!=EOF&&c2!=EOF); if(!flag) { printf("\nThe Files are not same\n"); } else { printf("\nThe Files are same\n"); } fclose(file1); fclose(file2); }

OUTPUT: COMPARE TWO FILES 1.Compare two files 0.Exit Choice:1 Name of File 1: abc.txt Name of File 2: def.txt The Files are not same COMPARE TWO FILES 1.Compare two files 0.Exit Choice:1 Name of File 1: abc.txt Name of File 2: abc.txt The Files are same COMPARE TWO FILES 1.Compare two files 0.Exit Choice:0 THANK YOU

9. Write a program in C++ to show the friendship of a member functions of another class. //A PROGRAM TO SHOW FRIENDSHIP OF TWO CLASSES #include<iostream> using namespace std; class second; class first { int i; public: void input(); void output(); void swap(second&); }; class second { int j; public: void getdata() { cout<<"\nj: "; cin>>j; } void showdata() { cout<<"\nj: "<<j<<"\n"; } friend void first::swap(second&); }; void first::input() { cout<<"\ni: "; cin>>i;

} void first::output() { cout<<"\ni: "<<i<<"\n"; } void first::swap(second &a) { int temp; temp=a.j; a.j=i; i=temp; } int main() { first obj1; second obj2; int choice; do { cout<<"\n\nFRIENDSHIP OF MEMBER FUNCTIONS "; cout<<"\n1.Input data"; cout<<"\n2.Print data"; cout<<"\n3.Swap data"; cout<<"\n0.Exit\nChoice: "; cin>>choice; switch(choice) { case 1: cout<<"\nFor first class:"; obj1.input(); cout<<"\nComplex number 2:"; obj2.getdata(); break; case 2: cout<<"\nFirst class"; obj1.output(); cout<<"\nSecond Class:"; obj2.showdata(); break; case 3: obj1.swap(obj2); cout<<"\nSwapped"; break; case 0: cout<<"\nThank You"; break;

default:cout<<"\nInvalid Choice"; break; } }while(choice); return 0; } OUTPUT: FRIENDSHIP OF MEMBER FUNCTIONS 1.Input data 2.Print data 3.Swap data 0.Exit Choice: 1 For first class: i: 12 Complex number 2: j: 21 FRIENDSHIP OF MEMBER FUNCTIONS 1.Input data 2.Print data 3.Swap data 0.Exit Choice: 2 First class i: 12 Second Class: j: 21 FRIENDSHIP OF MEMBER FUNCTIONS 1.Input data 2.Print data 3.Swap data 0.Exit Choice: 3 Swapped FRIENDSHIP OF MEMBER FUNCTIONS 1.Input data 2.Print data 3.Swap data 0.Exit

Choice: 2 First class i: 21 Second Class: j: 12 FRIENDSHIP OF MEMBER FUNCTIONS 1.Input data 2.Print data 3.Swap data 0.Exit Choice: 0 Thank You

10. Write a program in C++ to merge 2 arrays using

friend function.

//PROGRAMM TO MERGE TWO ARRAYS USING FRIEND FUNCTION #include<iostream> using namespace std; class array2; class array1 { int a[10]; int m; public: void getarray1() { cout<<"\nEnter Array size: "; cin>>m; cout<<"Enter Array: "; for(int i=0;i<m;i++) { cin>>a[i]; } } friend void merge(array1, array2); }; class array2 { int b[10]; int n; public: void getarray2() {

cout<<"\nEnter Array size: "; cin>>n; cout<<"Enter Array: "; for(int i=0;i<n;i++) { cin>>b[i]; } } friend void merge(array1, array2); }; void merge(array1 x, array2 y) { int c[20]; for(int i=0;i<x.m;i++) { c[i]=x.a[i]; } for(int j=0;j<y.n;j++) { c[j+x.m]=y.b[j]; } cout<<"\n"; cout<<"\nMERGED ARRAY: \n"; for(int k=0;k<(y.n+x.m);k++) { cout<<c[k]<<" "; }

int main() { cout<<"\nMERGE TWO ARRAYS USING FRIEND FUNCTION"; array1 obj1; array2 obj2; cout<<"\nFirst array: "; obj1.getarray1();

cout<<"\nSecond Array: " ; obj2.getarray2(); merge(obj1,obj2); return 0; }

OUTPUT MERGE TWO ARRAYS USING FRIEND FUNCTION First array: Enter Array size: 3 Enter Array: 1 2 3 Second Array: Enter Array size: 4 Enter Array: 32 231 34 23 MERGED ARRAY: 1 2 3 32 231 34 23

11. Write a program in C++ to show the significance of static and local variable #include<iostream> using namespace std; class ABC { int a; static int b; public: void getdata() { cin>>a; b++; } void printdata() { cout<<"private variable a : "<<a<<"\nstatic variabe b: "<<b<<"\n"; } }; int ABC::b; int main() { ABC p,q; cout<<"\nTO SHOW THE USE OF STATIC VARIABLE\n"; cout<<"\nEnter the value of 'a' of 1st object: "; p.getdata(); cout<<"\nThe value of elements of 1st variable: "; p.printdata(); cout<<"\nEnter the value of 'a' of 2nd object: "; q.getdata(); cout<<"\nThe value of elements of 2nd variable: "; q.printdata(); cout<<"\nThe value of elements of 1st variable: ";

p.printdata(); cout<<"\nValue of static variable 'b' of both objects are\n same but the value of 'a' variable is different"; return 0; }

OUTPUT: TO SHOW THE USE OF STATIC VARIABLE Enter the value of 'a' of 1st object: 123 The value of elements of 1st variable: private variable a : 123 static variabe b: 1 Enter the value of 'a' of 2nd object: 213 The value of elements of 2nd variable: private variable a : 213 static variabe b: 2 The value of elements of 1st variable: private variable a : 123 static variabe b: 2 Value of static variable 'b' of both objects are same but the value of 'a' variable is different

12. Write a program to show the friendship of a non member function by doing sum of two numbers #include<iostream> using namespace std; class me { int a; public: void getdata() { cout<<"\nEnter value of a: "; cin>>a; } void print() { cout<<"\na= "<<a; } friend void swapping(int,me); }; void swapping(int b,me q) { int temp; temp=q.a; q.a=b; b=temp; } int main() { int b=0; me look;

cout<<"Enter value of b "; cin>>b; look.getdata(); swapping(b,look); look.print(); cout<<"\nb = "<<b<<"\n"; return 0; } OUTPUT: Enter value of b 12 Enter value of a: 21 a= 21 b = 12 13. Write a program in C++ to perform function overloading on these cases to calculate the volume <i> Volume of Cube <ii> Volume of Cuboid <iii> Volume of Sphere //PROGRAM TO IMPLEMENT FUNCTION OVERLOADING #include<iostream> using namespace std; double volume(double); double volume(double,double,double); double volume(int); int main() { double edge; double length ,breadth ,height; int radius; int choice; do { cout<<"\n\nPROGRAM TO IMPLEMENT FUNCTION OVERLOADING\n"; cout<<"\n1.Calculate volume of cube"; cout<<"\n2.Calculate volume of cuboid"; cout<<"\n3.Calculate volume of sphere"; cout<<"\n0.Exit\nChoice: "; cin>>choice; cout<<"\n"; switch(choice) { case 1: cout<<"\nEnter length of side of cube.\n";

cin>>edge; cout<<"\nVolume: "<<volume(edge); break; case 2: cout<<"\nEnter length,width,height of cuboid.\n"; cin>>length>>breadth>>height; cout<<"\nVolume: "<<volume(length,breadth,height); break; case 3: cout<<"Enter the radius length.\n"; cin>>radius; cout<<"\nVolume: "<<volume(radius); break; case 0: cout<<"\nThank You\n"; break; default:cout<<"\nInvalid choice\n"; break; } }while(choice); return(0); } double volume(double x) { return(x*x*x); } double volume(double a,double b,double c) { return(a*b*c); } double volume(int d) { return(((3.14)*(4/3)*(d*d*d))); }

OUTPUT: PROGRAM TO IMPLEMENT FUNCTION OVERLOADING 1.Calculate volume of cube 2.Calculate volume of cuboid 3.Calculate volume of sphere 0.Exit Choice: 1 Enter length of side of cube. 12 Volume: 1728 PROGRAM TO IMPLEMENT FUNCTION OVERLOADING 1.Calculate volume of cube 2.Calculate volume of cuboid 3.Calculate volume of sphere 0.Exit Choice: 2 Enter length,width,height of cuboid. 12 3 4 Volume: 144 PROGRAM TO IMPLEMENT FUNCTION OVERLOADING 1.Calculate volume of cube 2.Calculate volume of cuboid 3.Calculate volume of sphere

0.Exit Choice: 3 Enter the radius length. 21 Volume: 29079.5 PROGRAM TO IMPLEMENT FUNCTION OVERLOADING 1.Calculate volume of cube 2.Calculate volume of cuboid 3.Calculate volume of sphere 0.Exit Choice: 0 Thank You

14. Write a program in C++ to perform operator overloading in 3 cases: <i> '+' to add 2 complex numbers <ii> '-' to subtract 2 complex numbers <iii> '*' to multiply 2 complex Numbers #include<iostream> using namespace std; class complex { float x; float y; public: complex() { } complex( float real,float imag) { x=real; y=imag; } complex operator+(complex ); complex operator-(complex); complex operator*(complex); void getdata(); void display(); }; complex complex::operator+(complex c) { complex temp;

temp.x=x+c.x; temp.y=y+c.y; return(temp); } complex complex::operator-(complex c1) { complex temp; temp.x=x-c1.x; temp.y=y-c1.y; return(temp); } complex complex::operator*(complex c2) { complex temp; temp.x=(((x*(c2.x))-(y*(c2.y)))); temp.y=(((x*(c2.y))+(y*(c2.x)))); return(temp); } void complex::getdata() { cout<<"\nReal part:"; cin>>x; cout<<"Imaginary part:"; cin>>y; } void complex::display() { cout<<x<<"+("<<y<<")i\n"; } int main() { complex C1,C2,C3; int choice=1; while(choice) { cout<<"\n ADDITION SUBTRACTION AND MULTIPLICATION OF COMPLEX NUMBERS"; cout<<"\n1.Add\n2.Subtract\n3.Multiply\n0.Exit\nChoice: "; cin>>choice; switch(choice) { case 1: cout<<"\nEnter two complex numbers"; cout<<"\n1st complex number:"; C1.getdata(); cout<<"\n2nd complex number:"; C2.getdata(); C3=C1+C2; cout<<"\nReuslt: "; C3.display();

break; case 2: cout<<"\nEnter two complex numbers"; cout<<"\n1st complex number:"; C1.getdata(); cout<<"\n2nd complex number:"; C2.getdata(); C3=C1-C2; cout<<"\nReuslt: "; C3.display(); break; case 3: cout<<"\nEnter two complex numbers"; cout<<"\n1st complex number:"; C1.getdata(); cout<<"\n2nd complex number:"; C2.getdata(); C3=C1-C2; cout<<"\nReuslt: "; C3.display(); break; case 0: cout<<"\nTHANK YOU\n"; break; default:cout<<"\nInvalid Choice"; break; } }

return(0); }

OUTPUT: ADDITION SUBTRACTION AND MULTIPLICATION OF COMPLEX NUMBERS 1.Add 2.Subtract 3.Multiply 0.Exit Choice: 1 Enter two complex numbers 1st complex number: Real part:12 Imaginary part:12 2nd complex number: Real part:21 Imaginary part:2 Reuslt: 33+(14)i ADDITION SUBTRACTION AND MULTIPLICATION OF COMPLEX NUMBERS 1.Add 2.Subtract 3.Multiply 0.Exit Choice: 2 Enter two complex numbers 1st complex number: Real part:21 Imaginary part:21

2nd complex number: Real part:22 Imaginary part:3 Reuslt: -1+(18)i ADDITION SUBTRACTION AND MULTIPLICATION OF COMPLEX NUMBERS 1.Add 2.Subtract 3.Multiply 0.Exit Choice: 3 Enter two complex numbers 1st complex number: Real part:2 Imaginary part:2 2nd complex number: Real part:1 Imaginary part:2 Reuslt: 1+(0)i ADDITION SUBTRACTION AND MULTIPLICATION OF COMPLEX NUMBERS 1.Add 2.Subtract 3.Multiply 0.Exit Choice: 0 THANK YOU

15. Write a program in C++ to illustrate use of constructor & destructor by constructing and destructing that will increase or decrease count. #include<iostream> using namespace std; class cons_dest { static int count; public: cons_dest() { count++; cout<<"\n"<<count<<"\n"; } ~cons_dest() { count--; cout<<"\ncount: "<<count<<"\n"; } void printdata() { cout<<"\ncount : "<<count<<"\n"; } };

int cons_dest::count=0; int main() { cons_dest obj1, obj2; cout<<"\nObj1 constructed:\n"; obj1.printdata(); cout<<"\nObj2 constructed:\n"; obj2.printdata(); cout<<"\nObj1 and obj2 destructed:\n"; }

OUTPUT: 1 2 Obj1 constructed: count : 2 Obj2 constructed: count : 2 Obj1 and obj2 destructed: count: 1 count: 0

16. Write a program in C++ to add 2 numbers by passing object as an argument. #include<iostream> using namespace std; class data { public: int i; void getdata() { cin>>i; } }; class cons_addition { public: cons_addition(data a,data b) { int sum; sum = a.i + b.i; cout<<"\nsum :"<<sum; } };

int main() { data obj1, obj2; cout<<"\nEnter data for the first object:"; obj1.getdata(); cout<<"\nEnter data for the second object:"; obj2.getdata(); cout<<"\nHere is the sum: "; cons_addition ll(obj1,obj2); return 0; } OUTPUT: Enter data for the first object:12 Enter data for the second object:21 Here is the sum: sum :33

17. Write a program in C++ to convert class Cartesian to class Polar using constructor #include<iostream> #include<math.h> using namespace std;

class cartesian { int x,y; public: void getdata() { cout<<"\nx-coordinate:"; cin>>x; cout<<"y-coordinate:"; cin>>y; } int getx(){return x;} int gety(){return y;} };

class polar { float r; float angle; public: polar(cartesian c) { r = sqrt((c.getx()*c.getx())+(c.gety()*c.gety())); angle = atan((c.gety())/(c.getx())); cout<<"\nr : "<<r<<"\n"; cout<<"angle: "<<angle<<" radians\n"; }; }; int main() { cartesian obj1; cout<<"ENTER THE CORDINATES OF THE POINT\n"; obj1.getdata(); polar obj2(obj1); return 0; }

OUTPUT: ENTER THE CORDINATES OF THE POINT x-coordinate:3 y-coordinate:4 r:5 angle: 0.785398 radians

18. Write a program in C++ to implement multilevel inheritance #include<iostream> using namespace std; class student { char name[10]; int adm_no; public: void getinfo() { cout<<"\nEnter name:"; cin>>name; cout<<"Enter admission no.:"; cin>>adm_no; } void showinfo() { cout<<"Name of student is\t"<<name;

cout<<"Admission no. is\t"<<adm_no; } }; class marks : public student { int mark; public: void getdata() { student::getinfo(); cout<<"\nEnter marks: "; cin>>mark; } void showdata() { student::showinfo(); cout<<"\nMarks are\t"<<mark; } }; class degree : public marks { char degree[10]; public: void getval() { marks::getdata(); cout<<"Enter degree: "; cin>>degree; } void showval() { marks::showdata(); cout<<"\nDegree is\t"<<degree; } }; int main() { int i,j; degree d[3]; cout<<"\nPROGRAM FOR MULTILEVEL INHERITANCE"; for( i=0;i<3;i++) { cout<<"\nStudent "<<i+1<<":"; d[i].getval(); } for( j=0;j<3;j++) { cout<<"\nStudent "<<j+1<<":\n"; d[j].showval(); } return 0 ; }

OUTPUT: PROGRAM FOR MULTILEVEL INHERITANCE Student 1: Enter name:John Enter admission no.:12122 Enter marks: 12 Enter degree: cbse Student 2: Enter name:amy Enter admission no.:12223 Enter marks: 121 Enter degree: btech Student 3: Enter name:sue Enter admission no.:12221 Enter marks: 140

Enter degree: icse Student 1: Name of student is JohnAdmission no. is 12122 Marks are 12 Degree is cbse Student 2: Name of student is amyAdmission no. is 12223 Marks are 121 Degree is btech Student 3: Name of student is sueAdmission no. is 12221 Marks are 140 Degree is icse

19. Write a program in C++ to implement Hybrid Inheritance //.....Header files..... #include<iostream> using namespace std; //...class declaration..... class person { private: char name[20]; int code; public: void getdata() { cout<<"Enter the name: ";

cin>>name; cout<<"Enter code: "; cin>>code; } void showdata() { cout<<"Code of the employee is: "<<name<<endl<<"Code: "<<code<<endl; } }; //.....declaring derived class of person base class.... class account:virtual public person { private: int pay; public: void getpay() { cout<<"Enter the payment: "; cin>>pay; } void printpay() { cout<<"Payment of the employee is: "<<pay<<" rupees"<<endl; } }; //....declaring another derived class of person base class.... class admin:virtual public person { private: int exp; public: void getexp() { cout<<"Enter experience of the employee: "; cin>>exp; } void showexp() { cout<<"The experience of the employee is: "<<exp<<"years"<<endl; } }; //.....declaring derived class of account and admin base class/.... class employee:public account,public admin { private: public:

void get() { cout<<"\nWelcome\n"<<endl; getdata(); getpay(); getexp(); } void print() { cout<<"\n\nDisplaying the details: "<<endl; showdata(); printpay(); showexp(); } }; //....main function declaration.... int main() { employee e; e.get(); e.print(); }

OUTPUT: Welcome Enter the name: John Enter code: 12232 Enter the payment: 20000 Enter experience of the employee: 2 Displaying the details: Code of the employee is: John Code: 12232 Payment of the employee is: 20000 rupees The experience of the employee is: 2years

20. Write a program in C++ using generic function to perform swapping of different datatype #include<iostream> using namespace std; template<class T> void swapping(T &x,T &y) { T temp; temp=x; x=y; y=temp; }

int main() { int choice; int a,b; float c,d; char e,f; do { cout<<"\n\nGENERIC FUNCTION FOR SWAPPING"; cout<<"\n1.Swap 2 integers\n2.Swap 2 Real numbers"; cout<<"\n3.Swap 2 Characters\n0.Exit\nChoice: "; cin>>choice; cout<<"\n"; switch(choice) { case 1: cout<<"\nFirst Integer: "; cin>>a; cout<<"Second Integer: "; cin>>b; swapping<int>(a,b); cout<<"\nAfter swapping:"; cout<<"\nFirst Integer: "<<a<<"\nSecond Integer: "<<b; break; case 2: cout<<"\nFirst number: "; cin>>c; cout<<"Second number: "; cin>>d; swapping<float>(c,d); cout<<"\nAfter swapping:"; cout<<"\nFirst Number: "<<c<<"\nSecond Number: "<<d; break; case 3: cout<<"\nFirst Character: "; cin>>e; cout<<"Second Character: "; cin>>f; swapping<char>(e,f); cout<<"\nAfter swapping:"; cout<<"\nFirst Character: "<<e<<"\nSecond Character: "<<f; break; case 0: cout<<"\nTHANK YOU\n"; break; default:cout<<"\nInvalid choice";

break; } }while(choice); return 0; }

OUTPUT: GENERIC FUNCTION FOR SWAPPING 1.Swap 2 integers 2.Swap 2 Real numbers 3.Swap 2 Characters 0.Exit Choice: 1 First Integer: 12 Second Integer: 21 After swapping: First Integer: 21 Second Integer: 12 GENERIC FUNCTION FOR SWAPPING 1.Swap 2 integers

2.Swap 2 Real numbers 3.Swap 2 Characters 0.Exit Choice: 2 First number: 12.08 Second number: 0.02 After swapping: First Number: 0.02 Second Number: 12.08 GENERIC FUNCTION FOR SWAPPING 1.Swap 2 integers 2.Swap 2 Real numbers 3.Swap 2 Characters 0.Exit Choice: 3 First Character: a Second Character: d After swapping: First Character: d Second Character: a GENERIC FUNCTION FOR SWAPPING 1.Swap 2 integers 2.Swap 2 Real numbers 3.Swap 2 Characters 0.Exit Choice: 0 THANK YOU 21. Write a function in C++ to implement generic class and perform addition of two complex numbers #include<iostream> using namespace std; template <class T>

class complexNumbers { T real,imaginary; public:

void complexInput() { cout<<"\nEnter real part: "; cin>>real; cout<<"Enter imaginary part:"; cin>>imaginary; } void complexOutput() { cout<<"\nThe complex number is :"; cout<<real<<" + i "<<imaginary; } complexNumbers operator+(complexNumbers a) { complexNumbers temp; temp.real=real + a.real; temp.imaginary=imaginary + a.imaginary; return temp; } void operator=(complexNumbers a) { real=a.real; imaginary=a.imaginary; }

}; int main() { complexNumbers<int> a,b,c; complexNumbers<float> d,e,f; int choice; do { cout<<"\n\nOPERATIONS OF COMPLEX NUMBERS"; cout<<"\n1.Add two integer type complex numbers"; cout<<"\n2.Add 2 real type complex numbers"; cout<<"\n0.Exit\nChoice: "; cin>>choice;

switch(choice) { case 1: cout<<"\nComplex number 1:"; a.complexInput(); cout<<"\nComplex number 2:"; b.complexInput(); c=a+b; c.complexOutput(); break; case 2: cout<<"\nComplex number 1:"; d.complexInput(); cout<<"\nComplex number 2:"; e.complexInput(); f=d+e; f.complexOutput(); break; case 0: cout<<"\nThank You\n"; break; } }while(choice); return 0; }

OUTPUT: OPERATIONS OF COMPLEX NUMBERS 1.Add two integer type complex numbers 2.Add 2 real type complex numbers 0.Exit Choice: 1 Complex number 1: Enter real part: 12 Enter imaginary part:21 Complex number 2: Enter real part: 22

Enter imaginary part:-9 The complex number is :34 + i 12 OPERATIONS OF COMPLEX NUMBERS 1.Add two integer type complex numbers 2.Add 2 real type complex numbers 0.Exit Choice: 2 Complex number 1: Enter real part: 31.99 Enter imaginary part:12.02 Complex number 2: Enter real part: 21.09 Enter imaginary part:-2.21 The complex number is :53.08 + i 9.81 OPERATIONS OF COMPLEX NUMBERS 1.Add two integer type complex numbers 2.Add 2 real type complex numbers 0.Exit Choice: 0 Thank You

22. Write a program in C++ to implement virtual function #include<iostream> using namespace std; class window { public: virtual void create() { cout<<"base class window"; } };

class command_button:public window { public: void create() { cout<<"derived class command button"; } }; int main() { window *x,*y; x=new window(); x->create(); y=new command_button(); cout<<endl; y->create(); return 0; }

// x,y both are of window class type //x is created dynamically of window class //y is created dynamically of command_button class

OUTPUT: base class window derived class command button

23. Write a function to implement exception handling in C++ #include<iostream> using namespace std; int main() { try { float x,y,z; cout<<"\nx: "; cin>>x;

cout<<"\ny: "; cin>>y; if(y==0) { throw(z); } else { z=x/y; cout<<z; } } catch(...) { cout<<"\nError caught: Division by zero\nAbnormal termination.\n"; } return 0; } OUTPUT: x: 12 y: 0 Error caught: Division by zero Abnormal termination.

You might also like