Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
118 views

C++ Lab Manual

The document contains source code for 17 C programming examples including: adding two numbers, backslash character, checking odd or even number, sum of digits, counting digits, counting months and days, finding the biggest of three numbers, converting Celsius to Fahrenheit, checking prime number, reversing digits, Adam number, Armstrong number, searching a number, recursive factorial, Fibonacci series, and checking leap year. Each example provides the problem statement, algorithm, source code, sample output and result.

Uploaded by

lakshmi.s
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

C++ Lab Manual

The document contains source code for 17 C programming examples including: adding two numbers, backslash character, checking odd or even number, sum of digits, counting digits, counting months and days, finding the biggest of three numbers, converting Celsius to Fahrenheit, checking prime number, reversing digits, Adam number, Armstrong number, searching a number, recursive factorial, Fibonacci series, and checking leap year. Each example provides the problem statement, algorithm, source code, sample output and result.

Uploaded by

lakshmi.s
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 153

CONTENT

PROGRAMMING IN C
S.NO DATE PROGRAMS PAGE.NO SIGNATURE
1 ADDING TWO NUMBERS
2 BACKSLASH CHARACTER

3 ODD OR EVEN NUMBER


4 SUM OF DIGIT

5 COUNTING THE DIGIT


6 COUNTING MONTH AND DAYS
7 BIGGEST OF THREE NUMBERS

8 CONVERT CELSIUS TO FAHRENHEIT


9 PRIME NUMBER
10 REVERSE THE DIGIT
11 ADAM NUMBER
12 ARMSTRONG NUMBER
13 SEARCHING A NUMBER
14 RECURSION OF FACTORIAL
15 FIBONACCI SERIES
16 FIND THE LEAP YEAR
17 SIMPLE AND COMPOUND INTEREST

18 ARITHMETIC CALCULATION
19 ASCENDING ORDER

20 AREA OF CIRCLE
DATA STRUCTURE WITH C++
S.NO DATE PROGRAMS PAGE.NO SINGATURE
21 SCOPE RESOLUTION OPERATER
22 USE OF CLASS
23 CONSTRUCTOR
24 DESTRUCTOR
25 FRIEND FUNCTION
26 VIRTUAL FUNCTION
27 NESTING OF MEMBER FUNCTION
28 INLINE FUNCTION
29 FUNCTION OVERLODING
30 OPERATOR OVERLODING
31 THIS POINTER
32 INPUT OUTPUT MANIPULATION
33 DEFAULT ARGUMENT
34 SINGLE INHERITANCE
35 MULTILEVEL INHERITANCE
36 MULTIPLE INHERITANCE
37 HIERARCHICAL INHERITANCE
38 HYBRID INHERITANCE
39 STACK USING ARRAY
40 QUEUE USING ARRAY
41 SINGLY LINKED LIST
42 DOUBLY LINKED LIST
43 BINARY TREE TRAVERSAL
EX.NO:1
ADDING TWO NUMBERS
DATE:

AIM:

ALGORITHM:

1
SOURCE CODE:
#include<stdio.h>

#include<conio.h>

void main()

int x,y,z;

clrscr();

printf("\n Enter the value x:");

scanf("%d",&x);

printf("\n enter the value y:");

scanf("%d",&y);

z=x+y;

printf("\n The sum of value %d",z);

getch();

2
OUTPUT:

RESULT:

3
EX.NO:2
BACKSLASH CHARACTER CONSTANT
DATE :

AIM:

ALGORITHM:

4
SOURCE CODE:
#include<stdio.h>

#include<conio.h>

void main()

clrscr();

printf("Back slash character");

printf("\n\t\t1.AUDIBLE ALERT (BELL) \a");

printf("\n\t\t\t2.BACK SPACE \b");

printf("\n\t\t3.FORMFEED \f");

printf("\n\t\t4.NEW LINE \n");

printf("\n\t\t5.CARRIAGE");

printf("\n\t\t6.HORIZONLTAB \t");

printf("\n\t\t7.VERTICAL TAB \v");

printf("\n\t\t8.SINGLE QUOTE \'");

printf("\n\t\t9.DOUBLE QUOTE \"");

printf("\n\t\t\t10.QUESTION MARK \?");

printf("\n\t\t\t11.BACK SLASH \\");

printf("\n\t\t\t12.NULL\0");

getch();

5
OUTPUT:

RESULT:

6
EX.NO:3

CHECK ODD OR EVEN


DATE:

AIM:

ALGORITHM:

7
SOURCE CODE:
#include<stdio.h>

#include<conio.h>

void main()

int num;

clrscr();

printf("enter a Number");

scanf("%d",&num);

if(num%2==0)

printf("The given number%d even",num);

else

printf("The given number%d odd",num);

getch();

8
OUTPUT:

RESULT:

9
EX.NO:4

SUM OF DIGIT
DATE:

AIM:

ALGORITHM:

10
SOURCE CODE:
#include<stdio.h>

#include<conio.h>

void main()

int n,r,s;

clrscr();

printf("\n\n\t\t Enter the value for n:");

scanf("%d",&n);

s=0;

while(n!=0)

r=n%10;

s=s+r;

n=n/10;

printf("\n\t\tSum of digit=%d",s);

getch();

11
OUTPUT:

RESULT:

12
EX.NO:5
COUNT THE DIGIT
DATE:

AIM:

ALGORITHM:

13
SOURCE CODE:
#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

int n,r,c;

clrscr();

printf("Input\n");

printf("Enter the number:");

