0% found this document useful (0 votes)
49 views45 pages

OOPS File

The document contains 10 C++ programs demonstrating various programming concepts: 1. A recursive program to calculate factorial of a number. 2. A program to generate Fibonacci series using recursion. 3. A program to multiply two matrices using nested for loops. 4. A program to check if a number is prime. 5. A program to check if a number is a palindrome. 6. Programs demonstrating classes and objects including a cylinder class and student class. 7. Programs passing objects as arguments including by value, reference and address. 8. A program adding two complex numbers by returning an object from a function. 9. A program implementing a friend function bridging two classes.

Uploaded by

yankyyash1306
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views45 pages

OOPS File

The document contains 10 C++ programs demonstrating various programming concepts: 1. A recursive program to calculate factorial of a number. 2. A program to generate Fibonacci series using recursion. 3. A program to multiply two matrices using nested for loops. 4. A program to check if a number is prime. 5. A program to check if a number is a palindrome. 6. Programs demonstrating classes and objects including a cylinder class and student class. 7. Programs passing objects as arguments including by value, reference and address. 8. A program adding two complex numbers by returning an object from a function. 9. A program implementing a friend function bridging two classes.

Uploaded by

yankyyash1306
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Program-1

Write a program to find the factorial of number using recursion


#include<iostream.h>

#include<conio.h>

int factorial(int n);

void main()

clrscr();

int n;

cout << "Enter a positive integer: ";

cin >> n;

cout << "Factorial of " << n << " = " << factorial(n);

getch();

int factorial(int n) {

if(n > 1)

return n * factorial(n - 1);

else

return 1;

}
Program-2

Write a program to generate Fibonacci series using function


#include<stdio.h>

#include<conio.h>

int Fibonacci(int n)

if ( n == 0 )

return 0;

else if ( n == 1 )

return 1;

else

return ( Fibonacci(n-1) + Fibonacci(n-2) );

void main() {

clrscr();

int n,i=0,c;

printf("\n How many Fibonacci term to be generated :");

scanf("%d",&n);

printf("\nFibonacci series\n");

for ( c = 1 ; c <= n ; c++ )

printf("%d\t", Fibonacci(i));

i++;

getch();

}
Program-3

Write a program to find the multiplication of two metrics


#include<iostream.h>

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void main()

clrscr();

int a[5][5],b[5][5],s[5][5]; // Maximum size is taken 5 for ease in calculation

int r1,c1,r2,c2,i,j,k,sum;

cout<<"\nEnter no of rows for first matrix : ";

cin>>r1;

cout<<"nEnter no of columns for first matrix : ";

cin>>c1;

cout<<"\nEnter no of rows for second matrix : ";

cin>>r2;

cout<<"\nEnter no of column for second matrix :";

cin>>c2;

if(c1!=r2)

cout<<"\n--- Matrices cannot be multiplied ---";

getch();

exit(0);

}
cout<<"\nEnter element of 1st matrix :";

for(i=0;i<r1;i++)

for(j=0;j<c1;j++)

cin>>a[i][j];

cout<<" ";

cout<<"\nEnter element of 2nd matrix :";

for(i=0;i<r2;i++)

for(j=0;j<c2;j++)

cin>>b[i][j];

cout<<" ";

for(i=0;i<r1;i++)

for(j=0;j<c2;j++)

int sum=0;

for(int k=0;k<c1;k++)

sum=sum+a[i][k]*b[k][j];

s[i][j]=sum;
}

cout<<"\nThe product of matrix is :";

for(i=0;i<r1;i++)

cout<<"\n";

for(j=0;j<c2;j++)

cout<<s[i][j];

cout<<"\t";

getch();

}
Program-4

Write a program to check whether the given number is prime or not

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int num,temp,i,flag=0;

cout<<"Enter the number : ";

cin>>num;

if(num==1||num==2)

cout<<"--- Given number is not prime ---";

for(i=2;i<=num/2;i++)

if(num%i==0)

flag=1;

cout<<"--- Given number is not prime ---";

break;

}
if(flag==0)

cout<<"--- Given number is prime ---";

getch();

Program-5
Write a program to check whether given number is palindrome or
not
#include<iostream.h>

#include<conio.h>

void main()

