Object Oriented Programming Using C++: Bachelor of Technology in
Object Oriented Programming Using C++: Bachelor of Technology in
CT INSTITUTE OF TECHNOLOGY SHAHPUR (JALANDHAR), PUNJAB (INDIA) (Affiliated to Punjab Technical University, Jalandhar, Punjab (India))
TABLE OF CONTENTS
S.NO. NAME OF EXPERIMENT 1 Write a program to swap two numbers without using third variable. 2 Write a program to find the reverse of entered number. 3 Write a program to check that entered number is palindrome or not. 4 Write a program to find the sum of digits of entered number. 5 Write a program to find the greatest number among 3 using nested if-else. 6 7 8 9 Write a program to make calculator using switch statement. Write a program to input indefinite numbers and calculate sum of only +ve numbers. The program terminates when number is ve. Write a program to check that entered number is digit, character or any special character. Write a program to check grade according to given conditions:- if marks are between 70-80 then print C grade if marks are between 80-90 then print B grade or if marks are between 90100 then print A grade. Write program to find fibbonacci series upto n. Write a program to print given pattern. Write a program to print given pattern. Write a program to swap two numbers using call by reference argument passing technique. Write a program to find factorial of a number using recursion. Write a program to show the use of inline function. Write a program to format the output of program using manipulators. Write a program to find sum of elements entered in an array. Write a program to sort the elements using bubble sort technique. Write a program to add two matrices. Write a program to find the transpose of matrix. Write a program to multiply two matrices. Write a program to illustrate the memory concept of union and structure. Write a program to calculate area of rectangle by defining member function outside the class. Write a program to show the use of parameterized constructor.
PAGE NO.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
PRACTICAL NO. 1
Write a program to swap two numbers without using third variable.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b; cout<<"ENTER THE VALUE OF a "; cin>>a; cout<<"ENTER THE VALUE OF b "; cin>>b; cout<<"AFTER SWAPPING "<<endl; a=a+b; b=a-b; a=a-b; cout<<"VALUE OF a "<<a<<endl; cout<<"VALUE OF b "<<b; getch(); } Its output is:-
PRACTICAL NO. 2
Write a program to find the reverse of entered number.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int i, n, r, s=0; cout<<"ENTER THE NUMBER "; cin>>n; while(n>0) { r=n%10; s=s*10+r; n=n/10; } cout<<"REVERSE IS "<<s; getch(); } Its output is:-
PRACTICAL NO. 3
Write a program to find that entered number is palindrome or not.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int i,t,n,r,s=0; cout<<"ENTER THE NUMBER "; cin>>n; t=n; while(n>0) { r=n%10; s=s*10+r; n=n/10; } cout<<"REVERSE IS "<<s; if(t==s) cout<<"STRING IS PALINDROME"; else
PRACTICAL NO. 4
Write a program to calculate the sum of digits of entered number.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int i,n,r,s=0; cout<<"ENTER THE NUMBER "; cin>>n; while(n>0) { r=n%10; s=s+r; n=n/10; } cout<<"SUM IS "<<s; getch(); } Its output is:-
PRACTICAL NO. 5
Write a program to find the greatest number among 3 using nested if-else.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b,c; cout<<"ENTER THE 1st NUMBER "; cin>>a; cout<<"ENTER THE 2nd NUMBER "; cin>>b; cout<<"ENTER THE 3rd NUMBER "; cin>>c; if(a>b){ if(a>c) cout<<"1st ELEMENT IS GREATEST"; else cout<<"3rd ELEMENT IS GREATEST"; } else{
if(b>c) cout<<"2nd ELEMENT IS GREATEST"; else cout<<"3rd ELEMENT IS GREATEST"; } getch(); } Its output is:-
PRACTICAL NO. 6
Write a program to make a calculator using switch.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b; char ch; cout<<"ENTER THE 1st NUMBER "; cin>>a; cout<<"ENTER THE 2nd NUMBER "; cin>>b; cout<<"\n\nCHOICES AVAILABLE ARE\n\nADDITION (+)\n\nSUBTRACTION (-)\n\nMULTIPLICATION (*)\n\nDIVISION (/)\n\n" cout<<ENTER WHAT YOU WANT TO DO ; cin>>ch; switch(ch) { case '+': cout<<"RESULT IS "<<a+b;
break; case '-': cout<<"RESULT IS "<<a-b; break; case '*': cout<<"RESULT IS "<<a*b; break; case '/': cout<<"RESULT IS "<<a/b; break; default: cout<<"INVALID ENTRY" getch(); }
PRACTICAL NO. 7
Write a program to input indefinite numbers and calculate sum of only +ve numbers. The program terminates when number is ve.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int s=0,n; i=1; while(i<3) { cout<<"ENTER ANY THREE TERMS "; cin>>n; } if(n>=0) { s=s+n; } else {
PRACTICAL NO. 8
Write a program to find that entered number is digit, character or special character.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int n; cout<<"ENTER WHAT YOU WANT TO FEED\n"; cin>>n; if(n>=0&&n<=9) cout<<"YOU HAVE ENTERED A DIGIT"; else if((n>=a || n>=A) || (n<=z || n<=Z)) cout<<"YOU HAVE ENTERED A CHARACTER"; else cout<<"YOU HAVE ENTERED A SPECIAL CHARACTER"; getch(); } Its output is:-
PRACTICAL NO. 9
Write a program to check grade according to given conditions a. if marks are between 70-80 then print C grade b. if marks are between 80-90 then print B grade c. if marks are between 90-100 then print A grade
#include<iostream.h> #include<conio.h> void main() { clrscr(); int marks; cout<<"ENTER PERCENTAGE OF MARKS OBTAINED BY STUDENT (ABOVE 70) "; cin>>marks; if(marks>=70) { if(marks>=70&&marks<=80) cout<<"STUDENT HAS EARNED C GRADE"; else if(marks>80&&marks<=90) cout<<"STUDENT HAS EARNED B GRADE"; else cout<<"STUDENT HAS EARNED A GRADE";
PRACTICAL NO. 10
Write a program to print Fibonacci series up to n terms.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a=0,b=1,c,n; cout<<"ENTER THE NUMBER OF TERMS UPTO YOU WANT THE SERIES "; cin>>n; cout<<a<<endl<<b<<endl; for(int i=1;i<=n-2;i++) { c=a+b; cout<<c; a=b; b=c; } getch(); }
PRACTICAL NO. 11
Write a program to calculate factorial of given number.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int f=1,i,n; cout<<"ENTER THE NUMBER "; cin>>n; for(i=1;i<=n;i++) { f=f*i; } cout<<"FACTORIAL OF NUMBER IS "<<f; getch(); } Its output is:-
PRACTICAL NO. 12
Write a program to print given pattern 1 12 123 1234 12345
#include<iostream.h> #include<conio.h> void main() { clrscr(); int n; for(int i=1;i<=5; i++) { for(int j=1;j<=i; j++) { cout<<j; }
PRACTICAL NO. 13
Write a program to print given pattern 1 22 333 4444 55555
#include<iostream.h> #include<conio.h> void main() { clrscr(); int n; for(int i=1;i<=5; i++) { for(int j=1;j<=i; j++) { cout<<i; }
PRACTICAL NO. 14
Write a program to swap two numbers using call by reference argument passing technique.
#include<iostream.h> #include<conio.h> void swap(int *,int *); void main() { clrscr(); int a,b; cout<<"ENTER THE VALUE OF a "; cin>>a; cout<<"ENTER THE VALUE OF b "; cin>>b; swap(&a, &b); getch(); } void swap(int *m, int *n) { int c; c=*m; *m=*n; *n=*c;
PRACTICAL NO. 15
Write a program to find factorial of a number using recursion.
#include<iostream.h> #include<conio.h> int fact(int); void main() { clrscr(); int n,k; cout<<"ENTER ANY INTEGER "; cin>>n; k=fact(n); cout<<"FACTORIAL OF ENTERED INTEGER IS "<<k; getch(); } int fact(int n) { int f; if(n==1) return(1);
PRACTICAL NO. 16
Write a program to show the use of inline function.
#include<iostream.h> #include<conio.h> inline float mul(float x, float y) { return(x*y); } void main() { float a=12.345; float b=9.82; cout<<endl<<mul(a,b); getch(); } Its output is:-
PRACTICAL NO. 17
Write a program to format the output of program using manipulators.
#include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { clrscr(); int basic=950, allowance=95, total=1045; cout<<setw(10)<<"BASIC"<<setw(10)<<basic<<endl; cout<<setw(10)<<"ALLOWANCE"<<setw(10)<<allowance<<endl; cout<<setw(10)<<"TOTAL"<<setw(10)<<total<<endl; getch(); }
PRACTICAL NO. 18
Write a program to find the sum of entered elements in an array.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,a[100],i,s=0; cout<<"ENTER THE NUMBER UPTO WHICH YOU WANT TO ENTER THE DIGITS "; cin>>n; for(i=1;i<=n;i++) { cin>>a[i]; }
PRACTICAL NO. 19
Write a program to sort the elements using bubble sort technique.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,j,a[100],i; cout<<"ENTER THE NUMBER UPTO WHICH YOU WANT TO ENTER THE DIGITS "; cin>>n; for(i=0;i<n;i++) { cin>>a[i]; } for(i=0;i<n-1;i++)
{ for(j=0;j<n-i-1;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } cout<<"SORTED ARRAy IS \n\n"; for(i=0;i<n;i++) { cout<<a[i]<<endl; } getch(); }
PRACTICAL NO. 20
Write a program to add two matrices.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int r,c,m,n,a[50][50],i,j,b[50][50],p[100][100]; cout<<"ENTER THE NUMBER OF ROWS OF 1st MATRIX "; cin>>r; cout<<"ENTER THE NUMBER OF COLUMNS OF 1st MATRIX "; cin>>c; cout<<"ENTER THE NUMBER OF ROWS OF 2nd MATRIX "; cin>>m; cout<<"ENTER THE NUMBER OF COLUMNS OF 2nd MATRIX "; cin>>n; cout<<"\n\nNOW,ENTER THE ELEMENTS IN 1st MATRIX \n";
for(i=0;i<r;i++) { for(j=0;j<c;j++) { cin>>a[i][j]; } } cout<<"\nNOW,ENTER THE ELEMENTS IN 2nd MATRIX \n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cin>>b[i][j]; } } for(i=0;i<r;i++) { for(j=0;j<c;j++) { p[i][j]=a[i][j]+b[i][j]; } }
if(r==m&&c==n) { cout<<"\nSUM OF MATRICES IS\n\n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cout<<p[i][j]<<" "; } cout<<endl; } } getch(); } Its output is:-
PRACTICAL NO. 21
Write a program to find the transpose of matrix.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int r,c,a[50][50],i,j; cout<<"ENTER THE NUMBER OF ROWS "; cin>>r; cout<<"ENTER THE NUMBER OF COLUMNS "; cin>>c; cout<<"\n\nNOW,ENTER THE ELEMENTS IN MATRIX \n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) {
cin>>a[i][j]; }} cout<<"TRANSPOSE OF MATRIX IS \n\n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cout<<a[j][i]<<" "; } cout<<endl; } getch(); }
PRACTICAL NO. 22
Write a program to multiply two matrices.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int r,c,m,n,a[50][50],i,j,b[50][50],h[100][100]; cout<<"ENTER THE NUMBER OF ROWS OF 1st MATRIX "; cin>>r; cout<<"ENTER THE NUMBER OF COLUMNS OF 1st MATRIX "; cin>>c; cout<<"ENTER THE NUMBER OF ROWS OF 2nd MATRIX "; cin>>m; cout<<"ENTER THE NUMBER OF COLUMNS OF 2nd MATRIX "; cin>>n; cout<<"\n\nNOW,ENTER THE ELEMENTS IN 1st MATRIX \n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cin>>a[i][j]; } } cout<<"\n\nNOW,ENTER THE ELEMENTS IN 2nd MATRIX \n"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>b[i][j]; } } for(i=0;i<r;i++) { for(j=0;j<n;j++) { { h[i][j]=0; for(int k=0;k<c;k++) { h[i][j]=h[i][j]+a[i][k]*b[k][j]; } } } } cout<<"PRODUCT OF MATRICES IS \n";
PRACTICAL NO. 23
Write a program to show the memory concept of union.
#include<iostream.h> #include<conio.h> struct gurdeep { int a,b,c; char p; }abc; union oops { int a,b,c; char p; }xyz; void main() { clrscr(); cout<<"SIZE OF abc is "<<sizeof(abc)<<endl; cout<<"SIZE OF xyz is "<<sizeof(xyz); getch(); } Its output is:-
PRACTICAL NO. 24
Write a program to find area of rectangle by defining member functions outside the class.
#include<iostream.h> #include<conio.h> class rectangle { private: int area,l,b; public: void put(); void show(); }; rectangle::void put(); { cout<<"ENTER THE LENGTH "; cin>>l; cout<<"ENTER THE BREATH "; cin>>b; } rectangle::void show() { area=l*b; cout<<"AREA IS "<<area; } void main() { clrscr(); rectangle gurdeep; gurdeep.put(); gurdeep.show(); getch(); }
PRACTICAL NO. 25
Write a program to show use of parameterized constructor.
#include<iostream.h> #include<conio.h> class fact { private: int n,f,i; public: fact(int m) { f=1; n=m; } void ans() { for(i=1;i<=n;i++) { f=f*i; } cout<<"FACTORIAL IS "<<f; }}; void main() { clrscr(); fact f(5); f.ans(); getch(); } Its output is:-
PRACTICAL NO. 26
Write a program to show the use of copy constructor.
#include<iostream.h> #include<conio.h> class dist { int feet; float inch; public: dist() {} dist(int f1, float in1) { feet=f1; inch=in1; } dist(dist&d) { cout<<"COPY CONSTRUCTOR INVOKED\n"; feet=d.feet; inch=d.inch; } void show() { cout<<feet<<'\t'<<inch; } dist add(dist d) { dist t; t.feet=feet+d.feet; t.inch=inch+d.inch; if(t.inch>=12.00) { t.inch-=12.00; t.feet++; } return(t); } };
dist d1(5,7) dist d2=d1; dist d3; d3=d1.add(d2); d3.show(); getch(); } Its output is:-
PRACTICAL NO. 27
PRACTICAL NO. 28
PRACTICAL NO. 29
public: void now() { cout<<"VALUE OF b AFTER ADDING c "<<b+c; } }; void main() { clrscr(); cse3 c; c.enter(); c.show(); c.find(); c.now(); getch(); } Its output is:-
PRACTICAL NO. 30
PRACTICAL NO. 31
PRACTICAL NO. 32
PRACTICAL NO. 33
PRACTICAL NO. 34
PRACTICAL NO. 35