scanf("%d",&n);

c=0;

while(n>0)

r=n%10;

c=c+1;

n=n/10;

printf("output\n");

printf("count the digit:%d",c);

getch();

14
OUTPUT:

RESULT:

15
EX.NO:6

DATE: COUNTING MONTHS&DAYS

AIM:

ALGORITHM:

16
SOURCE CODE:
#include<stdio.h>

#include<conio.h>

void main()

int months,days;

clrscr();

printf("Enter the days\n");

scanf("%d",&days);

months=days/30;

days=days%30;

printf("months=%d days=%d",months,days);

getch();

17
OUTPUT:

RESULT:

18
EX.NO:7

BIGGEST OF THREE NUMBER


DATE:

AIM:

ALGORITHM:

19
SOURCE CODE:
#include<stdio.h>

#include<conio.h>

void main()

int a,b,c;

clrscr();

printf("Enter the three number:");

scanf("%d%d%d",&a,&b,&c);

printf("the biggest number is:");

if(a>b&&a>c)

printf("%d\n",a);

else

if(b>c)

printf("%d\n",b);

else

printf("%d\n",c);

getch();

20
OUTPUT:

RESULT:

21
EX.NO:8

CONVERT CELSIUS TO FAHRENHIET


DATE:

AIM:

ALGORITHM:

22
SOURCE CODE:
#include<stdio.h>

#include<conio.h>

void main()

float centi,faren;

clrscr();

printf("\n Enter temperature in centigrade");

scanf("%f",&centi);

faren=(1.8*centi+32);

printf("\n\n The temperatature in fahrenheit is %7.2f",faren);

getch();

23
OUTPUT:

RESULT:

24
EX.NO:9

PRIME NUMBER
DATE:

AIM:

ALGORITHM:

25
SOURCE CODE:
#include<stdio.h>

#include<conio.h>

void main()

int n,i,p,q;

p=0;

clrscr();

printf("Enter the value of n:");

scanf("%d",&n);

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

q=n%i;

if(q==0)

p++;

if(p==0)

printf("The given number is prime number");

else

printf("The given number is not prime number");

getch();

26
OUTPUT:

RESULT:

27
EX.NO:10
REVERSE THE DIGIT
DATE:

AIM:

ALGORITHM:

28
SOURCE CODE:
#include<stdio.h>

#include<conio.h>

void main()

int s,r,n;

clrscr();

printf("\n\t Enter the number:");

scanf("%d",&n);

s=0;

while(n!=0)

r=n%10;

s=s*10+r;

n=n/10;

printf("\n\t Reverse the digit is:");

printf("%d",s);

getch();

29
OUTPUT:

RESULT:

30
EX.NO:11

ADAM NUMBER
DATE:

AIM:

ALGORITHM:

31
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a,sum=0,d=0,b,m,s;
clrscr();
printf("\n\t Enter the number:");
scanf("%d",&n);
a=n*n;
while(n!=0)
{
b=n%10;
sum=sum*10+b;
n=n/10;
}
s=sum*sum;
while(s!=0)
{
m=s%10;
d=d*10+m;
s=s/10;
}
if(d==a)
{
printf("\nThe given number is an adam number");
}
else
{
printf("\nThe given number is not an adam number");
}
getch();
}

32
OUTPUT:

RESULT:

33
EX.NO:12

ARMSTRONG NUMBER
DATE:

AIM:

ALGORITHM:

34
SOURCE CODE:
#include<stdio.h>

#include<conio.h>

void main()

int n,x,y,sum=0;

clrscr();

printf("\n\n\t enter the value of n:");

scanf("%d",&n);

y=n;

while(n!=0)

x=n%10;

sum=sum+(x*x*x);

n=n/10;

if(y==sum)

printf("\n\n\t The given number is armstong");

else

printf("Thegiven number is not armstong");

getch();

35
OUTPUT:

RESULT:

36
EX.NO:13

SEARCHING NUMBER
DATE:

AIM:

ALGORITHM:

37
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5];
int c,i,j,b=0,n;
clrscr();
printf("\n\n\t\tSEARCHING NUMBER\n");
printf("\n\t\t\t*****************");
printf("\n\t\t\t INPUT");
printf("\n\n\t\t*****************:");
printf("\n\n\t Enter how many number:\t");
scanf("%d",&n);
printf("\n\n\t Enter the number\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n\tEnter Searching number:");
scanf("%d",&c);
for(i=0;i<n;i++)
{
if(c==a[i])
b=1;
}
printf("\n\n\t\t OUTPUT");
printf("\n\n\t\t*******");
if(b==1)
printf("\n\n\t\t***The Number is Present\n");
else
printf("\n\n\t\The number is not present\n");
getch();
}

38
OUTPUT:

RESULT:

39
EX.NO:14

DATE: RECURSION OF FACTORIAL

AIM:

ALGORITHM:

40
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int res,n1,i;
long int factorial(int);
clrscr();
printf("\n\n\t\t\tRecursion of factorial");
printf("\n\t\t\t.........................");
printf("\n\n\n\t\t\tEnter the value\t:");
scanf("%d",&n1);
res=factorial(n1);
printf("\n\n\t\t The factorial value:%d",res);
getch();
}
long int factorial (int n)
{
int i;
long int fact=1;
if(n==0)
return(0);
else
if(n==1)
return(1);
else
{
fact=n*factorial(n-1);
return(fact);
}
}

41
OUTPUT:

RESULT:

42
EX.NO:15
FIBBONACCI SERIES
DATE:

AIM:

ALGORITHM:

43
SOURCE CODE:
#include<stdio.h>

#include<conio.h>

void main()

int i,n,f,f0,f1;

f0=-1;

f1=1;

clrscr();

printf("\n\t\tEnter how many number you want to generate:");

scanf("%d",&n);

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

f=f0+f1;

printf("%d",f);

f0=f1;

f1=f;

getch();

44
OUTPUT:

RESULT:

45
EX.NO:16

FIND THE LEAF YEAR


DATE:

AIM:

ALGORITHM:

46
SOURCE CODE:
#include<stdio.h>

#include<conio.h>

void main()

int year;

clrscr();

printf("\n Enter the year:");

scanf("%d",&year);

if(year%4==0)

printf("The year%d is leap year",year);

else

printf("The year%d is not leap year",year);

getch();

47
OUTPUT:

RESULT:

48
EX.NO:17

ARITHMATIC CALCULATION
DATE:

AIM:

ALGORITHM:

49
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
char code;
clrscr();
printf("\n\t\tEnter calculation code +,-,*,/\n");
scanf("%c",&code);
printf("\n\t\t Enter two number a,b:");
scanf("%f%f",&a,&b);
switch(code)
{
case'+':
c=a+b;
break;
case'-':
c=a-b;
break;
case'*':
c=a*b;
break;
case'/':
c=a/b;
break;
}
printf("\n\t\tResult=%f",c);
getch();
}

50
OUTPUT:

RESULT:

51
EX.NO:18

ASENDING ORDER
DATE:

AIM:

ALGORITHM:

52
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[5],i,j,t;
clrscr();
printf("\n\t\tEnter the value for n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\t\tEnter the value:");
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("\n\t\tAscenting orter:");
for(i=0;i<n;i++)
printf("%d\n\t\t\t",a[i]);
getch();
}

53
OUTPUT:

RESULT:

54
EX.NO:19
AREA OF CIRCLE
DATE:

AIM:

ALGORITHM:

55
SOURCE CODE:
#include<stdio.h>

#include<conio.h>

void areacircle(int r,float *a)

*a=3.14*r*r;

void main()

int radius;

float area;

clrscr();

printf("\n Enter Radius of circle:");

scanf("d",&radius);

areacircle(radius,&area);

printf("\narea=%f",area);

getch();

56
OUTPUT:

RESULT:

57
EX.NO:20

SIMPLE INTEREST AND COMPOUNT


DATE: INTEREST

AIM:

ALGORITHM:

58
SOURCE CODE:
#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

float p,r,t,si,ci,amt;

int n;

clrscr();

printf("Enter principle,rate,time, and year\n");

scanf("%f%f%f%d",&p,&r,&t,&n);

si=(p*r*t)/100;

printf("\nsimple interest=%7.2f",si);

amt=p*pow((double)(1+r/(n*100)),(double)(n*t));

printf("\n The amount is=%d7.2",amt);

ci=amt-p;

printf("\n Compound interest =%7.2f",ci);

getch();

59
OUTPUT:

RESULT:

60
EX.NO:21

SCOPE RESOLUTION OPEARATOR


DATE:

AIM:

ALGORITHM:

61
SOURCE CODE:
#include<iostream.h>

#include<conio.h>

int m=10;

int main()

clrscr();

int m=20;

int k=m;

int m=30;

cout<<"We are inner block\n";

cout<<"k="<<k<<"\n";

cout<<"m="<<m<<"\n";

cout<<"::m="<<::m<<"\n";

cout<<"\nWe are in outer block\n";

cout<<"m="<<m<<"\n";

cout<<"::m="<<::m<<"\cn";

getch();

return 0;

62
OUTPUT:

RESULT:

63
EX.NO:22

DATE: USE OF CLASS

AIM:

ALGORITHM:

64
SOURCE CODE:

#include<iostream.h>
#include<conio.h>
class person
{
char name[30];
int age;
public:
void getdata(void);
void display(void);
};
void person::getdata(void)
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter age:";
cin>>age;
}
void person::display(void)
{
cout<<"\nName:"<<name;
cout<<"\nAge:"<<age;
}
int main()
{
person p;
p.getdata();
p.display();
return 0;
}

65
OUTPUT:

RESULT:

66
EX.NO:23

CONSTRUCTOR
DATE:

AIM:

ALGORITHM:

67
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class integer
{
int m,n;
public:
integer(int,int);
void display(void)
{
cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
}
};
integer::integer(int x,int y)
{
m=x,n=y;
}
int main()
{
integer int1(0,100);
integer int2=integer(25,75);
clrscr();
cout<<"\n OBJECT1"<<"\n";
int1.display();
cout<<"\n OBJECT2"<<"\n";
int2.display();
getch();
return(0);
}

68
OUTPUT:

RESULT:

69
EX.NO:24

DESTRUCTOR
DATE:

AIM:

ALGORITHM:

70
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
int count=0;
class alpha
{
public:
alpha()
{
count++;
cout<<"\n No of object destroyed:"<<count;
}
~alpha()
{
cout<<"\n No of object destroyed:"<<count;
count--;
}
};
int main()
{
cout<<"\n\n ENTER MAIN";
alpha A1,A2,A3,A4;
{
cout<<"\n\nENTER BLOCK1\n";
alpha A5;
}
{
cout<<"\n\nENTER BLOCK2\n";
alpha A6;
}
cout<<"\n\n REENTER MAIN\n";
getch();
return(0);
}