{ clrscr();

int num,temp,i,rev=0,pop;

cout<<"Enter the number : ";

cin>>num;

temp=num;

while(temp>0)

pop=temp%10;

rev=(rev*10)+pop;

temp=temp/10;

if(rev==num)

cout<<"--- Given number is palindrome ---";

else

cout<<"*** Given number is not palindrome ***";

getch();

Program-7
WAP of the class cylinder having the following : Data types - length, height,
pi=3.14Functions – calculate volume(), surface area (), area of cylinder()
Using scope resolution operator

#include<iostream.h>

#include<conio.h>

class cylinder

private:

float r;

float h;

float pi;

public:

cylinder() : pi(3.14) {}

float volume(float,float);

float area(float,float);

float sur_area(float,float);

};

float cylinder::volume(float x,float y)

r=x;

h=y;

return(pi*r*r*h);

float cylinder::area(float x,float y)

r=x;

h=y;
return(2*pi*r*r+2*pi*r*h);

float cylinder::sur_area(float x,float y)

r=x;

h=y;

return(2*pi*r*h);

cylinder c1;

void main()

clrscr();

int radius,height;

cout<<"Enter the radius of cylinder :";

cin>>radius;

cout<<"\nEnter the height of cylinder :";

cin>>height;

cout<<"\nVolume of cylinder is : "<<c1.volume(radius,height);

cout<<"\nArea of cylinder is : "<<c1.area(radius,height);

cout<<"\nSurface area of cylinder is : "<<c1.sur_area(radius,height);

getch();

Program-7
WAP of class student & display the data of a student of a class having 30
students. Write a class student having the following -

Data types : name,rollno,marks,percentage

Functions: readdata(),displaydata(),computedata().

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

class student

int rollno;

char name[30];

float marks[5];

float total,percentage;

public:

void readdata();

void displaydata();

void computedata();

};

void student::readdata()

cout<<"Enter roll number : ";

cin>>rollno;

cout<<"\nEnter name : ";

cin>>name;
}

void student::computedata()

float total=0;

for(int i=0;i<5;i++)

cout<<"\nEnter the marks of subject "<<i+1<<":";

cin>>marks[i];

total=total+marks[i];

percentage=total/5;

void student::displaydata()

cout<<"\nRoll no : "<<rollno<<endl;

cout<<"Name : "<<name<<endl;

for(int i=0;i<5;i++)

cout<<"Marks in subject "<<i+1<<":"<<marks[i]<<endl;

cout<<"Percentage : "<<percentage<<endl;

int main()

student s[30];

for(int i=0;i<30;i++)
{

clrscr();

s[i].readdata();

s[i].computedata();

getch();

clrscr();

for(int j=0;j<30;j++)

s[j].displaydata();

getch();

return 0;

}
Program -8

WAP to implement a class using object as a function argument with 3 cases :-

A) Pass by Value

#include<iostream.h>

#include<conio.h>

class Account

unsigned int accno;

char name[20];

float balance;

public:

void getdata()

cout<<"\n\nEnter the Account Number: ";

cin>>accno;

cout<<"\nEnter Customer Name: ";

cin>>name;

cout<<"\nEnter initial Amount: ";

cin>>balance;

void display()

{
cout<<"\nAcoount Number: "<<accno;

cout<<"\nCustomer Name: "<<name;

cout<<"\nBalance Amount: "<<balance;

void transfer(Account a,float amount)

a.balance=a.balance+amount;

};

int main()

clrscr();

Account a1,a2,a3;

float amo;

cout<<"\t\t\t\tPass by value";

a1.getdata();

cout<<"\nEnter the amount to deposit: ";

cin>>amo;

a1.transfer(a1,amo);

a1.display();

getch();

return 0;

}
B) Pass by reference

#include<iostream.h>

#include<conio.h>

class Account

unsigned int accno;

char name[20];

float balance;

public:

void getdata()

cout<<"\n\nEnter the Account Number: ";

cin>>accno;

cout<<"\nEnter Customer Name: ";

cin>>name;

cout<<"\nEnter initial Amount: ";

cin>>balance;

void display()

cout<<"\nAcoount Number: "<<accno;

cout<<"\nCustomer Name: "<<name;


cout<<"\nBalance Amount: "<<balance;

void transfer(Account &a,float amount)

a.balance=a.balance+amount;

};

int main()

clrscr();

Account a1,a2,a3;

float amo;

