You are on page 1of 20

OOP C++ lab#7

Program no 1
#include<iostream.h>

#include<conio.h>

int sum(int,int);

void product(int a,int b)

cout<<"product of numbers= "<<a*b<<endl;

void main()

clrscr();

int x,y;

cout<<"Enter two integers= "<<endl;

cin>>x>>y;

cout<<"sum of numbers= "<<sum(x,y)<<endl;

product(x,y);

getch();

int sum(int a,int b)

return a+b;

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#7

Output(12-EE-133)

Program no 2
#include<iostream.h>

#include<conio.h>

void sumsquare(int);

int num,sum=0;

int main ()

clrscr();

cout<<"enter an integer=";

cin>>num;

sumsquare(num);

cout<<"Sum of squares is= "<<sum<<endl;

getch();

return 0;

void sumsquare(int)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#7

for (int loop=1;loop<=num;loop++)

sum+=(loop*loop);

Output(12-EE-133)

Program no 3
#include<iostream.h>

#include<conio.h>

void nendl(int n=1);

int main()

clrscr();

cout<<"No. of blank spaces \n";

nendl();

getch();

return 0;

void nendl(int n)

for (int loop=0;loop<n;loop++)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#7

cout<<"*";

Output(12-EE-133)

Program no 4
#include<iostream.h>

#include<conio.h>

float Average(int newdata)

static float total=0.0f;

static int count=0;

count++;

total+=newdata;

return total/count;

void main()

clrscr();

float ave;

int data=1;

while(data!=0)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#7

cout<<"Enter a number -->";

cin>>data;

cout<<"\n New averageis -->"<<Average(data)<<endl;

getch();

Output(12-EE133)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#7

Program no 5
#include<iostream.h>

#include<conio.h>

#include<math.h>

void QE(float,float,float);

int a,b,c;

int s;

void main ()

{ clrscr();

cout<<"argument 1=";

cin>>a;

cout<<"argument 2=";

cin>>b;

cout<<"argument 3=";

cin>>c;

QE(a,b,c);

void QE(float a,float b,float c)

float disc,disc1,disc2;

disc1=b*b;

disc2=4*a*c;

disc=disc1-disc2;

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#7

if(disc>0)

cout<<"real";

cout<<"r1="<<(-b-sqrt(disc))/2*a<<endl;

cout<<"r2="<<(-b+sqrt(disc))/2*a<<endl;

else if(disc<0)

cout<<"imaginary"<<endl;

cout<<"r1="<<-b/(2*a)<<"+i"<<sqrt(-disc)/(2*a)<<endl;

cout<<"r2="<<-b/(2*a)<<"-i"<<sqrt(-disc)/(2*a)<<endl;

else

cout<<"zero";

Output(12-EE-133)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#7

Program no 6
#include<iostream.h>

#include<conio.h>

class greateroftwo

int greater;

int findmax(int i,int j);

public:

void getandcomputemax();

void printmax();

};

void greateroftwo::printmax()

cout<<"the greater of the two integers is "<<greater <<endl;

int greateroftwo::findmax(int a,int b)

return (a>b)?a:b;

void greateroftwo::getandcomputemax()

int c,d;

cout<<"enter two intgers";

cin>>c>>d;

greater=findmax(c,d);

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#7

void main()

greateroftwo test;

clrscr();

test.getandcomputemax();

test.printmax();

getch();