71
OUTPUT:

RESULT:

72
EX.NO:25

FRIEND FUNCTION
DATE:

AIM:

ALGORITHM:

73
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class sample
{
int a;
int b;
public:
void setvalue()
{
a=25;
b=40;
}
friend float mean(sample s);
};
float mean(sample s)
{
return float(s.a+s.b)/2.0;
}
int main()
{
clrscr();
sample x;
x.setvalue();
cout<<"\n\t mean value="<<mean(x)<<"\n";
getch();
return(0);
}

74
OUTPUT:

RESULT:

75
EX.NO:26

DATE: VIRTUAL FUNCTION

AIM:

ALGORITHM:

76
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"\ndisplay base";
}
virtual void show()
{
cout<<"\n Show base";
}
};
class derived:public base
{
public:
void display()
{
cout<<"\n display derived";
}
void show()
{
cout<<"\n show derived";
}
};
int main()
{
clrscr();
base b;
derived d;
base*bptr;
cout<<"\n\t bptr pointer to base\n";
bptr=&b;
bptr->display();
bptr->show();
cout<<"\n\nbptr pointer to derived\n";
bptr=&d;
bptr->display();
bptr->show();
getch();
return(0);
}

77
OUTPUT:

RESULT:

78
EX.NO:27

NESTING OF MEMBER FUNCTION


DATE:

AIM:

ALGORITHM:

79
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class set
{
int m,n;
public:
void input(void);
void display(void);
int largest(void);
};
int set::largest(void)
{
if(m>=n)
return(m);
else
return(n);
}
void set::input(void)
{
cout<<"\n\tINPUT VALUES OF M:";
cin>>m;
cout<<"\n\tOUTPUT VALUES OF N:";
cin>>n;
}
void set::display(void)
{
cout<<"\n\tLARGEST VALUES="<<largest()<<"\n";
}
int main()
{
clrscr();
set a;
a.input();
a.display();
getch();
return(0);
}

80
OUTPUT:

RESULT:

81
EX.NO:28

INLINE FUNCTION
DATE:

AIM:

ALGORITHM:

82
SOURCE CODE:
#include<iostream.h>

#include<conio.h>

inline float largest(int a,intb,int c)

cout<<"\n\t\tlargest number is\t";

if(a>b&&a>c)

return(a);

if(b>c&&b>a)

return(b);

else

return(c);

int main()

int a,b,c;

clrscr();

cout<<"\t\tINLINE FUNCTION\n";

cout<<"\nEnter three no to be checked:\n";

cin>>a>>b>>c;

cout<<largest(a,b,c);

getch();

return(0);

83
OUTPUT:

RESULT:

84
EX.NO:29

FUNCTION OVERLOADING
DATE:

AIM:

ALGORITHM:

85
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
int vol(int);
double vol(double,int);
long vol(long,int,int);
int main()
{
clrscr();
cout<<"\n\tfunction overloading\n";
cout<<"\n\t***********************\n\n";
cout<<vol(10)<<"\n\n";
cout<<vol(2.5,8)<<"\n";
cout<<vol(100,75,15);
getch();
return(0);
}
int vol(int s)
{
return(s*s*s);
}
double vol(double r,int n)
{
return(3.14519*r*r*n);
}
long vol(long l,intb,int n)
{
return(l*b*n);
}

86
OUTPUT:

RESULT:

87
EX.NO:30
OPERATOR OVERLOADING
DATE:

AIM:

ALGORITHM:

88
SOURCE CODE:
#include<iostream.h>

#include<conio.h>

class complex

float x,y;

public:

complex()

complex(float real,float image)

x=real;y=image;

complex operator+(complex);

void display(void);

};

complex complex::operator+(complex c)

complex temp;

temp.x=x+c.x;

temp.y=y+c.y;

return(temp);

void complex::display(void)

cout<<x<<"+j"<<y<<"\n";

89
}

int main()

clrscr();

complex c1,c2,c3;

c1=complex(2.5,3.5);

c2=complex(1.6,2.7);

c3=c1+c2;

cout<<"\n\tOperatoroverloding";

cout<<"\n\t*********************";

cout<<"\n\n c1=";

c1.display();

cout<<"\n\n c2=";

c2.display();

cout<<"\n\n c3=";

c3.display();

getch();

return(0);

90
OUTPUT:

RESULT:

91
EX.NO:31
THIS POINTER
DATE:

AIM:

ALGORITHM:

92
SOURCE CODE:
#include<iostream.h>

#include<conio.h>

#include<string.h>

class person

char name[20];

float age;

public:

person(char*s,float a)

strcpy(name,s);

age=a;

person&person::greator(person&x)

if(x.age>=age)

return x;

else

return*this;

void display(void)

cout<<"NAME:"<<name<<"\n"<<"AGE:"<<age<<"\n";

};

int main()