cout<<"\t\t\t\tPass by Reference";

a1.getdata();

cout<<"\nEnter the amount to deposit: ";

cin>>amo;

a1.transfer(a1,amo);

a1.display();

getch();

return 0;

}
C) Pass by address

#include<iostream.h>

#include<conio.h>

class Account

unsigned int accno;

char name[20];

float balance;

public:

void getdata()

cout<<"\n\nEnter the Account Number: ";

cin>>accno;

cout<<"\nEnter Customer Name: ";

cin>>name;

cout<<"\nEnter initial Amount: ";

cin>>balance;

void display()

cout<<"\nAcoount Number: "<<accno;

cout<<"\nCustomer Name: "<<name;

cout<<"\nBalance Amount: "<<balance;

}
void transfer(Account *a,float amount)

a->balance=a->balance+amount;

};

int main()

clrscr();

Account a1,a2,a3;

float amo;

cout<<"\t\t\t\tPass by value";

a1.getdata();

cout<<"\nEnter the amount to deposit: ";

cin>>amo;

a1.transfer(&a1,amo);

a1.display();

getch();

return 0;

}
Program-9

WAP for addition of two complex numbers using returning object from
function

#include<iostream.h>

#include<conio.h>

class complex

int real;

int img;

public:

void getdata()

cout<<"\nEnter the real part of complex number : ";

cin>>real;

cout<<"\nEnter the imaginary part of complex number : ";

cin>>img;

void display()

cout<<"\nThe sum of complex number is : ";

cout<<real<<" + "<<img<<"i";

complex add(complex c);

};

complex complex::add(complex c)

{
complex temp;

temp.real=real+c.real;

temp.img=img+c.img;

return temp;

int main()

clrscr();

complex c1,c2,c3;

c1.getdata();

c2.getdata();

c3=c1.add(c2);

c3.display();

getch();

return 0;

}
Program – 10

WAP to implement friend function with bridging of two classes

#include<iostream.h>

#include<conio.h>

class account

int salary;

public:

void getdata()

cout<<"\nEnter the salary : ";

cin>>salary;

void show()

cout<<"\nThe salary is : "<<salary;

friend int addbonus(account);

};

int addbonus(account a)

return (a.salary+5000);

int main()

{
clrscr();

account a;

a.getdata();

a.show();

cout<<"\nTotal salary after bonus : "<<addbonus(a);

getch();

return 0;

}
Program-11

WAP to implement friend class

#include<iostream.h>

#include<conio.h>

class ABC

int num;

friend class xyz;

public:

ABC()

num=20;

};

class xyz

int value;

public:

xyz()

value=30;

int add()

{
clrscr();

ABC a;

return a.num+value;

};

int main()

xyz obj;

cout<<"\nThe sum is : "<<obj.add();

getch();

return 0;

}
Program – 12

WAP to calculate square of an integer using inline function

#include<iostream.h>

#include<conio.h>

class number

int num;

public:

number()

cout<<"\nEnter the number : " ;

cin>>num;

inline int square()

return (num*num);

};

int main()

clrscr();

number n;

int val=n.square();

cout<<"\nSquare of number : "<<val;

getch();
return 0;

}
Program – 13

WAP to find the area of various object using function overloading

#include<iostream.h>

#include<conio.h>

float area(float s)

return (s*s);

float area(float l, float b)

return (l*b);

float area(int h,int b)

return (0.5*h*b);

int main()

clrscr();

cout<<"\nArea of square is : "<<area(12.5);

cout<<"\nArea of rectangle is : "<<area(5.5,8);

cout<<"\nArea of triangle is : "<<area(12,20);

getch();

return 0;

}
Program – 14

WAP to implement Dynamic Constructor

#include<iostream.h>

#include<conio.h>

class student

int rollno;

char name[15];

float *marks;

public:

student()

cout<<"\nEnter the roll no : ";

cin>>rollno;

cout<<"\nEnter the name : ";

cin>>name;

marks=new float;

cout<<"\nEnter total marks : ";

cin>>*marks;

void display()

cout<<"\n\nRoll no : "<<rollno;

cout<<"\nName : "<<name;

cout<<"\nTotal marks : "<<*marks;


}

};

int main()

clrscr();

student s1;

s1.display();

getch();

return 0;

}
Program – 15

WAP to perform operator overloading on system operators