/*void main()

clrscr();

greateroftwo test;

clrscr();

cout<<test.findmax(23,234);

getch();

void main()

greateroftwo test;

clrscr();

test.getandcomputemax();

cout<<test.greater;getch();

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#7

Output

Program no 7
#include <iostream.h>

#include <math.h>

#include<conio.h>

int main(void)

clrscr();

int a;

float b;

long int c;

cout<<"Enter integer value=";

cin>>a;

cout<<"Enter float value value=";

cin>>b;

cout<<"Enter long integer value=";

cin>>c;

cout<<"Absolute value of "<<a<<"="<<abs(a)<<endl;

cout<<"Absolute value of "<<b<<"="<<fabs(b)<<endl;

cout<<"Absolute value of "<<c<<"="<<labs(c)<<endl;

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#7

getch();

return 0;

Output

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#8

Program no1
#include<iostream.h>

#include<conio.h>

class C

int a,b;

const int c;

public:

C():a(10),c(30){b=20; cout<<"constructor 1"<<endl;}

C(int d,int e,int f):b(e),c(f){a=d; cout<<"consturctor 2"<<endl;}

~C(){cout<<"destructor "<<endl;}

void print()

{cout<<"a="<<a<<"b="<<b<<"c="<<c<<endl;}

};

void main()

{clrscr();

C c1=C();

C c2=C(4,5,6);

C c3;

C c4(70,80,90);

c1.print();

c2.print();

c3.print();

c4.print();

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#8

getch();

Output(12-EE-133)

Programno2
# include<iostream.h>

# include<conio.h>

class Rectangle

{ int l, w;

public:

Rectangle(int a, int b)

{ l=a; w=b; cout<<"This is a constructor"<<endl;}

~ Rectangle(){ cout<<"This is a destructor\n\tGOOD BYE!!"<<endl;}

void gdata()

{ cout<<"Enter Length:";

cin>>l;

cout<<"Enter Width:";

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#8

cin>>w;

void perimeter()

{ float peri;

peri=2*(l+w);

cout<<"Perimeter="<<peri<<endl;

void area()

{ float area;

area=l*w;

cout<<"Area="<<area<<endl;

};

void main()

{ Rectangle rect=Rectangle(2,3);

clrscr();

rect.perimeter();

rect.area();

getch();

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#8

Output(12-EE-133)

Program no 4

# include<iostream.h>

# include<conio.h>

int smallest(int []);

void main()

{ int num[5]={3,5,1,2,0};

clrscr();

cout<<"Smallest value is="<<smallest(num);

getch();

int smallest(int num[5])

{ int small;

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

if( num[i]<num[i+1])

small=num[i];

return small;

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#8

Output(12-EE-133)

Program no 5

#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int a[2][2][2];

for (int loop1=0;loop1<2;loop1++)

for (int loop2=0;loop2<2;loop2++)

for (int loop3=0;loop3<2;loop3++)

cout<<"enter next=";

cin>>a[loop1][loop2][loop3];

for (loop1=0;loop1<2;loop1++)

for (loop2=0;loop2<2;loop2++)

for (loop3=0;loop3<2;loop3++)

cout<<" x= "<<loop1

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#8

<<"\t y= "<<loop2

<<"\t z= "<<loop3

<<"\t "<<a[loop1][loop2][loop3]<<endl;

getch();

return 0;

Output

Program no 7

#include<iostream.h>

#include<conio.h>

#include<ctype.h>

main()

clrscr();

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#8

char ch;

cout<<"Enter any string of any length ? \n";

cin>>ch;

int digits=0,small=0,line=0,space=0,caps=0;

while(cin.get(ch),!cin.eof())

if (isdigit(ch))

digits++;

if (isupper(ch))

caps++;

if (isspace(ch) && !isprint(ch))

line++;

if (islower(ch))

small++;

if (isspace(ch))

space++;

cout<<"small case letters ="<<small<<endl

<<"capital letters ="<<caps<<endl

<<"digits ="<<digits<<endl

<<"spaces ="<<space-line<<endl

<<"line ="<<line<<endl;

getch();

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#8

return 0;

Output

Program no 8

#include<iostream.h>

#include<conio.h>

#include<ctype.h>

int main()

clrscr();

char ch,ch1;

cout<<"Enter Chracter=";

cin>>ch;

if (ch<='z' && ch>='a')

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#8

ch1=toupper(ch);

cout<<"Upper case="<<ch1;

getch();

return 0;

Output

Sohail Akhtar 12-EE-133 A2

You might also like