93
{

clrscr();

person p1("john",37.50),

p2("ahmad",29.0),

p3("hebber",40.25);

person p=p1.greator(p3);

cout<<"elder person is:\n";

p.display();

p=p1.greator(p2);

cout<<"elder person is:\n";

p.display();

getch();

return(0);

94
OUTPUT:

RESULT:

95
EX.NO:32

INPUT OUTPUT MANIPULATOR


DATE:

AIM:

ALGORITHM:

96
SOURCE CODE:
#include<iostream.h>

#include<conio.h>

#include<iomanip.h>

int main()

clrscr();

int f=1,i,n;

cout<<"\n INPUT OUTPUT MANIPULATORS \n";

cout<<"\n\t************************";

cout<<"\n\n enter the number:";

cin>>n;

cout<<setw(5)<<"\n\t NUMBER"<<setw(10)<<"\t FACTORIAL VALUE""\n\n\n";

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

f=f*i;

cout<<setw(5)<<"\t"<<i<<"!"<<setw(10)<<"\t"<<f<<"\n";

getch();

return(0);

97
OUTPUT:

RESULT:

98
EX.NO:33

DEFAULT ARGUMENT
DATE:

AIM:

ALGORITHM:

99
SOURCE CODE:
#include<iostream.h>

#include<conio.h>
#include<math.h>
class defa
{
int m,n,r;
public:
void put();
void compute(int,int=4);
};
void defa::compute(int m,int n)
{
r=pow(m,n);
}
void defa::put()
{
cout<<r;
}
void main()
{
clrscr();
int m;
defa d;
cout<<"\n DEFAULT ARGUMENT";
cout<<"\n******************";
cout<<"\n enter the value:";
cin>>m;
d.compute(m);
cout<<"\n THE VALUE IS:";
d.put();
getch();
}

100
OUTPUT:

RESULT:

101
EX.NO:34

SINGLE INHERITANCE
DATE:

AIM:

ALGORITHM:

102
SOURCE CODE:
#include<iostream.h>

#include<conio.h>

class B

int a;

public:

int b;

void set_ab();

int get_a(void);

void show_a(void);

};

class D:public B

int c;

public:

void mul(void);

void display(void);

};

void B::set_ab(void)

a=5;b=10;

int B::get_a()

return a;

103
void B::show_a()

cout<<"a="<<a<<"\n";

void D::mul()

c=b*get_a();

void D::display()

cout<<"a="<<get_a()<<"\n";

cout<<"b="<<b<<"\n";

cout<<"c="<<c<<"\n\n";

int main()

clrscr();

D d;

d.set_ab();

d.mul();

d.show_a();

d.display();

d.b=20;

d.display();

getch();

return(0);

104
OUTPUT:

RESULT:

105
EX.NO:35
MULTILEVEL INHERITANCE
DATE:

AIM:

ALGORITHM:

106
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno;
public:
void get_no(int);
void put_no(void);
};
void student::get_no(int a)
{
rno=a;
}
void student::put_no()
{
cout<<"\n\n\t roll number:\t"<<rno<<endl;
}
class test:public student
{
protected:
int sub1;
int sub2;
int sub3;
public:
void get_marks(int,int,int);
void put_marks(void);
};
void test::get_marks(int x,inty,int z)
{
sub1=x;
sub2=y;
sub3=z;
}
void test::put_marks()
{
cout<<"\n\n\t marks in sub1:"<<sub1<<"\n";
cout<<"\n\t marks in sub2:"<<sub2<<"\n";
cout<<"\n\t marks in sub3:"<<sub3<<"\n";
}
class result:public test
{

107
float total;
public:
void display(void);
};
void result::display(void)
{
total=sub1+sub2+sub3;
put_no();
put_marks();
cout<<"\n\n\tTotal="<<total<<"\n";
}
int main()
{
clrscr();
cout<<"\n\t\tMULTILEVEL INHERITANCE";
cout<<"\n\t\t**********************";
result res;
res.get_no(123);
res.get_marks(90,80,70);
res.display();
getch();
return(0);
}

108
OUTPUT:

RESULT:

109
EX.NO:36

MULTIPLE INHERITANCE
DATE:

AIM:

ALGORITHM:

110
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class M
{
protected:
int m;
public:
void get_m(int);
};
class N
{
protected:
int n;
public:
void get_n(int);
};
class P:public M,public N
{
public:
void display(void);
};
void M::get_m(int x)
{
m=x;
}
void N::get_n(int y)
{
n=y;
}
void P::display(void)
{
cout<<"\n\n\t\tThe value of m and n are:";

111
cout<<"\n\n\t\tm="<<m;
cout<<"\n\n\t\tn="<<n;
cout<<"\n\n\t\tAddition";
cout<<"\n\n\t\tm+n="<<m+n;
cout<<"\n\n\t\tSubtraction";
cout<<"\n\n\t\tm-n="<<m-n;
cout<<"\n\n\t\tMultiplication";
cout<<"\n\n\t\tm*n="<<m*n;
cout<<"\n\n\t\tDivision";
cout<<"\n\n\t\tm/n="<<m/n;
}
int main()
{
clrscr();
cout<<"\n\nMULTIPLE INHERITANCE";
cout<<"\n\n*********************";
P p;
p.get_m(50);
p.get_n(2);
p.display();
getch();
return(0);
}

112
OUTPUT:

RESULT:

113
EX.NO:37
HIERARCHICAL INHERITANCE
DATE:

AIM:

ALGORITHM:

114
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class publish
{
char title[50];
int noitem;
public:
void get();
void disp();
};
class book:public publish
{
int pages;
public:
void get1();
void disp1();
};
class tape:public publish
{
int length;
public:
void get2();
void disp2();
};
void publish::get()
{
cout<<"\n\nEnter the title:";
cin>>title;
cout<<"\n\nEnter the no of item:";
cin>>noitem;
}
void publish::disp()
{
cout<<"\n\tTitle:"<<title;
cout<<"\n\tno of item:"<<noitem;
}
void book::get1()
{
get();
cout<<"\n\nEnter the book pages:";
cin>>pages;

115
}
void book::disp1()
{
disp();
cout<<"\n\tPages:"<<pages;
}
void tape::get2()
{
get();
cout<<"\n\n Enter the tape length:";
cin>>length;
}
void tape::disp2()
{
disp();
cout<<"\n\n\ttape length:"<<length;
}
void main()
{
book b1;
tape b2;
clrscr();
cout<<"\n\t\tHIERARCHICAL INHERITANCE";
cout<<"\n\t\t........................\n\n";
b1.get1();
b1.disp1();
cout<<"\n\n\tTAPE";
b2.get2();
b2.disp2();
getch();
}