#include<iostream.h>

#include<conio.h>

class fraction

float num;

float den;

public:

void get()

cout<<"\nEnter the numerator and denominator : ";

cin>>num>>den;

void show()

cout<<"\nThe fraction is :"<<num<<"/"<<den;

friend fraction operator / (fraction a,fraction b);

};

fraction operator / (fraction a,fraction b)

fraction temp;

temp.num=a.num*b.den;

temp.den=a.den*b.num;
return temp;

int main()

clrscr();

fraction f1,f2,f3;

f1.get();

f2.get();

f3=f1/f2;

f3.show();

getch();

return 0;

}
Program – 16

WAP to implement a class hierarchy using concept of inheritance

#include<iostream.h>

#include<conio.h>

class length

public:

int l;

void len()

cout<<"\nEnter Length : ";

cin>>l;

};

class breadth : public length

public:

int b;

void bre()

cout<<"\nEnter Breadth : ";

cin>>b;

};

class height
{

public:

int h;

void hei()

cout<<"\nEnter the height : ";

cin>>h;

};

class result:public breadth,public height

public:

int res;

void value()

len();

bre();

hei();

res=l*b*h;

cout<<"\n\nResult : "<<res;

};

int main()

clrscr();

result r1;

r1.value();
getch();

return 0;

}
Program – 17

WAP to count the number of object created in the program using a static
counter

#include<iostream.h>

#include<conio.h>

class myclass

static int created;

static int alive;

public:

myclass()

created ++;

alive ++;

cout<<"\nObject created : "<<created;

~myclass()

alive--;

cout<<"\nObject alive : "<<alive;

};
int myclass:: created =0;

int myclass::alive=0;

int main()

clrscr();

myclass a1;

myclass a2,a3;

myclass a4,a5;

myclass a6,a7;

myclass a8;

getch();

return 0;

}
Program – 18

WAP to use Virtual Function to show run time polymorphism

#include<iostream.h>

#include<conio.h>

class marks

int internal;

public:

marks()

cout<<"\nEnter the internal marks : ";

cin>>internal;

virtual void show()

cout<<"\nInternal Marks (base classs) : "<<internal;

};

class external : public marks

int ext;

public:

external()

cout<<"\Enter external marks : ";


cin>>ext;

void show()

marks::show();

cout<<"\nExternal marks (derived class) : "<<ext;

};

int main()

clrscr();

marks *p;

external e;

p=&e;

p->show();

getch();

return 0;

}
Program – 19

WAP to perform exceptional handling


#include <iostream>

using namespace std;

int main()

try

int no = 10;

int d;

cin>>d;

int res;

if(d != 0)

res = no/d;

cout<<res;

else

throw(d);

catch (int n)

cout<<"Exception Occured";

return 0;

}
Program – 20

WAP to perform swapping of integer ,double and string using Function


Handling

#include<iostream.h>

#include<conio.h>

template <class t>

void swap(t &x,t &y)

t temp;

temp=x;

x=y;

y=temp;

int main()

clrscr();

int a,b;

double num1,num2;

char str1,str2;

cout<<"\nEnter value of a : ";

cin>>a;
cout<<"\nEnter value of b : ";

cin>>b;

cout<<"\nEnter value of double num1 : ";

cin>>num1;

cout<<"\nEnter value of double num2 :";

cin>>num2;

cout<<"\nEnter value of string 1 : ";

cin>>str1;

cout<<"\nEnter value of string 2 : ";

cin>>str2;

getch();

clrscr();

cout<<"\nBefor swapping ";

cout<<"\nValue of a : "<<a;

cout<<"\nValue of b : "<<b;

cout<<"\nValue of double num1 : "<<num1;

cout<<"\n Value of double num2 : "<<num2;

cout<<"\nValue of string 1 : "<<str1;

cout<<"\nValue of string 2 : "<<str2;

cout<<"\n\nAfter Function Template";

swap(a,b);

swap(num1,num2);

swap(str1,str2);

cout<<"\nValue of a : "<<a;

cout<<"\nValue of b : "<<b;
cout<<"\nValue of double num1 : "<<num1;

cout<<"\n Value of double num2 : "<<num2;

cout<<"\nValue of string 1 : "<<str1;

cout<<"\nValue of string 2 : "<<str2;

getch();

return 0;

You might also like