116
OUTPUT:

RESULT:

117
EX.NO:38

HYBRID INHERITANCE
DATE:

AIM:

ALGORITHM:

118
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollno;
public:
void get_no(int a)
{
rollno=a;
}
void put_no(void)
{
cout<<"\n\n\n\n\tRoll no:"<<rollno<<"\n";
}
};
class test:public student
{
protected:
float part1,part2;
public:
void get_marks(float x,float y)
{
part1=x;
part2=y;
}
void put_marks(void)
{
cout<<"\n\tMARKS OBTAINED:\n";
cout<<"\n\t*****************";
cout<<"\n\n\tpart1="<<part1;
cout<<"\n\n\tpart2="<<part2;
}
};
class sports
{
protected:
float score;
public:
void get_score(float s)
{
score=s;

119
}
void put_score(void)
{
cout<<"\n\n\n\tsportswt:"<<score<<"\n";
}
};
class result:publictest,public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_no();
put_marks();
put_score();
cout<<"\n\t\ttotal score:"<<total<<"\n";
}
int main()
{
result r;
clrscr();
cout<<"\n\t\tHYBRID INHERITANCE:";
cout<<"\n\t\t******************";
r.get_no(1234);
r.get_marks(27.5,53.0);
r.get_score(6.0);
r.display();
getch();
return(0);
}

120
OUTPUT:

RESULT:

121
EX.NO:39
STACK USING ARRAY
DATE:

AIM:

ALGORITHM:

122
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
int top=0,val;
class stack
{
int s[10];
public:
void create();
void push();
void pop();
void peek();
void disp();
};
void stack::create()
{
int n,i;
cout<<"\n\n\tEnter The Limit:";
cin>>n;
if(top>n)
{
cout<<"\n\tSTACK IS FULL:";
}
else
{
cout<<"\n\n\tEnter The Elements:";
for(i=0;i<n;i++)
{
top++;
cin>>val;
s[top]=val;
}
}
}
void stack::push()
{
if(top>9)
{
cout<<"\n\tSTACK is full";
}
else
{
top++;

123
cout<<"\n\n\tEnter The Elements:";
cin>>val;
s[top]=val;
}}
void stack::pop()
{
if(top<0)
{
cout<<"\n\tSTACK IS EMPTY";
}
else
{
val=s[top];
cout<<"\n\t\tpoped value is:"<<val;
top--;
}
}
void stack::peek()
{
if(top<0)
{
cout<<"\n\t STACK IS EMPTY";
}
else
{
val=s[top];
cout<<"\n\n\t The Top Most Element is"<<val;
}
}
void stack::disp()
{
for(int i=top;i>=1;i--)
{
cout<<"\n\t"<<s[i];
cout<<"\n";
}
}
void main()
{
stack s;
int c;
clrscr();
do
{

124
cout<<"\n\t\t\tSTACK IMPLEMENTATION";
cout<<"\n\t\t\t*********************";
cout<<"\n\t\t\t::::::1.CREATE::::::";
cout<<"\n\t\t\t::::::2.PUSH::::::";
cout<<"\n\t\t\t::::::3.POP::::::";
cout<<"\n\t\t\t::::::4.PEEK::::::";
cout<<"\n\t\t\t::::::5.DISPLAY::::::";
cout<<"\n\t\t\t::::::6.EXIT::::::";
cout<<"\n\tEnter Your Choice:";
cin>>c;
switch(c)
{
case 1:
s.create();
break;
case 2:
s.push();
break;
case 3:
s.pop();
break;
case 4:
s.peek();
break;
case 5:
s.disp();
break;
case 6:
break;
}
}
while(c!=6);
getch();
}

125
OUTPUT:

126
127
RESULT:

128
EX.NO:40
QUEUE USING ARRAY
DATE:

AIM:

ALGORITHM:

129
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
int re=0,fr=0,q[10];
class queue
{
int val;
public:
void create();
void add();
void del();
void peek();
void disp();
};
void queue::create()
{
int n,i;
cout<<"\n\tENTER THE LIMIT:";
cin>>n;
if(re>n)
cout<<"\t\tQUEUE IS FULL\n";
else
cout<<"\t\tENTER THE ELEMENTs:";
for(i=0;i<n;i++)
{
cin>>val;
q[re]=val;
re++;
}
}
void queue::add()
{
if(re>9)
{
cout<<"QUEUE IS FULL\n";
}
else
{
cout<<"\n\tENTER THE ELEMENTS";
cin>>val;
cout<<val;
q[re]=val;
re++;

130
}
if(fr<0)
fr=fr+1;
}
void queue::del()
{
if(fr<0)
cout<<"\t\tQUEUE IS EMPTY\n";
else
{
val=q[fr];
cout<<"\t\tDELETE VALUE IS:"<<val;
fr++;
}
}
void queue::peek()
{
if(fr<0)
cout<<"\tQUEUE IS EMPTY\n";
else
{
val=q[fr];
cout<<"\nThe first element:"<<val;
}
}
void queue::disp()
{
for(int i=fr;i<re;i++)
{cout<<q[i];
cout<<"\n";
}
}
void main()
{
queue q;
int c;
clrscr();
do
{
cout<<"\n\tQUEUE IMPLIMENTATION";
cout<<"\n\t::::::1.create:::::::";
cout<<"\n\t::::::2.add:::::::";
cout<<"\n\t::::::3.delete::::::";
cout<<"\n\t::::::4.peek::::::";

131
cout<<"\n\t::::::5.display::::::";
cout<<"\n\t::::::6.exit::::::";
cout<<"ENTER UR CHOICE:";
cin>>c;
switch(c)
{
case 1:
q.create();
break;
case 2:
q.add();
break;
case 3:
q.del();
break;
case 4:
q.peek();
break;
case 5:
q.disp();
break;
case 6:
break;
}
}
while(c!=6);
getch();
}

132
OUTPUT:

RESULT:

133
EX.NO:41

SINGLY LINKED LIST


DATE:

AIM:

ALGORITHM:

134
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
struct node
{
int val;
struct node*next;
};
typedefstruct node nodes;
nodes*head;
class list
{
nodes*p,*q;
public:
void create();
void ins();
void del();
void disp();
int cnode();
};
void list::create()
{
int nval;
if(head==NULL)
{
head=new(nodes);
cout<<"\n\tEnter the value";
cin>>nval;
head->val=nval;
head->next=NULL;
}
else
{
p=head;
while(p->next!=NULL)
p=p->next;
cout<<"\n\t\tEnter the value";
cin>>nval;
q=new(nodes);
q->val=nval;
q->next=NULL;

135
q->next=q;
}
}
void list::disp()
{
p=head;
while(p!=NULL)
{
cout<<p->val;
p=p->next;
if(p!=NULL)
cout<<"-->";
else
cout<<"\n-->NULL";
}
getch();
}
int list::cnode()
{
int c=0;
p=head;
while(p!=NULL)
{
c++;
p=p->next;
}
return(c);
}
void list::del()
{
int nv,mnode,i;
mnode=cnode();
cout<<"\n\tTOTAL NODES"<<mnode;
cout<<"\n\tWhitch nodes do u want to delete";
cin>>nv;
if(head==NULL)
{
cout<<"\n\tList is empty";
}
if(nv==1)
{
p=head;
head->next;
free(q);

136
}
else
{
p=head;
for(i=1;i<nv-i;i++)
p=p->next;
q=p->next;p->next=q->next;
free(q);
}
}
void list::ins()
{
int nv,i,nval;
cout<<"\n\tEnter the position";
cin>>nv;
if(nv==i)
{
cout<<"\n\tEnter the value";
cin>>nval;
p=new(nodes);
p->val=nval;
p->next=head;
head=p;
}
else
{
cout<<"Enter the value";
cin>>nval;
p=head;
for(i=1;i<nv-i;i++)
{
p=p->next;
}
q=new(nodes);
q->val=nval;
q->next=NULL;
q->next=p->next;
p->next=q;
}
}
void main()
{
list l;
int c;

137
clrscr();
cout<<"\n\tSINGLY LINKERD LIST";
cout<<"\n\t******************";
do
{
cout<<"\n\t1.CREATE\t";
cout<<"\n\t2.INSERT\t";
cout<<"\n\t3.DELETE\t";
cout<<"\n\t4.DISPLAY\t";
cout<<"\n\t5.exit\t";
cout<<"\n\tEnter your choice:\n";
cin>>c;
switch(c)
{
case 1:
l.create();
break;
case 2:
l.ins();
break;
case 3:
l.del();
break;
case 4:
l.disp();
break;
case 5:
break;
}
}
while(c!=5);
getch();
}

138
OUTPUT:

139
RESULT:

140
EX.NO:42
DOUBLY LINNKED LIST
DATE:

AIM:

ALGORITHM:

141
SOURCE CODE:
#include<iostream>
using namespace std;
struct list
{
struct list *prev;
int data;
struct list *next;
}
*node = NULL, *first = NULL, *last = NULL, *node1 = NULL, *node2 = NULL;

class linkedlist
}
public:
void insert_beginning()
{
list *addBeg = new list;
cout<< "Enter value for the node:" <<endl;
cin>>addBeg->data;
if(first == NULL)
{
addBeg->prev = NULL;
addBeg->next = NULL;
first = addBeg;
last = addBeg;
cout<< "Linked list Created!" <<endl;
}
else
{
addBeg->prev = NULL;
first->prev = addBeg;
addBeg->next = first;
first = addBeg;
cout<< "Data Inserted at the beginning of the Linked list!" <<endl;
}
}

void insert_end()
{
list *addEnd = new list;
cout<< "Enter value for the node:" <<endl;
cin>>addEnd->data;
if(first == NULL)
}
addEnd->prev = NULL;
addEnd->next = NULL;
first = addEnd;
last = addEnd;
cout<< "Linked list Created!" <<endl;
}
else
{

142
addEnd->next = NULL;
last->next = addEnd;
addEnd->prev = last;
last = addEnd;
cout<< "Data Inserted at the end of the Linked list!" <<endl;
}
}

void display()
{
node = first;
cout<< "List of data in Linked list in Ascending order!" <<endl;
while(node != NULL)
{
cout<< node->data <<endl;
node = node->next;
}
node = last;
cout<< "List of data in Linked list in Descending order!" <<endl;
while(node != NULL)
{
cout<< node->data <<endl;
node = node->prev;
}
}
void del()
{
int count = 0, number, i;
node = node1 = node2 = first;
for(node = first; node != NULL; node = node->next)
cout<< "Enter value for the node:" <<endl;
count++;
display();
cout<< count << " nodes available here!" <<endl;
cout<< "Enter the node number which you want to delete:" <<endl;
cin>> number;
if(number != 1)
{
if(number < count && number > 0) {
for(i = 2; i <= number; i++)
node = node->next;
for(i = 2; i <= number-1; i++)
node1 = node1->next;
for(i = 2; i <= number+1; i++)
node2 = node2->next;
node2->prev = node1;
node1->next = node2;
node->prev = NULL;
node->next = NULL;
node = NULL;
}
else if(number == count)
{
node = last;

143
last = node->prev;
last->next = NULL;
node = NULL;
}
else
cout<< "Invalid node number!" <<endl;
}
else
{
node = first;
first = node->next;
first->prev = NULL;
node = NULL;
}
cout<< "Node has been deleted successfully!" <<endl;
}

};

int main()
{
int op = 0;
linkedlistllist = linkedlist();
while(op != 4)
{

cout<< "1. Insert at the beginning\n2. Insert at the end\n3. Delete\n4. Display\n5. Exit" <<endl;

cout<< "Enter your choice:" <<endl;


cin>> op;
switch(op)
{
case 1:
llist.insert_beginning();
break;
case 2:
llist.insert_end();
break;
case 3:
llist.del();
break;
case 4:
llist.display();
break;
case 5:
cout<< "Bye Bye!" <<endl;
return 0;
break;
default:
cout<< "Invalid choice!" <<endl;
}
}
return 0;
}

144
OUTPUT:
1. Insert at the beginning
2. Insert at the end
3. Delete
4. Display
5. Exit
Enter your choice:
1
Enter value for the node:
5
Linked list Created!
1. Insert at the beginning
2. Insert at the end
3. Delete
4. Display
5. Exit
Enter your choice:
1
Enter value for the node:
6
Data Inserted at the beginning of the Linked list!
1. Insert at the beginning
2. Insert at the end
3. Delete
4. Display
5. Exit
Enter your choice:
2
Enter value for the node:
7
Data Inserted at the end of the Linked list!
1. Insert at the beginning
2. Insert at the end
3. Delete
4. Display
5. Exit
Enter your choice:
4
List of data in Linked list inAscending order!
6
5
7
List of data in Linked list in Descending order!
7
5
6
List of data in Linked list
5
6
1. Insert at the beginning

145
2. Insert at the end
3. Delete
4. Display
5. Exit
Enter your choice:
3
List of data in Linked list inAscending order!
6
5
7
List of data in Linked list in Descending order!
7
5
6
3 nodes available here!
Enter the node number which you want to delete:
2
Node has been deleted successfully!
1. Insert at the beginning
2. Insert at the end
3. Delete
4. Display
5. Exit
Enter your choice:
5
Bye Bye!

RESULT:

146
EX.NO:43

BINARY TREE TRAVERSAL


DATE:

AIM:

ALGORITHM:

147
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class btree
{
private:
struct node
{
node*leftchild;
int data;
node*rightchild;
}
*root;
public:
btree();
void buildtree(int num);
static void insert(node**sr,int num);
void traverse();
static void inorder(node*sr);
static void preorder(node*sr);
static void postorder(node*sr);
static void del(node*sr);
};
btree::btree()
{
root=NULL;
}
void btree::buildtree(int num)
{
insert(&root,num);
}
void btree::insert(node**sr,int num)
{
if(*sr==NULL)
{
*sr=new node;
(*sr)->leftchild=NULL;
(*sr)->data=num;
(*sr)->rightchild=NULL;
return;
}
else
{
if(num<(*sr)->data)
insert(&((*sr)->leftchild),num);
else
insert(&((*sr)->rightchild),num);
}
return;
}
void btree::traverse()
{
cout<<"\n inorder traversal:\t\t";
inorder(root);
cout<<"\n preorder traversal:\n\t";
preorder(root);
cout<<"\n postorder traversal:\n\t";
postorder(root);
}

148
void btree::inorder(node*sr)
{
if(sr!=NULL)
{
inorder(sr->leftchild);
cout<<"\t"<<sr->data;
inorder(sr->rightchild);
}
else
return;
}
void btree::preorder(node*sr)
{
if(sr!=NULL)
{
cout<<"\t"<<sr->data;
preorder(sr->leftchild);
preorder(sr->rightchild);
}
else
return;
}
void btree::postorder(node*sr)
{
if(sr!=NULL)
{
postorder(sr->leftchild);
postorder(sr->rightchild);
cout<<"\t"<<sr->data;
}
else
return;
}
void btree::del(node*sr)
{
if(sr!=NULL)
{
del(sr->leftchild);
del(sr->rightchild);
}
delete sr;
}
void main()
{
btreebt;
int req,i=1,num;
clrscr();
cout<<endl<<"\t\t\n binary tree traversal";
cout<<"\n specify the number of items to be inserted:";
cin>>req;
cout<<req<<endl;
while(i++<=req)
{
cout<<"\n\t\t entry the data:";
cin>>num;
bt.buildtree(num);
}
bt.traverse();
getch();
}

149
OUTPUT:

RESULT:

150

You might also like