KEMBAR78
Common Lab Manual Print | PDF | Html Element | Html
0% found this document useful (0 votes)
124 views136 pages

Common Lab Manual Print

HTML and C++ Prog for practice

Uploaded by

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

Common Lab Manual Print

HTML and C++ Prog for practice

Uploaded by

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

// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

HTML
programs

BIPU Lab Manual: Cpp programs – 1


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg - 1
<html>
<head>
<title> Bold Italics </title>
</head>
<body>
<b> this is bold text.</b>
<i> this is italics text.</i>
</body>
</html>

Prg – 2
<html>
<head>
<title> Strong and Emphasized</title>
</head>
<body>
<strong> this is strong text. </strong>
<b> this is bold text. </b>
<em> this is emphasized text. </em>
<i> this is italics text. </i>
</body>
</html>

Prg – 3
<html>
<head>
<title> Superscript and Subscript</title>
</head>
<body>
<p> Na<sub>2</sub>S<sub>2</sub>O<sub>3</sub></p>
<p> 1 amu = 1.66 x 10<sup>-24</sup> g</p>
</body>
</html>

Prg – 4
<html>
<head>
<title> Delete text and Insert text</title>
</head>
<body>
<p> <del> this text is deleted </del> </p>
<p> <ins> this text is inserted </ins> </p>
</body>
</html>

BIPU Lab Manual: Cpp programs – 2


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 5
<html>
<head>
<title> Mark Tag</title>
</head>
<body>
Do not forget to buy <mark>Fruits</mark> today.
</body>
</html>

Prg – 6
<html>
<head>
<title> To change the background color of Mark Tag</title>
</head>
<body>
Do not forget to buy <mark style="color:white; background-
color:blue"> Fruits </mark> today.
</body>
</html>

Prg – 7
<html>
<head>
<title> Normal, Big and Small Tags</title>
</head>
<body>
<p> this text is normal text </p>
<p> <Big> this text is big text </big> </p>
<p> <small> this text is small text </small> </p>
</body>
</html>

Prg – 8
<html>
<head>
<title> Nesting of Big and Small Tags</title>
</head>
<body>
<p> this text is normal text </p>
<p> <Big> <Big> <Big>this text is big text </big> </big> </big>
</p>
<p> <small> <small> <small> this text is small text </small>
</small> </small> </p>
</body>
</html>

BIPU Lab Manual: Cpp programs – 3


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 9
<html>
<head>
<title> Headings </title>
</head>
<body>
<h1>This is a heading one </h1>
<h2>This is a heading two </h2>
<h3>This is a heading three </h3>
<h4>This is a heading four </h4>
<h5>This is a heading five </h5>
<h6>This is a heading six </h6>
</body>
</html>
Prg – 10
<html>
<head>
<title> Headings with style options </title>
</head>
<body>
<h1 style="color:blue;">This is a heading with color blue</h1>
<h2 style="font-family:verdana;">This is a heading with different
font </h2>
<h3 style="font-size:300%;">This is a heading with font size
300%</h3>
<h4 style="text-align:center;">Centered aligned Heading</h4></body>
</html>
Prg – 11
<html>
<head>
<title> Font Size, color and face</title>
</head>
<body bgcolor="black" text ="red">
<p> <Font size="6" face="courier new" color="white"> this font size
is 6 </font> </p>
<p> this text is normal text in red color</p>
</body>
</html>
Prg – 12
<html>
<head>
<title> Paragraph </title> </head>
<body>
<p> this is paragraph 1 </p>
<p > this is paragraph 2 </p>
</body>
</html>

BIPU Lab Manual: Cpp programs – 4


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 12
<html>
<head>
<title> Paragraph with style attributes </title>
</head>
<body>
<p> this is paragraph 1 default left aligned</p>
<p align="left" style="color:red;"> this is paragraph 2 </p>
<p align="center" style="font-family:courier;">
this is paragraph 3 </p>
<p align="right" style="font-size:160%;"> this is paragraph 4</p>
<b style="color:red;"> Note: </b>
Style option can contain more than one effects.
<p align="left" style="color:green; font-family:courier; font-
size:300%"> Style option can contain more than one effects. </p>
</body>
</html>
Prg – 13
<html>
<head>
<title> Center </title>
</head>
<body>
<center> this text is center aligned </center> <br>
this text is normal text
</body>
</html>
Prg – 14
<html>
<head>
<title> Center with style effects </title>
</head>
<body>
<center style="color:green; font-family:courier; font-size:300%;">
this text is center aligned </center> <br>
this text is normal text
</body>
</html>
Prg – 15
<html>
<head>
<title>DIV</title> </head>
<body>
<div >div text 1</div>
<div >div text 2</div>
<div >div text 3</div>
</body> <html>

BIPU Lab Manual: Cpp programs – 5


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 16
<html>
<head>
<title>DIV with align</title>
</head>
<body>
<div align="left">div text 1</div>
<div align="center">div text 2</div>
<div align="right">div text 3</div>
</body>
<html>
Prg – 17
<html>
<head>
<title>DIV with style effects </title>
</head>
<body>
<div style="color:red"; align="center">center text</div>
<div style="color:purple"; align="right">right text</div>
<div style="color:blue"; align="left">left text</div>
</body>
<html>
Prg – 18
<html>
<head>
<title>UL default </title>
</head>
<body>
<h2>Unordered List with Default Bullets</h2>
<ul >
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Note: By default the UL tag will always display disc option if you do
not mention it.
</body>
</html>
Prg - 19
<html>
<head> <title> UL Disc </title> </head>
<body><h2>Unordered List with Default Bullets</h2>
<ul type=“disc” >
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li> </ul>
</body> </html>

BIPU Lab Manual: Cpp programs – 6


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 20
<html>
<head>
<title>UL Circle </title>
</head>
<body>
<h2>Unordered List with Circle Bullets</h2>
<ul type=“circle”>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</body>
</html>

Prg – 21
<html>
<head>
<title>UL Square </title>
</head>
<body>
<h2>Unordered List with Square Bullets</h2>
<ul type=“square”>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</body>
</html>

Prg – 22
<html>
<head>
<title>UL None </title>
</head>
<body>
<h2>Unordered List with None</h2>
<ul type=“None”>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</body>
</html>

BIPU Lab Manual: Cpp programs – 7


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 22
<html>
<head>
<title>OL Lists </title>
</head>
<body>
<h2>Ordered List with Default Numbers</h2>
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
</body>
</html>

Prg – 23
<html>
<head>
<title>OL Lists </title>
</head>
<body>
<h2>Ordered List with default start numbers</h2>
<ol start="5">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
</body>
</html>

Prg – 24
<html>
<head>
<title>OL Lists </title>
</head>
<body>
<h2>Ordered List with Capital Alphabets</h2>
<ol type=“A”>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
</body>
</html>

BIPU Lab Manual: Cpp programs – 8


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 25
<html>
<head>
<title>OL Lists </title>
</head>
<body>
<h2>Ordered List with Capital Alphabets with default start</h2>
<ol type=“A” start=“3”>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
</body>
</html>

Prg – 26
<html>
<head>
<title>OL Lists </title>
</head>
<body>
<h2>Ordered List with small Alphabets</h2>
<ol type=“a”>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
</body>
</html>

Prg - 27
<html>
<head>
<title>OL Lists </title>
</head>
<body>
<h2>Ordered List with Small Alphabets with default start</h2>
<ol type=“a” start=“3”>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
</body>
</html>

BIPU Lab Manual: Cpp programs – 9


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 28
<html>
<head>
<title>OL Lists </title>
</head>
<body>
<h2>Ordered List with Capital Roman Numbers</h2>
<ol type=“I”>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
</body>
</html>

Prg – 29
<html>
<head>
<title>OL Lists </title>
</head>
<body>
<h2>Ordered List with Capital Roman Numbers with default start</h2>
<ol type=“I” start=“3”>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
</body>
</html>

Prg – 30
<html>
<head>
<title>OL Lists </title>
</head>
<body>
<h2>Ordered List with small Roman numbers</h2>
<ol type=“i”>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
</body>
</html>

BIPU Lab Manual: Cpp programs – 10


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 31
<html>
<head>
<title> OL Lists </title>
</head>
<body>
<h2>Ordered List with Small Roman numbers with default start</h2>
<ol type=“i” start=“3”>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
</body>
</html>
Prg – 32
<html>
<head>
<title>OL Lists </title>
</head>
<body>
<h2>Reversed Ordered List </h2>
<ol reversed>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
<b style="Color:red;">Note: </b> Does not work in<b>Internet
Explorer</b>
</body>
</html>
Prg – 33
<html>
<head>
<title>OL Lists </title>
</head>
<body>
<h2>Reversed Ordered List with default start numbers</h2>
<ol reversed start="5">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
<b style="Color:red;"> Note: </b>
Does not work in <b> Internet Explorer</b>
</body> </html>

BIPU Lab Manual: Cpp programs – 11


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 34
<html>
<head>
<title> OL Lists </title>
</head>
<body>
<h2> Reversed Ordered List with default roman numbers </h2>
<ol reversed type = "i">
<li> Coffee </li>
<li> Tea </li>
<li> Milk </li>
</ol>
<b style="Color:red;">Note: </b>
Does not work in<b>Internet Explorer</b>
</body>
</html>
Prg – 35
<html>
<head>
<title>OL Lists </title>
</head>
<body>
<h2>Reversed Ordered List with default roman start numbers</h2>
<ol reversed type="i" start="5">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
<b style="Color:red;">Note: </b>
Does not work in<b>Internet Explorer</b>
</body>
</html>
Prg – 36
<html>
<head>
<title>OL Lists </title>
</head>
<body>
<h2>Reversed Ordered List with Capital Alphabets</h2>
<ol reversed type="A" >
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
<b style="Color:red;">Note: </b>
Does not work in<b>Internet Explorer</b>
</body> </html>
BIPU Lab Manual: Cpp programs – 12
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 37
<html>
<head>
<title>OL Lists </title>
</head>
<body>
<h2>Reversed Ordered List with Capital Alphabets</h2>
<ol reversed type="A" start=”7”>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
<b style="Color:red;">Note: </b>
Does not work in<b>Internet Explorer</b>
</body>
</html>
Prg – 38
<html>
<head> <title> LI Types </title> </head>
<body>
<ul>
<li> Bullet types I like </li>
<ul>
<li type="disc">Discs</li>
<li type="circle">Circles</li>
<li type="square">Squares</li>
<li type="none">None</li>
</ul>
<li> Bullet types I don't like </li>
<ol>
<li type="I">Roman Numerals</li>
<li type="i">Lowercase Roman Numerals</li>
</ol>
</ul>
<b style="Color:red;">Note: </b>
Does not work in<b>Internet Explorer</b>
</body>
</html>
Prg – 39
<html>
<head> <title>Li Values </title> </head>
<body>
<ol>
<li> One </li> <li> Two </li> <li> Skip a few </li>
<li value="99"> Ninety Nine </li> <li> and Hundred </li>
</ol>
</body> </html>
BIPU Lab Manual: Cpp programs – 13
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg - 40
<html>
<head>
<title> Description Lists </title>
</head>
<body bgcolor="#ffaabb">
<dl>
<dt>Parents</dt>
<dd>Jamie</dd>
<dd>Charlie</dd>
<dt>Children</dt>
<dd>Landry</dd>
<dd>Oakley</dd>
<dd>Skyler</dd>
<dt>Pets</dt>
<dd>Cat</dd>
<dd>Dog</dd>
<dd>Gerbil</dd>
</dl>
</body>
</html>

Prg - 41
<html>
<head>
<title> Description Lists with CSS effects</title>
</head>
<body bgcolor="#ffaabb">
<dl>
<dt style="font-weight:bold;">Parents</dt>
<dd>Jamie</dd>
<dd>Charlie</dd>
<dt style="font-style:italic;">Children</dt>
<dd>Landry</dd>
<dd>Oakley</dd>
<dd>Skyler</dd>
<dt style="font-style:oblique;">Pets</dt>
<dd>Cat</dd>
<dd>Dog</dd>
<dd>Gerbil</dd>
</dl>
<b style="Color:red;">Note: </b> oblique - The text is "leaning" it is
very similar to italic, but less supported.
</body>
</html>

BIPU Lab Manual: Cpp programs – 14


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 42
<html>
<head>
<title> Description Lists with CSS effects with letter spacing</title>
</head>
<body bgcolor="#ffaabb">
<dl>
<dt style="font-weight:bolder; letter-spacing: 7px">Parents</dt>
<dd>Jamie</dd>
<dd>Charlie</dd>
<dt style="font-style:italic; letter-spacing: 5px">Children</dt>
<dd>Landry</dd>
<dd>Oakley</dd>
<dd>Skyler</dd>
<dt style="font-style:oblique; letter-spacing: 3px">Pets</dt>
<dd>Cat</dd>
<dd>Dog</dd>
<dd>Gerbil</dd>
</dl>
<b style="Color:red;">Note: </b> oblique - The text is "leaning" it is
very similar to italic, but less supported.
</body>
</html>

Prg - 43
<html>
<head>
<title> Description Lists with CSS effects</title>
</head>
<body bgcolor="#ffaabb">
<dl>
<dt style="font-variant:small-caps;">Parents</dt>
<dd>Jamie</dd>
<dd>Charlie</dd>
<dt style="font-variant:small-caps;">Children</dt>
<dd>Landry</dd>
<dd>Oakley</dd>
<dd>Skyler</dd>
<dt style="font-variant:small-caps;">Pets</dt>
<dd>Cat</dd>
<dd>Dog</dd>
<dd>Gerbil</dd>
</dl>
</body>
</html>

BIPU Lab Manual: Cpp programs – 15


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg - 44
<html>
<head>
<title> Nested Lists</title>
</head>
<body>
<ul>
<li style="background-color:brown; font-variant:small-caps;
width:3.5%; color:white;"> Coffee </li>
<ol>
<li>Espresso (Short Black)</li>
<li>Double Espresso (Doppio)</li>
<li>Short Macchiato</li>
<li>Long Black (Americano)</li>
<li>Cappuccino</li>
</ol>
<li style="background-color:brown; font-variant:small-caps;
width:2%; color:white;"> Tea </li>
<ol>
<li>White Tea</li>
<li>Black Tea</li>
<li>Green Tea</li>
<li>Herbal Tea</li>
<li>Plain Tea</li>
</ol>
<li style="background-color:brown; font-variant:small-caps;
width:10%; color:white;"> Fruit-Flavored Tea </li>
<ol>
<li>Orange Tea</li>
<li>Lemon Tea</li>
<li>Raspberry Tea</li>
<li>Pineapple Tea</li>
<li>Mango Tea</li>
</ol>
</ul>
</body>
</html>
Prg – 45
<html>
<head> <title> Lists with images using CSS effects </title> </head>
<body>
<ul style="list-style-image:url(miniowl.jpg);">
<li>Espresso (Short Black)</li>
<li>Double Espresso (Doppio)</li>
<li>Short Macchiato</li>
</ul>
</body> </html>

BIPU Lab Manual: Cpp programs – 16


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 47
<html>
<head>
<title> background image with CSS effects </title>
</head>
<body style="background-image:url(treeowl.jpg);">
</body>
</html>
Prg – 48
<html>
<head>
<title> background image with CSS effects </title>
</head>
<body style="background-image:url(treeowl.jpg); background-
repeat:repeat-x;">
</body>
</html>

Prg – 49
<html>
<head>
<title> background image with CSS effects </title>
</head>
<body style="background-image:url(treeowl.jpg); background-
repeat:repeat-y;">
</body>
</html>

Prg – 50
<html>
<head>
<title> background image with CSS effects </title>
</head>
<body style="background-image:url(treeowl.jpg); background-
repeat:no-repeat;">
</body>
</html>

Prg – 51
<html>
<head>
<title> background image with CSS effects </title>
</head>
<body style="background-image:url(treeowl.jpg); background-
repeat:no-repeat; background-position:right top;">
</body>
</html>

BIPU Lab Manual: Cpp programs – 17


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 52
<html>
<head>
<title> background image with CSS effects </title>
</head>
<body style="background-image:url(treeowl.jpg); background-repeat:no-
repeat; background-position:right bottom;">
</body>
</html>
Prg – 53
<html>
<head>
<title> background image with CSS effects </title>
</head>
<body style="background-image:url(treeowl.jpg); background-
repeat:no-repeat; background-position:left top;">
</body>
</html>
Prg – 54
<html>
<head>
<title> background image with CSS effects </title>
</head>
<body style="background-image:url(treeowl.jpg); background-
repeat:no-repeat; background-position:left bottom;">
</body>
</html>
Prg – 55
<html>
<head>
<title> background image with CSS effects </title>
</head>
<body style="background-image:url(treeowl.jpg); background-
repeat:no-repeat; background-position:center;"> </body>
</html>
Prg – 56
<html>
<head>
<title> background image with CSS effects </title>
</head>
<body style="background-image:url(treeowl.jpg);">
<h1>Hello Reader!</h1>
<p>The background image is displayed, but it is disturbing the
reader! </p>
</body>
</html>

BIPU Lab Manual: Cpp programs – 18


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 57
<html>
<head> <title> background image with CSS effects </title> </head>
<body style="background-image:url(treeowl.jpg); background-
repeat:repeat-x;">
<h1>Hello Reader!</h1>
<p>Here, a backgound image is repeated only horizontally!</p>
</body>
</html>

Prg – 58
<html>
<head>
<title> background image with CSS effects </title>
</head>
<body style="background-image:url(treeowl.jpg); background-
repeat:repeat-y;">
<h1>Hello Reader!</h1>
<p>Here, a backgound image is repeated only vertically!</p>
</body>
</html>

Prg – 59
<html>
<head>
<title> background image with CSS effects </title>
</head>
<body style="background-image:url(treeowl.jpg); background-repeat:no-
repeat;">
<h1>Hello Reader!</h1>
<p>The background image is only showing once, but it is disturbing
the reader!</p>
</body>
</html>
Prg – 60
<html>
<head>
<title> background image with CSS effects </title>
</head>
<body style="background-image:url(treeowl.jpg); background-repeat:no-
repeat; background-position:right top; ">
<h1> Hello Reader! </h1>
Now the background image is only shown once, and positioned away from
the text. In this example the text is overlapping on the image on the
right side, so the background image is disturbing the text.
</body>
</html>

BIPU Lab Manual: Cpp programs – 19


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 61
<html>
<head>
<title> background image with CSS effects </title>
</head>
<body style="background-image:url(treeowl.jpg); background-repeat:no-
repeat; background-position:right top; margin-right:200px;">
<h1>Hello Reader!</h1>
Now the background image is only shown once, and positioned away from
the text. In this example we have also added a margin on the right side,
so the background image will never disturb the text.
</body>
</html>
Prg – 62
<html>
<head>
<title> Links </title>
</head>
<body>
<a href="prg24.html">prg24</a>
</body>
</html>

Prg – 63
<html>
<head>
<style>
a:link { color: green; background-color: cyan; text-decoration: none; }
a:visited { color: blue; background-color: transparent; text-decoration:
none; }
a:hover { color: red; background-color: transparent; text-decoration:
underline; }
a:active { color: green; background-color: transparent; text-decoration:
underline; }
</style>
</head>
<title> Links - With Colors</title>
<body>
<font size="5">
<a href="prg24.html">move your mouse pointer over me</a>
</font>
</body>
</html>
Prg – 64
<html>
<head>
<style>
a:link { color: green; background-color: cyan; text-decoration: none; }
a:visited { color: blue; background-color: transparent; text-decoration:
none; }
BIPU Lab Manual: Cpp programs – 20
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

a:hover { color: red; background-color: transparent; text-decoration:


underline; }
a:active { color: green; background-color: transparent; text-decoration:
underline; }
</style>
</head>
<title> HTML Links - The target Attribute </title>
<body>
<font size="4">
Click here for <a href="prg24.html" target="_blank">new window</a>
<p>If you set the target attribute to "_blank", the link will open in a new
browser window or tab.</p>
Click here for <a href="prg24.html" target="_self">self-window</a>
<p>If you set the target attribute to "_blank", the link will Opens the
linked document in the same frame as it was clicked (this is default). </p>
</font>
</body>
</html>
Prg – 65
<html>
<head>
<title> Image as Link </title>
</head>
<body>
<p>The image is a link. You can click on it.</p>
<a href="thank.gif">
<img src="thank.gif" alt="Thank You" style="width:100px;
height:100px; border:0">
</a>
<p>We have added "border:0" to prevent IE9 (and earlier) from displaying a
border around the image.</p>
<p>Note: GIF pictures work better in Internet Explorer </p>
</body>
</html>
Prg – 66
<html>
<head>
<title> Bookmark within a webpage </title></head>
<body>
<p><a href="#C8">Jump to Chapter 8</a></p>
<h2 id="C1">Chapter 1</h2>
<p>This chapter explains about <b> Overview of Computers</b> </p>
<a href="#C7">Jump to Chapter 7</a>
<h2 id="C2">Chapter 2</h2>
<p>This chapter explains about <b> Input Output and Memory Devices
</b> </p>
<h2 id="C3">Chapter 3</h2>
<p>This chapter explains about <b> Data Representations</b></p>
<h2 id="C4">Chapter 4</h2>
<p>This chapter explains about <b>Software Concepts </b></p>
<h2 id="C5">Chapter 5</h2>
<p>This chapter explains about <b>Problem Solving Methodology </b></p>

BIPU Lab Manual: Cpp programs – 21


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

<h2 id="C6">Chapter 6</h2>


<p>This chapter explains about <b> Object Oriented Concepts </b>
</p>
<h2 id="C7">Chapter 7</h2>
<p>This chapter explains about <b> Introduction to C++ </b> </p>
<h2 id="C8">Chapter 8</h2>
<p>This chapter explains about <b> Data Types </b></p>
<a href="#C1">Jump to chapter 1 </a>
</body>
</html>
Prg – 67
<html>
<head> <title> Table Rows </title> </head>
<body>
<table border="1">
<tr> <td>This is row one</td> </tr>
<tr> <td>this is row two</td> </tr>
</table>
</body>
</html>
Prg – 68
<html>
<head> <title> Table Columns </title> </head>
<body>
<table border=”1”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>
Prg – 69
<html>
<head> <title> Table Heading </title> </head>
<body>
<table border=”1”>
<tr>
<th> This table heading, column one </th>
<td> This table heading, column two </t>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>

BIPU Lab Manual: Cpp programs – 22


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 70
<html>
<head> <title> table borders</title> </head>
<body> Table with outer border / frame (void)
<table border=“1” frame=“void”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>
Prg – 71
<html>
<head> <title> table border </title> </head>
<body> Table with outer border / frame (above)
<table border=“1” frame=“above”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>
Prg – 72
<html>
<head> <title> table border </title> </head>
<body> Table with outer border / frame (below)
<table border=“1” frame=“below”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>

BIPU Lab Manual: Cpp programs – 23


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 73
<html>
<head> <title> table border </title> </head>
<body> Table with outer border / frame (left)
<table border=“1” frame=“lhs”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>
Prg – 74
<html>
<head> <title> table borders</title> </head>
<body> Table with outer border / frame (right)
<table border=“1” frame=“rhs”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>
Prg – 75
<html>
<head> <title>Table border </title> </head>
<body> Table with outer border / frame (above and below)
<table border=“1” frame=“hsides”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>

BIPU Lab Manual: Cpp programs – 24


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 76
<html>
<head> <title>Table border </title> </head>
<body> Table with outer border / frame (right and left)
<table border=“1” frame=“vsides”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>
Prg – 77
<html>
<head> <title>Table border </title> </head>
<body> Table with outer border / frame (box)
<table border=“1” frame=“box”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>
Prg – 78
<html>
<head> <title>Table border </title> </head>
<body> Table with border color
<table border=“2” bordercolor=“#ffff00”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>

BIPU Lab Manual: Cpp programs – 25


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 79
<html>
<head> <title>Table border </title> </head>
<body> Table with background color
<table border=“2” bgcolor=“#ffff00”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>
Prg – 80
<html>
<head> <title>Tables</title> </head>
<body> Table with background image
<table border=“2” background=“url”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>
Prg – 81
<html>
<head> <title> Table alignment </title> </head>
<body> Table align (left, right, center)
<table border=“2” align=“right”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>

BIPU Lab Manual: Cpp programs – 26


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 82
<html>
<head> <title> Table alignment </title> </head>
<body> Table align (left, right, center)
<table border=“2” align=“left”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>
Prg – 83
<html>
<head> <title> Table alignment </title> </head>
<body> Table align (left, right, center)
<table border=“2” align=“center”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>
Prg – 85
<html>
<head> <title>Table cell padding </title> </head>
<body> Table cell padding
<table border=“2” align=“right” cellpadding=“25”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>

BIPU Lab Manual: Cpp programs – 27


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 86
<html>
<head> <title> Table cell spacing </title> </head>
<body> Table cell spacing
<table border=“2” align=“right” cellspacing=“20”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>
Prg – 87
<html>
<head> <title> Table cell spacing and cell padding </title> </head>
<body> Table cell spacing and cell padding
<table border=“2” align=“right” cellspacing=“40” cellpadding=“5”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>
Prg – 88
<html>
<head> <title> Table Width and Height </title> </head>
<body> Table Width and Height
<table border=“2” align=“right” width=“40%” height=“75%”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table>
</body>
</html>

BIPU Lab Manual: Cpp programs – 28


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 89
<html>
<head> <title>Table row and cell properties </title> </head>
<body> Row and Cell properties
<table border=“2” align=“center” width=“40%” height=“75%”>
<tr>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
</table> </body> </html>
Prg – 90
<html>
<head> <title> Align content </title> </head>
<body> Align content row wise (center / right). Note: The alignment is
always left by default.
<table border=“2” align=“center” width=“40%” height=“75%”>
<tr align=“right”>
<td> row one, column one </td>
<td> row one, column two</td>
</tr>
<tr align=“center”>
<td> row two, column one </td>
<td> row two, column two </td>
</tr>
</table> </body> </html>
Prg – 91
<html>
<head> <title> Vertical Align</title> </head>
<body> Vertical Align content row wise. Note: Vertical alignment can be done
either top, bottom or center.
<table border=“2” align=“center” width=“40%” height=“75%”>
<tr align=“right” valign=“top”>
<td>This is row one</td>
</tr>
<tr align=“center” valign=“center”>
<td>This is row two </td>
</tr>
<tr align=“left” valign=“bottom”>
<td>This is row three </td>
</tr>
</table> </body> </html>
Prg – 92
<html>
<head> <title> color change </title> </head>
<body> Row wise background color change
<table border=“2” align=“center” width=“40%” height=“75%”>
<tr align=“right” valign=“top” bgcolor=“cyan”>
<td>This is row one </td>
</tr>
<tr align=“center” valign=“center” bgcolor=“teal”>
<td> This is row two. </td>
</tr>
BIPU Lab Manual: Cpp programs – 29
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

<tr align=“left” valign=“bottom” bgcolor=“lime”>


<td> This is row three. </td>
</tr>
</table> </body> </html>
Prg – 93
<html>
<head> <title> border color change </title> </head>
<body> Row wise border color change
<table border=“2” align=“center” width=“40%” height=“75%”>
<tr align=“right” valign=“top” bgcolor=“cyan” bordercolor=“green”>
<td>This is row one. </td>
</tr>
<tr align=“center” valign=“center” bgcolor=“teal” bordercolor=“red”>
<td>This is row two. </td>
</tr>
<tr align=“left” valign=“bottom” bgcolor=“lime” bordercolor=“lime”>
<td>This is row three.</td>
</tr>
</table> </body> </html>
Prg – 94
<html>
<head> <title> background image </title> </head>
<body> Row wise background image
<table border=“2” align=“center” width=“40%” height=“75%”>
<tr align=“right” valign=“top” bgcolor=“cyan” background=“url”>
<td>This is row one.</td>
</tr>
<tr align=“center” valign=“center” bgcolor=“teal” background=“url”>
<td>This is row two.</td>
</tr>
<tr align=“left” valign=“bottom” bgcolor=“lime” background=“url”>
<td>This is row three.</td>
</tr>
</table>
</body>
</html>
Prg – 95
<html>
<head> <title> Row wise height </title> </head>
<body> Row wise height
<table border=“2” align=“center” width=“40%”>
<tr align=“right” valign=“top” bgcolor=“cyan” background=“url” height=“250”>
<td>This is row one.</td>
</tr>
<tr align=“center” valign=“center” bgcolor=“teal” background=“url”>
<td>This is row two.</td>
</tr>
<tr align=“left” valign=“bottom” bgcolor=“lime” background=“url”>
<td>This is row three.</td>
</tr>
</table> </body> </html>

BIPU Lab Manual: Cpp programs – 30


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 96
<html>
<head> <title>Tables</title> </head>
<body> Column wise background color change
<table border=“2” align=“center” width=“40%” height=“75%”>
<tr height="150">
<td align="right" valign="top" bgcolor="lime">This is row one </td>
<td align="left" valign="bottom" bgcolor="gray">This is row one</td>
<td align="center" valign="middle" bgcolor="pink">This is row one
</td>
</tr>
</table> </body> </html>
Prg – 97
<html>
<head> <title>Tables</title> </head>
<body> Column wise border color change
<table border=“2” align=“center” width=“40%” height=“75%”>
<tr height="150">
<td align="right" valign="top" bordercolor="red">This is row one, left
side.</td>
<td align="left" valign="bottom" bordercolor="gray">This is row one,
left side.</td>
<td align="center" valign="middle" bordercolor="pink">This is row one,
left side.</td>
</tr>
</table> </body> </html>
Prg – 98
<html>
<head> <title>Tables</title> </head>
<body> column wise background image
<table border=“2” align=“center” width=“40%” height=“75%”>
<tr height="150">
<td align="right" valign="top" background=“url”>This is row one, left
side.</td>
<td align="left" valign="bottom" background=“url”>This is row one,
left side.</td>
<td align="center" valign="middle" background=“url”>This is row one,
left side.</td>
</tr>
</table> </body> </html>
Prg – 99
<html>
<head> <title>Tables</title> </head>
<body> Column wise width
<table border=“2” align=“center” width=“40%”>
<tr>
<td align="right" valign="top" bordercolor="red" width="50">This is
row one, left side. </td>
<td align="left" valign="bottom" bordercolor="gray" width="100">This
is row one, left side. </td>
<td align="center" valign="middle" bordercolor="pink" width="250">This
is row one, left side. </td>
BIPU Lab Manual: Cpp programs – 31
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

</tr>
</table> </body> </html>
Prg – 100
<html>
<head> <title> Table with Column Span (Merging) </title> </head>
<body> Column Span: Merging
<table border="1" width="50%">
<tr>
<th colspan = "3"> Computer Marks </th>
</tr>
<tr>
<td>Ravi</td> <td>Kumar</td> <td>99</td>
</tr>
<tr>
<td>Mohan</td> <td>Reddy</td> <td>100</td>
</tr>
<tr>
<td>Swaroop</td> <td>M</td> <td>100</td>
</tr>
</table> </body> </html>
Prg – 101
<html>
<head> <title> Table with Width, Align, Cell Spacing, and Cell Padding
</title> </head>
<body> Row Span: Merging
<table border="1" width="50%" align="right" cellspacing="5"
cellpadding="10">
<tr>
<th rowspan="5"> Neo BS - II </th>
</tr>
<tr>
<th colspan = "3"> Computer Marks </th>
</tr>
<tr>
<td>Ravi</td> <td>Kumar</td> <td>99</td>
</tr>
<tr>
<td>Mohan</td> <td>Reddy</td> <td>100</td>
</tr>
<tr>
<td>Swaroop</td> <td>M</td> <td>100</td>
</tr>
</table> </body> </html>
Prg – 102
<html>
<head>
<style>
div
{
width:130px;
height:50px;
-ms-transform:rotate(45deg); /* IE 9 */

BIPU Lab Manual: Cpp programs – 32


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

-moz-transform:rotate(270deg); /* Firefox */
-webkit-transform:rotate(270deg);/*Safari & Chrome */
-o-transform:rotate(270deg); /* Opera */
}
</style>
<title> Vertical Text </title></head>
<body> Vertical Text in Tables
<div> vertical text </div>
</body> </html>
Prg – 103
<html>
<head> <title> Preformatted Text </title> </head>
<body> Preformatted Text. It preserves both spaces and line breaks.
The pre tag is good for displaying computer code:
<pre>
This is
preformatted text.
It preserves both spaces
and line breaks.
</pre>
<p>The pre tag is good for displaying computer code:</p>
<pre>
for i = 1 to 10
print i
next i
</pre>
</body> </html>
Prg – 104
<html>
<head> <title> Bidirectional Override </title> </head>
<body> Bi-directional Override. If your browser supports bi-directional
override (bdo), then the line will be written from the right to the left
(rtl):
<p> This is normal text BELLARY </p>
<p> Here comes Bidirectional Override text for BALLARI</p>
<bdo dir=“rtl”>
BALLARI
</bdo> </body> </html>
Prg – 105
<!DOCTYPE html>
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> Marquee: An HTML marquee is a scrolling piece of text displayed
either horizontally across or vertically down your webpage depending on the
settings.
Note: The HTML <marquee> tag may not be supported by various browsers so its
not recommended to rely on this tag, instead you can use Java script and CSS
to create such effects.
<marquee>This is basic example of marquee</marquee>
</body> </html>

BIPU Lab Manual: Cpp programs – 33


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 106
<!DOCTYPE html>
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> The simple marquee (left to right)
<marquee direction="right"> This text will scroll from left to right
</marquee> </body> </html>
Prg – 107
<!DOCTYPE html>
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> The simple marquee (up)
<marquee direction="up"> This text will scroll from bottom to up </marquee>
</body> </html>
Prg – 108
<!DOCTYPE html>
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> The simple marquee (down)
<marquee direction="down"> This text will scroll from up to bottom
</marquee> </body> </html>
Prg – 109
<!DOCTYPE html>
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> The simple marquee (width)
<marquee width="50%"> This example will take only 50% width </marquee>
</body> </html>
Prg – 110
<!DOCTYPE html>
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> The simple marquee (height)
<marquee height="500" direction=“up”> This example will take 500 pixels in
height. </marquee>
</body> </html>
Prg – 111
<!DOCTYPE html>
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> The simple marquee (behavior - scroll)
<marquee behavior="scroll"> This text will scroll </marquee>
</body> </html>
Prg – 112
<!DOCTYPE html>
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> The simple marquee (behavior - slide)
<marquee behavior="slide"> This text will slide </marquee>
</body> </html>

BIPU Lab Manual: Cpp programs – 34


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 113
<!DOCTYPE html>
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> The simple marquee (behavior - alternate)
<marquee behavior="alternate"> This text will alternate </marquee>
</body> </html>
Prg – 114
<!DOCTYPE html>
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> The simple marquee(scroll amount)
<marquee behavior="scroll" scrollamount="10"> This text will scroll
</marquee>
<marquee behavior="scroll" scrollamount="20"> This text will scroll
</marquee>
<marquee behavior="scroll" scrollamount="30"> This text will scroll
</marquee>
<marquee behavior="scroll" scrollamount="40"> This text will scroll
</marquee> </body> </html>
Prg – 115
<!DOCTYPE html>
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> The simple marquee(scroll delay)
<marquee behavior="scroll" scrolldelay="250"> This text will scroll dealy
250 = ¼ second
</marquee>
<marquee behavior="scroll" scrolldelay="500"> This text will scroll
</marquee>
<marquee behavior="scroll" scrolldelay="750"> This text will scroll
</marquee>
<marquee behavior="scroll" scrolldelay="1000"> This text will scroll
</marquee> </body> </html>
Prg – 116
<!DOCTYPE html>
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> The simple marquee (loop)
<marquee behavior="scroll" loop="1"> This text will loop 1 time </marquee>
<marquee behavior="scroll" loop="2"> This text will loop 2 times </marquee>
<marquee behavior="scroll" loop="3"> This text will loop 3 times </marquee>
<marquee behavior="scroll" loop="4"> This text will loop 4 times </marquee>
</body> </html>
Prg – 117
<!DOCTYPE html>
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> The simple marquee (bgcolor)
<marquee behavior="alternate" bgcolor="cyan">
marquee with bgcolor
</marquee> </body> </html>
BIPU Lab Manual: Cpp programs – 35
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 118
<!DOCTYPE html>
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> The simple marquee (hspace)
<marquee behavior="alternate" bgcolor="cyan" hspace="250%"> marquee with
bgcolor
</marquee> </body> </html>
Prg – 119
<!DOCTYPE html>
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> The simple marquee (vspace)
<marquee behavior="alternate" bgcolor="cyan" hspace="250%" vspace="150%">
marquee with bgcolor
</marquee> </body> </html>
Prg – 120
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> Miscellaneous HTML Marquee Tags <center>
<marquee scrollamount="5" width="250" bgcolor="cyan"> &lt;&lt;&lt;
</marquee>
Opposite Direction Marquees
<marquee scrollamount="5" direction="right" width="250" bgcolor="lime">
&gt;&gt;&gt;
</marquee></center> </body> </html>
Prg – 121
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> Miscellaneous HTML Marquee Tags
<marquee behavior="alternate"> <marquee width="200" bgcolor="cyan">
Reoccuring Marquee
</marquee> </marquee> </body> </html>
Prg – 122
<html>
<head> <title>HTML marquee Tag</title> </head>
<body> Miscellaneous HTML Marquee Tags
<marquee direction="down" behavior="alternate" height="200" bgcolor="cyan">
<marquee behavior="alternate" width="1000" >
<font size="7" color="red">?</font>
</marquee> </marquee> </body> </html>
Prg – 123
<html>
<head> <title> HTML Frames </title> </head>
<frameset cols="120,*">
<frame src=“url”>
<frameset rows="*,50">
<frame src=“url”>
<frame src=“url”>
</frameset>
</frameset>
</html>
BIPU Lab Manual: Cpp programs – 36
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 124
<html>
<head> <title> HTML Frames </title> </head>
<frameset cols="120,*">
<frame src="url" >
<frame src="url">
</frameset> </html>
Prg – 125
<html>
<head> <title> HTML Frames DEFAULT PAGES </title> </head>
<frameset cols="120,*" >
<frame src="menu.html" name="menu" >
<frame src="frontf.html" name="main" >
</frameset> </html>
Prg – 126
<html>
<head> <title> HTML Frames Borders</title> </head>
<frameset cols="120,*" frameborder="0" border="0“ framespacing="0">
<frame src="menu.htm" name="menu" >
<frame src="frontf.htm" name="main" >
</frameset> </html>
Prg – 127
<html>
<head> <title>My Frames Page</title> </head>
<frameset rows="16%,84%">
<frame src="top.htm" name="top">
<frame src="bottom.htm" name="bottom">
</frameset> </html>
Prg – 128
<html>
<head> <title>My Frames Page</title> </head>
<frameset rows="16%,84%">
<frameset cols="50%,50%">
<frame src="tl.htm" name="tl">
<frame src="tr.htm" name="tr">
</frameset>
<frame src="bottom.htm" name="bottom">
</frameset> </html>
Prg – 129
<html>
<head> <title>My Frames Page</title> </head>
<frameset rows="16%,84%">
<frame src="top.htm" name="top">
<frameset cols="50%,50%">
<frame src="left.htm" name="left">
<frame src="right.htm" name="right">
</frameset>
</frameset> </html>

BIPU Lab Manual: Cpp programs – 37


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 130
<html>
<head> <title>My Frames Page</title> </head>
<frameset rows="50%,50%" cols="50%,50%">
<frame src="topleft.htm" name="topleft">
<frame src="topright.htm" name="topright">
<frame src="botleft.htm" name="botleft">
<frame src="botright.htm" name="botright">
</frameset>
</html>
Prg – 131
<html>
<head><title>My Frames Page</title></head>
<frameset rows="50%,50%" cols="50%,50%">
<frame src="topleft.htm" name="topleft">
<frame src="topright.htm" name="topright">
<frame src="botleft.htm" name="botleft">
<frameset rows="50%,50%">
<frameset cols="50%,50%">
<frame src="brtl.htm" name="brtl">
<frame src="brtr.htm" name="brtr">
</frameset>
<frame src="botrbot.htm" name="botrbot">
</frameset>
</frameset>
</html>
Prg – 132
<html>
<head> <title> Forms - Text Fields </title> </head>
<body> defines a password field:
Note: The characters in a password field are masked (shown as asterisks or
circles).
<form>
First name: <input type="text" name="firstname" Value="First Name"
Size="25"><br>
Last name: <input type="text" name="lastname" Value="Last Name" Size="25">
</form> </body> </html>
Prg – 133
<html>
<head> <title> Forms - Password Field </title> </head>
<body> defines a radio button. Radio buttons let a user select ONLY ONE of a
limited number of choices:
<form> Password: <input type="password" name="password"> </form>
</body> </html>
Prg – 134
<html>
<head> <title> Forms - Radio Buttons </title> </head>
<body>
<form>
<input type="radio" name="gender" value="male" checked>Male<br>
<input type="radio" name="gender" value="female">Female
</form> </body> </html>
BIPU Lab Manual: Cpp programs – 38
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 135
<html>
<head> <title> Forms - Check Box </title> </head>
<body> defines a checkbox. Checkboxes let a user select ZERO or MORE options
of a limited number of choices.
<form>
<input type="checkbox" name="vehicle" value="Bike" checked>I have a bike<br>
<input type="checkbox" name="vehicle" value="Car">I have a car
</form> </body> </html>
Prg – 136
<html>
<head> <title> Forms - File Upload </title> </head>
<body> is used to upload file to the web server.
<form>
Select a File: <input type="file" name="fileupload">
</form> </body> </html>
Prg – 137
<html>
<head> <title> Forms – Push Button </title>
<script>
function msg( )
{
alert("Hello world!");
}
</script>
</head>
<body> A clickable button, that activates a JavaScript when it is clicked:
<form>
<input type="button" value="Click me" onclick="msg()">
</form> </body> </html>
Prg – 138
<html>
<head> <title> Forms - Submit </title> </head>
<body> It causes the browser to send the names and values to the CGI
application.
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit">
</form>
<p>Click on the submit button to submit the form.</p>
</body> </html>
Prg – 139
<html>
<head> <title> Forms - Image </title> </head>
<body> Allows you to substitute an image for the standard submit button.
<form action="demo_form.asp">
First name: <input type="text" name="fname"> <br>
Last name: <input type="text" name="lname"> <br>
<input type="image" src="img_submit.gif" alt="Submit" width="48"
height="48">
</form> </body> </html>
BIPU Lab Manual: Cpp programs – 39
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 140
<html>
<head> <title> Forms - Reset </title> </head>
<body> It allows the surfer to clear all the input in the form to default
values
<form action="demo_form.asp">
Email: <input type="text" name="email"> <br>
Password: <input type="text" name="pin" maxlength="4"> <br>
<input type="reset" value="Reset">
<input type="submit" value="Submit">
</form>
<p>Click on the reset button to reset the form.</p>
</body> </html>
Prg – 141
<html>
<head> <title> Forms - Text Area </title> </head>
<body> It allows the user to enter free from text entry.
<form action="demo_form.asp">
<textarea name="Remarks" rows="5" cols="50" wrap="virtual">
Your Comment goes here:
</textarea> </form> </body> </html>
Prg – 142
<html>
<head> <title> select - Drop Down List </title> </head>
<body> Create a drop-down list with four options:
<form >
<select name=“dropdown”>
<option value="PCMB">PCMB</option>
<option value="PCMC">PCMC</option>
<option value="HEBA">HEBA</option>
<option value="CEBA">CEBA</option>
</select>
</form> </body> </html>
Prg – 143
<html>
<head> <title> select - List Box </title> </head>
<body> It shows a declared number of text rows and gives the visitor a list
of choices. Similar to the drop list, the list box length is determined by
the length of the longest OPTION
<form >
<select name="listbox" size="3">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
</form> </body> </html>

BIPU Lab Manual: Cpp programs – 40


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 144
<html>
<head> <title> select - List Box Pre-selected</title> </head>
<body> List Box Pre-selected
<form >
<select name="listbox" size="3">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel" selected="selected">Opel</option>
<option value="audi">Audi</option>
</select>
</form> </body> </html>
Prg – 144(1)
<html>
<head> <title> select - List Box Pre-selected</title> </head>
<body> List Box Pre-selected
<form >
<select name="listbox" size="3">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi" selected="selected">Audi</option>
</select>
</form> </body> </html>
Prg – 145
<html>
<head> <title> select - Option Group</title> </head>
<body> The opt group is used to group related options in a drop-down list.
If you have a long list of options, groups of related options are easier to
handle for a user.
<form >
<select>
<optgroup label="Swedish Cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</optgroup>
<optgroup label="German Cars">
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</optgroup>
</select>
</form> </body> </html>
Prg – 146
<html>
<head> <title> select - Option Group Disabled</title> </head>
<body>
<form>
<select>
<optgroup label="Swedish Cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</optgroup>
BIPU Lab Manual: Cpp programs – 41
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

<optgroup label="German Cars" disabled>


<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</optgroup>
</select>
</form> </body> </html>
Prg – 147
<html>
<head> <title> Layouts - Using Table </title> </head>
<body> A simple way of creating layouts is by using the HTML <table> tag.
Using <table> to create a nice layout is NOT the correct use of the element.
The purpose of the <table> element is to display tabular data!
<table style="width:500px;" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" style="background-color:#FFA500;">
<h1 style="margin:0;padding:0;">Main Title of Web Page</h1>
</td>
</tr>
<tr>
<td style="background-color:#FFD700;width:100px; vertical-align:top;">
<b>Menu</b> <br>
HTML<br>
CSS<br>
JavaScript
</td>
<td style="background-color:#eeeeee;height:200px;width:400px;vertical-
align:top;"> Content goes here</td>
</tr>
<tr>
<td colspan="2" style="background-color:#FFA500;text-align:center;">
Copyright © W3Schools.com</td>
</tr> </table> </body> </html>
Prg – 148
<html>
<head> <title> Layouts - Using Div </title> </head>
<body>
<div id="container" style="width:500px">
<div id="header" style="background-color:#FFA500;">
<h1 style="margin-bottom:0;">Main Title of Web Page</h1></div>

<div id="menu" style="background-color:#FFD700; height:200px;


width:100px; float:left;">
<b>Menu</b><br>
HTML<br>
CSS<br>
JavaScript</div>

<div id="content" style="background-color:#EEEEEE; height:200px;


width:400px; float:left;"> Content goes here</div>
<div id="footer" style="background-color:#FFA500;clear:both;text-
align:center;"> Copyright © W3Schools.com</div>
</div> </body> </html>

BIPU Lab Manual: Cpp programs – 42


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Prg – 149
<html>
<head> <title> Span Tags </title> </head>
<body> A span element used to color a part of a text.
The span tag is used to group inline-elements in a document.
<p>My mother has <span style="color:blue; font-weight:bold">blue</span> eyes
and my father has <span style="color:darkolivegreen; font-weight:bold"> dark
green </span> eyes.</p>
</body> </html>

BIPU Lab Manual: Cpp programs – 43


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

I PUC
Text
book
programs

BIPU Lab Manual: Cpp programs – 44


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

prg1 to display a message Welcome to C++ programming on the screen


# include <iostream.h>
# include <conio.h>
void main( )
{
clrscr ( );
cout << "Welcome to C++ programming";
getch( );
}
Prg2 to display the following output using cout statements. “I PUC practical
programs. There are 30 C++ programs in the practical list”.
# include <iostream.h>
void main( )
{
cout << "I PUC practical programs" << endl;
cout << "There are 30 c++ programs in the practical list."
<< endl;
cout << "Complete all the programs before January." << endl;
}
/* C++ program to find the sum of two numbers */
# include <iostream.h>
void main( )
{
int a, b, sum;
cout << "Input two numbers\n";
cin >> a >> b;
sum = a + b;
cout << "Sum of two numbers= " << sum;
}
/* C++ program to find the average of three numbers*/
# include <iostream.h>
void main( )
{
int a, b, c;
float average;
cout << "Input three numbers\n";
cin >> a >> b >> c;
average = (a + b + c) / 3.0;
cout << "Average of three numbers = " << average;
}
//Program that reads temperature in Celsius and display it in Fahrenheit
# include <iostream.h>
void main( )
{
float C, F;
cout << "Input temperature in Celsius";
cin >> C;
F = 9 / 5.0 * C + 32;
cout << C << " Degree Celsius = " << F << " Degree Fahrenheit";
}

BIPU Lab Manual: Cpp programs – 45


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

/* C++ program to convert Fahrenheit to Celsius */


# include <iostream.h>
void main( )
{
int F;
float C;
cout << "Enter Fahrenheit";
cin >> F;
C = (F - 32) * 5 / 9;
cout << "Celsius = " << C;
}
/* C++ program to find the area of a triangle given base and height */
# include <iostream.h>
void main( )
{
int base, height;
float area;
cout << "Input base and height of the triangle ";
cin >> base >> height;
area = base * height / 2.0;
cout << "Area of the triangle = " << area;
}
/* C++ program to calculate simple interest */
# include <iostream.h>
void main( )
{
double principal, rate, time, si;
cout << "Input principal amount ";
cin >> principal;
cout << "Input Rate of interest ";
cin >> rate;
cout << "Input number of years ";
cin >> time;
si = (principal * rate * time) / 100;
cout << "simple interest= " << si;
}
/* C++ program to read a floating point number and display the rightmost digit
of the integral part of the number */
# include <iostream.h>
void main( )
{
float Number;
long Intpart;
int RightDigit;
cout << "Input a floating point number ";
cin >> Number;
Intpart = Number;
RightDigit = Intpart % 10;
cout << "Rightmost digit of the Integral part = " << RightDigit;
}

BIPU Lab Manual: Cpp programs – 46


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

// C++ program to display the square root of a number using setprecision


# include <math.h>
# include <iomanip.h>
main( )
{
int n;
cout << "Input a number\n ";
cin >> n;
cout << "Square root of " << n << " is " << sqrt(n) << endl;
cout << "Square root of " << n << " is "
<< setprecision(4) << sqrt(n);
return 0;
}
/* C++ program to find compound Interest */
#include<iostream.h>
#include<math.h>
void main( )
{
int p, t;
float r, ci;
cout << "Input principal, time, rate of interest values \n";
cin >> p >> t >> r;
ci = p * pow((1 + r / 100), t) – p;
cout << "Compound Interest = " << ci;
}
/* C++ program to read two floating point numbers add these numbers and
assign the result to an integer finally display the value of all the numbers */
# include <iostream.h>
void main( )
{
float Number1, Number2;
int IntSum;
cout << "Input two floating numbers "; cin >> Number1 >> Number2;
IntSum = Number1 + Number2;
cout << "Number1 = " << Number1 << endl;
cout << "Number2 = " << Number2 << endl;
cout << "Integer sum is = " << IntSum;
}
/* C++ program to calculate the salary of an employee */
# include <iostream.h>
void main( )
{
float BasicSal, NetSal;
int const HRA = 10,TA = 5;
cout << "Input Basic Salary ";
cin >> BasicSal;
NetSal = BasicSal + (BasicSal * HRA / 100) + (BasicSal * TA / 100);
cout << "Net Salary = " << NetSal;
}

BIPU Lab Manual: Cpp programs – 47


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

/* C++ program to find largest of two numbers */


# include <iostream.h>
void main( )
{
int a, b, Big;
cout << "Enter two numbers " << endl;
cin >> a >> b;
Big = a;
if(b > Big)
Big = b;
cout << "Largest of " << a << " and " << b << " is " << Big;
}
/* C++ program to calculate NetAmount and Discount */
# include <iostream.h>
void main( )
{
float Amount, Discount, NetAmount;
cout << "Enter sales amount ";
cin >> Amount;
Discount = 0;
if(Amount >= 5000)
Discount = 0.2;
Discount = Amount * Discount;
NetAmount = Amount – Discount;
cout << "Amount = " << Amount << endl;
cout << "Discount = "<< Discount << endl;
cout << "Net amount = "<< NetAmount << endl;
}
/* C++ program to input principal amount and time. If time is more than 10
years, calculate the simple interest at the rate of 8%, otherwise calculate it at
the rate of 12% per annum */
# include <iostream.h>
void main()
{
float principal, time, rate, simpleinterest;
cout << "Enter the principal " << endl;
cin >> principal;
cout << "Enter the time " << endl;
cin >> time;
rate = 12.0;
if(time > 10)
rate = 8.0;
simpleinterest = principal * time * rate / 100;
cout << "simple interest for " << time << " years = "
<< simpleinterest;
}
/* C++ program to check whether a number is even or odd */
# include <iostream.h>
void main()
{
int number;
cout << "Input number ";
cin >> number;
BIPU Lab Manual: Cpp programs – 48
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

if(number % 2 == 0)
cout << number << " is even";
else
cout << number << " is odd";
}
/* C++ program to input choice (1 or 2).If choice is 1, print the area of a circle
otherwise print the circumference of circle. Accept the radius of the circle from
user */
# include <iostream.h>
void main( )
{
float radius, Area, Circum;
int choice;
cout << " Circle problem menu\n ";
cout << "1. Area of the circle " << endl;
cout << "2. Circumference of the circle " << endl;
cout << "Enter your choice ";
cin >> choice;
cout << "Enter the radius of the circle ";
cin >> radius;
if(choice == 1)
{
Area = 3.142 * radius * radius;
cout << endl << "Area of the circle = "<< Area;
}
else
{ Circum = 2 * 3.142 * radius;
cout << "Circumference of the circle = " << Circum;
}
}
/*C++ program to find largest of three numbers using nested if */
# include <iostream.h>
void main( )
{
int a, b, c, Big;
cout << "Enter three numbers " << endl;
cin >> a >> b >> c;
if(a > b)
if(a > c)
Big = a;
else
Big = c;
else
if(b > c)
Big = b;
else
Big = c;
cout << "Largest of " << a << "," << b << " and " << c << " = "
<< Big << end;
}

BIPU Lab Manual: Cpp programs – 49


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

/* C++ program to find the largest, second largest and smallest of three
numbers */
# include <iostream.h>
void main( )
{
int a, b, c, largest, small, seclar;
cout << "Enter three numbers " << endl;
cin >> a >> b >> c;
if(a > b)
{
largest = a; small = b;
}
else
{
largest = b; small = a;
}
if(largest < c)
largest = c;
else if(small > c)
small = c;
cout << "Largest number = " << largest << endl;
cout << "Smallest number = " << small << endl;
seclar = (a + b + c) - (largest + small);
cout << "Second Largest = " << seclar << endl;
}
/* C++ program to check whether a date is valid or invalid */
# include <iostream.h>
void main()
{
int dd, mm, yy, valid = 1;
cout << "Input a date ";
cin >> dd >> mm >> yy;
if((dd <= 0) || (dd > 31) || (mm <= 0) || (mm > 12) || (yy <= 0))
valid = 0;
else if((mm == 4 || mm == 6 || mm == 9 || mm == 11) && (dd > 30))
valid = 0;
else if(mm == 2)
{
if(yy % 4 == 0)
{
if(dd > 29)
valid = 0;
}
else if( dd > 28)
valid = 0;
}
cout << dd << " / " << mm << " / " << yy;
if(valid)
cout << " Date is valid";
else
cout << " Date is invalid";
}

BIPU Lab Manual: Cpp programs – 50


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

/* C++ program to find largest of three numbers using if-else ladder */


# include <iostream.h>
void main( )
{
int a, b, c, largest;
cout << "Enter three numbers " << endl;
cin >> a >> b >> c;
if((a > b) && (a > c))
largest = a;
else if(b > c)
largest = b;
else
largest = c;
cout << "Largest of " << a << "," << b << "," << c << " = "
<< largest;
}
/* C++ program to grade the students according to following rules
Marks Grade
70 to 100 Distinction
60 to 69 First Class
50 to 59 Second Class
40 to 49 Pass
0 to 39 Fail */
# include <iostream.h>
void main( )
{
int percentage;
cout << "Enter percentage of marks ";
cin >> percentage;
if((percentage >= 70) && (percentage <= 100))
cout << "Distinction";
else if(percentage >= 60)
cout << "First Class";
else if(percentage >= 50)
cout << "Second Class";
else if(percentage >= 40)
cout << "Pass";
else
cout << "Fail";
}
program to check if a character is a letter or a digit or a special character */
# include <iostream.h>
void main( )
{ char ch;
cout << "Input a character ";
cin >> ch;
if(((ch >= 'A') && (ch <= 'Z')) || ((ch >= 'a') && (ch <= 'z')))
cout << ch << " is a letter";
else if((ch >= '0') && (ch <= 9))
cout << ch << " is a number";
else
cout << ch << " is a special character";
}
BIPU Lab Manual: Cpp programs – 51
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

// C++ program to check whether an entered character is a vowel or not


# include <iostream.h>
void main( )
{
char ch;
cout << "Input a character ";
cin >> ch;
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case '0':
case 'u': cout << ch << " is a vowel";
break;
default: cout << ch << " is a consonant";
}
}
/* C++ program to enter a number from 1 to 7 and display the corresponding
day of the week using switch statement */
# include <iostream.h>
void main( )
{
int day;
cout << "Input any number between 1 and 7\n";
cin >> day;
switch(day)
{
case 1: cout << " Sunday "; break;
case 2: cout << " Monday "; break;
case 3: cout << " Tuesday "; break;
case 4: cout << " Wednesday "; break;
case 5: cout << " Thursday "; break;
case 6: cout << " Friday "; break;
case 7: cout << " Saturday "; break;
default: cout << "Invalid day";
}
}
/* C++ program to simulate the four arithmetic operations */
# include <iostream.h>
void main( )
{
int num1, num2, result;
char op;
cout << "Enter two numbers ";
cin >> num1 >> num2;
cout << "Enter an operator ";
cin >> op;
BIPU Lab Manual: Cpp programs – 52
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

switch(op)
{
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/': if(num2 != 0)
result = num1 / num2;
else
{
cout << "Warning: Division by zero";
result = 0;
}
break;
default: cout << "Unknown operator"; result = 0;
}
cout << " " << result;
}
/* C++ program to calculate sum of first 100 natural numbers */
# include <iostream.h>
void main( )
{
int sum = 0, num = 1;
while(num <= 100)
{
sum = sum + num;
num = num + 1;
}
cout << "Sum = " << sum;
}
/* C++ program to display the digits of a number */
# include <iostream.h>
void main( )
{
int num, digit;
cout << "Enter a number " << endl;
cin >> num;
while(num > 0)
{
digit = num % 10;
num = num / 10;
cout << "digit = " << digit << endl;
}
}
/* C++ program to reverse a number */
# include <iostream.h>
void main( )
{
long rev = 0, n, num;
int digit;
cout << "Enter a number ";
cin >> num;
n = num;

BIPU Lab Manual: Cpp programs – 53


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

while(num != 0)
{
digit = num % 10;
num = num / 10;
rev = rev * 10 + digit;
}
cout << "The number is " << n << endl;
cout << "Reverse of a number = " << rev;
}
/* C++ program to calculate the average of numbers entered by the user. The
list terminates when the number entered is zero */
# include <iostream.h>
void main( )
{
int num, sum = 0, count = 0;
float avg;
cout << "Input a number (Enter 0 to exit) ";
cin >> num;
while(num!=0)
{
sum = sum + num; count++;
cout << "Input a number (Enter 0 to exit) ";
cin >> num;
}
avg = (float) sum / count;
cout << "Total numbers = " << count << endl;
cout << "Sum of " << count << " numbers = " << sum << endl;
cout.precision(6);
cout << "Average of " << count << " numbers = "<< avg;
}
/* C++ program to generate the multiplication table of 5 */
# include <iostream.h>
void main( )
{
int result, num, count = 1;
cout << "Enter a number ";
cin >> num;
cout << "The table is " << endl;
do
{
result = num * count;
cout << num << "*" << count << "=" << result << endl;
++count;
}while(count <= 10);
}
// C++ program to calculate the compound interest using DO-WHILE
# include <iostream.h>
void main()
{ float p, amt, r, ci;
int time, year = 1;
cout << "Enter principal amount, rate of interest and time ";
cin >> p >> r >> time;
amt = p;
BIPU Lab Manual: Cpp programs – 54
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

do
{
amt = amt * (1 + r / 100);
++year;
}while(year <= time);
ci = amt - p;
cout << "Compound Interest earned = " << ci;
}
/* A Company gives 10% commission for toothpaste and 15% commission for
soaps. Calculate and output the total commission for each salesmen.*/
# include <iostream.h>
void main( )
{
int slno;
float paste, soap, commission;
char ch;
do
{
cout << "Enter salesman number ";
cin >> slno;
cout << "Enter the total sales of toothpastes and soaps ";
cin >> paste >> soap;
commission = (paste * 0.1 + soap * 0.15);
cout << "Salesman number = " << slno << endl;
cout << "commission = " << commission << endl;
cout <<"continue(y/n)?";
cin >> ch;
}while((ch == 'y') || (ch == 'Y')); }
/* C++ program to generate N natural number using for loop */
# include <iomanip.h>
main( )
{
int n;
cout << endl << "Enter the upper limit ";
cin >> n;
for(int i = 1; i <= n; i++)
cout << i << setw(7);
return 0;
}
/* C++ program to calculate and print the sum of even and odd integers of the
first N natural number */
# include <iostream.h>
void main()
{ int n, even = 0,odd = 0;
cout <<"Enter the upper limit ";
cin >> n;
for(int i = 0; i <= n; i++)
{
if(i % 2 == 0)
even = even + i;
else
odd = odd + i;
}
BIPU Lab Manual: Cpp programs – 55
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

cout << "Sum of even integer = " << even;


cout << "\nSum of odd integer = "<<odd;
}
/* program to generate divisors factors of a number */
# include <iomanip.h>
main( )
{
int n, i;
cout << "Input a number ";
cin >> n;
cout << "Factors of " << n << " are: " << endl;
for(i = 1; i <= n; i++)
{
if(n % i == 0)
cout << setw (7) << i;
}
return 0;
}
/* C++ program to calculate sum of the series X + X^2 + x^3 +...+ x^n */
# include <iostream.h>
void main( )
{ int n, x, i, sum = 0, term;
cout << "Input x value ";
cin >> x;
cout << "Input number of terms in series ";
cin >> n;
term = x;
for(i = 1; i <= n; i++)
{
sum = sum + term;
term = term * x;
}
cout << "Sum of the series = " << sum;
}
/* C++ program to check whether a number is a prime number or not. A number
N > 1 is a prime only if N has only two factors 1 and itself */
# include <iostream.h>
void main( )
{ int i, n, factors = 0;
cout << "Input a number ";
cin >> n;
for(i = 1; i <= n; i++)
if(n % i == 0)
{
factors++;
cout << i << endl;
}
cout << "Number " << n << " has " << factors << " Factors \n";
if(factors == 2)
cout << n << " is a prime number ";
else
cout << n << " is not a prime number ";
}
BIPU Lab Manual: Cpp programs – 56
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

/* C++ program to generate the following output


Line 1 1 2 3 4
Line 2 1 2 3 4
Line 3 1 2 3 4
Line 4 1 2 3 4 */
# include <iomanip.h>
main( )
{
int i, j;
for(i = 1; i <= 4; i++)
{
cout << "Line " << i;
for(j = 1; j <= 4; j++)
{
cout << setw(4) << j;
}
cout << endl;
}
return 0;
}

/* C++ program to generate the following output


*
* *
* * *
* * * *
*/
# include <iomanip.h>
void main( )
{
int i, j;
for(i = 1; i <= 4; i++)
{
for(j = 1; j <= i; j++)
{
cout << "*" << setw(4);
}
cout << endl;
}
}
/* C++ program to generate the following output
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFGHIJK
*/
# include <iostream.h>
void main()
{
int i, j, k, col = 5;
char ch;
BIPU Lab Manual: Cpp programs – 57
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

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


{
for(k = 1; k <= col; k++)
cout << " ";
ch = 'A';
for(j = 1; j <= 2 * i - 1; j++, ch++)
cout << ch;
cout << endl;
col--;
}
}
/*C++ program to generate the following output
1
23
456
7 8 9 10 */
# include <iomanip.h>
void main( )
{
int i, j, k = 0;
for(i = 1; i <= 4; i++)
{
for(j = 1; j <= i; j++)
{
cout << setw(3) << ++k;
}
cout << endl;
}
}
/* C++ program to generate the following output
12345
51234
45123
34512
23451 */
# include <iostream.h>
void main( )
{ int i;
long num = 12345;
for(i = 1; i <= 5; i++)
{
cout << " " << num;
num = (num % 10) * 10000 + (num / 10);
cout << endl;
}
}
/* C++ program to generate following output
********************
* *
* *
******************** */

BIPU Lab Manual: Cpp programs – 58


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

# include <iostream.h>
void main()
{
int i, j;
for(i = 1; i <= 5; i++)
{
if(i == 1 || i == 5)
for(j = 1; j <= 20; j++)
cout << "*";
else
{
cout << "*";
for(j = 1; j <= 18; j++)
cout << " ";
cout << "*";
}
cout << endl;
}
}
/* C++ program to generate N natural number using goto */
# include <iomanip.h>
void main()
{
int n, i = 1;
cout << "Enter number ";
cin >> n;
loop:
cout << setw (4) << i++;
if(i <= n)
goto loop;
}
//program to find the given number is prime or not using break function
# include <iostream.h>
void main()
{
int n, i, prime;
cout << "Input a number ";
cin >> n;
prime = 1;
for(i = 2; i <= n / 2; i++)
{
if(n % i == 0)
{
prime = 0;
break;
}
}
if(prime == 1)
cout << "it is prime";
else
cout << "It is not prime";
}

BIPU Lab Manual: Cpp programs – 59


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

/* C++ program to display all numbers from 1 to N which are not divisible by 5
using continue */
# include <iomanip.h>
main( )
{
int n, i = 0;
cout <<" Input a number ";
cin >> n;
while(i < n)
{
i++;
if(i % 5 == 0)
continue;
cout << setw(3) << i;
}
return 0;
}
// C++ program to check whether a number is prime or not using exit function
# include <iostream.h>
# include <stdlib.h> // for exit command
void main( )
{
int n, i;
cout << "Input a number ";
cin >> n;
for(i = 2; i <= n / 2; i++)
{
if(n % i == 0)
{
cout << n << " is not a prime";
exit(0);
}
}
cout << n << " is a prime";
}
//greatest common divisor
# include <iostream.h>
void main( )
{
int x, y;
cout << "Enter x and y values";
cin >> x >> y;
while(x != y)
{
if(x > y)
x = x – y;
else
y = y – x;
}
cout << x;
}

BIPU Lab Manual: Cpp programs – 60


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

/* C++ program to generate the following output


1
2 3
4 5 6
7 8 9 10
*/
# include <iostream.h>
void main( )
{
int i, j, k = 1;
for(i = 1; i <= 4; i++)
{
for(j = 1; j <= i; j++)
cout << ‘\t’ << k++;
cout << endl;
}
}
/* C++ program to generate following output
1
1 2
1 2 3
1 2 3 4
*/
#include<iostream.h>
void main( )
{
int i, j;
for(i = 1; i <= 4; i++)
{
for(j = 1; j <= i; j++)
cout << j << ‘\t’;
cout << endl;
}
}
/* C++ program to generate following output
1
2 2
3 3 3
4 4 4 4
*/
# include <iostream.h>
void main( )
{
int i, k;
for(i = 1; i <= 4;i++)
{
for(k = 1; k <= i; k++)
cout << i << ‘\t’;
cout << endl;
}
}

BIPU Lab Manual: Cpp programs – 61


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

/* C++ program to generate the following output


1 2 3 4
1 2 3
1 2
1
*/
# include <iostream.h>
void main( )
{
int i, j;
for(i = 4; i >= 1; i--)
{
for(j = 1; j <= i; j++)
cout << j << ‘\t’;
cout << endl;
}
}
/* C++ program to generate the following output
1
21
321
*/
#include<iostream.h>
void main()
{
int i, j;
for(i = 1; i <= 3; i++)
{
for(j = i; j >= 1; j--)
cout << j << ‘\t’;
cout << endl;
}
}
/* C++ program to read and print one dimensional array */
# include <iostream.h>
void main( )
{
int a[50], n, i;
cout << "Enter number of elements in the array\n";
cin >> n;
cout << "Enter the array elements " << endl;
for(i = 0; i < n; i++)
{
cout << "Input the value for number " << (i + 1);
cin >> a[i];
}
cout << endl << "array elements are " << endl;
for(i = 0; i < n; i++)
cout << "Number " << (i + 1) << "=" << a[i] << endl;
}

BIPU Lab Manual: Cpp programs – 62


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

//C++ program to generate N Fibonacci numbers using assignment statement


#include<iostream.h>
void main( )
{
int n, i, fibo[20];
cout << "Input number of elements in the series ";
cin >> n;
fibo[0] = 0;
fibo[1] = 1;
for(i = 2; i < n; i++)
{
fibo[i] = fibo[i – 1] + fibo[i – 2];
}
cout << "Fibonacci numbers are " << endl;
for(i = 0;i < n; i++)
cout << ‘\t’ << fibo[i];
}
/* C++ program to find the largest element and its position in an array of N
elements */
# include <iostream.h>
void main( )
{
int n, i, lar, pos, a[100];
cout << "Input number of elements in the array ";
cin >> n;
cout << "Input " << n << " numbers " <<endl;
for(i = 0; i < n; i++)
{
cout << "Input Number " << (i + 1) << " value = ";
cin >> a[i];
}
lar = a[0];
pos = 0;
for(i = 1; i < n; i++)
{
if(lar < a[i])
{
lar = a[i] ;
pos = i;
}
}
Cout << "Largest element is " << lar
<< " and its position is " << (pos + 1);
}
/* C++ program to read N student marks and find the class average using one
dimensional array */
# include <iostream.h>
void main()
{
int n, i, marks[50], total = 0;
float avg;
cout << "Input number of students in a class ";
cin >> n;
BIPU Lab Manual: Cpp programs – 63
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

cout << "In put " << n << " student marks: " << endl;
for(i = 0; i < n; i++)
{
cout << "Input marks of student " << (i + 1) << " = ";
cin >> marks[i];
total = total + marks[i];
}
avg = (float) total / n;
cout.precision(4);
cout << "class average marks = " << avg;
}
/* C++ program to read marks of N students in the range of 1-100 then make 11
groups G0(0-9), G1(10-19) G3(20-29) ..... G10(100)*/
# include <iomanip.h>
void main()
{
int marks[100], n, i;
int group[11] = {0};
cout << "Input number of students ";
cin >> n;
cout << "Input " << n << "students marks:" << endl;
for(i = 0; i < n; i++)
{
cout << "Input marks for student " << (i + 1) << " = ";
cin >> marks[i];
group [ marks [i] / 10] ++;
}
cout << "*********************" << endl;
cout << setw(13) << "group" << setw(13) << "Frequency \n";
for(i = 0;i <= 10; i++)
cout << setw(10) << i << setw(10) << group[i] << endl;
}
/* C++ program to check whether an array is in ascending order, descending
order or unsorted one */
# include <iostream.h>
void main()
{
int n, i, a[100], ascend = 1, descend = 1;
cout << "Input number of elements in the array ";
cin >> n;
cout << "Input " << n << " numbers: " << endl;
for(i = 0;i < n; i++)
{
cout << "Input number " << (i + 1) << " value = ";
cin >> a[i];
}
for(i = 0; i < n – 1; i++)
{
if(a[i] > a[i + 1]) ascend = 0;
if(a[i] < a[i + 1]) descend = 0;
}

BIPU Lab Manual: Cpp programs – 64


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

if(ascend == 1)
cout << "Numbers are arranged in ascending order ";
else if(descend == 1)
cout << "Numbers are in descending order ";
else
cout << " array is unsorted one ";
}
/* C++ program to reverse a one-dimensional array of integers */
# include <iomanip.h>
void main()
{
int n, i, r, temp, a[100];
cout << "Input number of elements in the array ";
cin >> n;
for(i = 0; i < n; i++)
{
Cout << "Input number " << (i + 1) << "value = ";
cin >> a[i];
}
cout << "Original array elements are ";
for(i = 0; i < n; i++)
cout << setw(5) << a[i];
for(i = 0, r = n – 1; i < n / 2; i++, r--)
{
temp = a[i];
a[i] = a[r];
a[r] = temp;
}
cout << endl << "Reverse array elements are " << endl;
for(i = 0; i < n; i++)
cout << setw(5) << a[i];
}
/* C++ program to accept temperature of each day of the month and print
maximum temperature and its day in that month, minimum temperature and
its day in that month, and also find the average temperature of the month */
# include <iomanip.h>
main( )
{
int n, i, maxday, minday;
float temp[31], tottemp = 0, maxtemp, mintemp, avgtemp;
cout << "Input number of days in the month ";
cin >> n;
cout << "Input " << n << " days temperature " << endl;
for(i = 0; i < n; i++)
{
cout << "Input temperature of day " << (i + 1) << " = ";
cin >> temp[i];
}
tottemp = temp[0];
maxtemp = temp[0];
maxday = 0;
mintemp = temp[0];
minday = 0;
BIPU Lab Manual: Cpp programs – 65
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

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


{
tottemp = tottemp + temp[i];
if(maxtemp < temp[i])
{
maxtemp = temp[i];
maxday = i;
}
if(mintemp > temp[i])
{
mintemp = temp[i];
minday = i;
}
}
avgtemp = tottemp / n;
cout << setprecision(4);
cout << "Average temperature of the month = " << avgtemp << endl;
cout << "maximum temperature is " << maxtemp
<< " on day " << (maxday + 1) << endl;
cout << "minimum temperature is " << mintemp
<< " on day " << (minday + 1);
return 0;
}
/* C++ program to find the number of positive numbers and their sum , number
of negative numbers and their sum in a list of N numbers */
# include <iostream.h>
void main()
{
int n, i, a[100];
int psum = 0, pcount = 0, nsum = 0, ncount = 0;
cout << "Input number of elements in the array ";
cin >> n;
for(i = 0; i < n; i++)
{
cout << "Input number " << (i + 1) << " value = ";
cin >> a[i];
}
for(i = 0; i < n; i++)
{
if(a[i] > 0)
{
psum = psum + a[i]; pcount ++;
}
if(a[i] < 0)
{
nsum = nsum + a[i]; ncount ++;
}
}
cout << "There are " << pcount << " positive numbers "
<< "and their sum = " << psum << endl;
cout << "There are " << ncount << " negative numbers "
<< " and their sum= " << nsum;
}
BIPU Lab Manual: Cpp programs – 66
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

//C++ program to find the sum of row elements of a matrix of order m x n


# include <iomanip.h>
main( )
{
int m, n, i, j, a[10][10], rsum;
cout << "Input order of the matrix (number of rows and column) : ";
cin >> m >> n;
cout << " Input matrix elements: " << endl;
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
cin >> a[i][j];
cout << " Matrix with row sum " << endl;
for(i = 0; i < m; i++)
{
rsum = 0;
for(j = 0; j < n; j++)
{
rsum = rsum + a[i][j];
cout << setw(5) << a[i][j];
}
cout << "--row " << (i + 1) << " sum = " << rsum;
}
return 0;
}
/* C++ program to find the maximum and minimum elements in a matrix of
order mxn */
# include <iomanip.h>
main( )
{
int m, n, i, j, max, min, a[10][10];
cout << " Input order of a matrix ";
cin >> m >> n;
cout << " Input matrix elements " << endl;
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
cin >> a[i][j];
max = a[0][0];
min = a[0][0];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if(min > a[i][j])
min = a[i][j];
if(max < a[i][j])
max = a[i][j];
}
}
cout << " maximum element is " << max << endl;
cout << " minimum element is " << min;
return 0;
}

BIPU Lab Manual: Cpp programs – 67


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

/* C++ program there are N students. Each student will take the examination in
3 subjects.
I. The marks obtained by each student in different subjects.
II. Total marks and average marks obtained by each student.
III. The maximum marks from three subjects. */
# include <iomanip.h>
void main()
{
int n, i, j, marks[10][3];
int total, max;
float avg;
cout << " Input number of students in a class ";
cin >> n;
cout << " Input " << n << " students marks in 3 subjects " << endl;
for(i = 0; i < n; i++)
{
cout << " Student " << (i + 1) << " ";
for(j = 0; j < 3; j++)
cin >> marks[i][j];
}
cout << setw(16) << "sub 1" << setw(7)
<< "sub 2" << setw(7) << "sub 3";
cout << setw(7) << " total " << setw(10)
<< " average" << setw(15);
cout << "max marks " <<endl;
for(i = 0; i < n; i++)
{
total = 0;
max = marks[0][0];
cout << "student" << (i + 1);
for(j = 0; j < 3; j++)
{
total = total + marks[i][j];
if(max < marks[i][j])
max = marks[i][j];
cout << setw(7) << marks[i][j];
}
cout << setprecision(4);
avg = (float) total / 3;
cout << setw(7) << total << setw(9) << avg << setw(12) << max;
}
return 0;
}
// C++ program to read a matrix of order MxN and display its transpose
# include <iomanip.h>
main( )
{
int m, n, i, j, a[10][10], trans[10][10];
cout << "Input order of matrix ";
cin >> m >> n;
cout << "Input matrix elements " << endl;

BIPU Lab Manual: Cpp programs – 68


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

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


{
for(j = 0; j < n; j++)
{
cin >> a[i][j];
trans[j][i] = a[i][j];
}
}
cout << "Original matrix elements are: \n";
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cout << setw(5) << a[i][j];
cout << endl;
}
cout << "Transpose matrix elements are \n";
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cout << setw(5) << trans[i][j];
cout << endl;
}
return 0;
}
/* C++ program to read a matrix of order MxN. Find the sum of principal
diagonal elements and display its value */
# include <iomanip.h>
void main( )
{
int m, n, i, j, a[10][10], diasum;
cout << " Input order of matrix "; cin >> m >> n;
cout << " Input matrix elements " << endl;
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
cin >> a[i][j];
if(m == n)
{
diasum = 0;
cout << " Principal diagonal elements are: " << endl;
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
if(i == j)
{
cout << setw(5) << a[i][j];
diasum = diasum + a[i][j];
}
cout << endl << " Sum of principal diagonal elements = "
<< diasum;
}
else
cout << " Not a square matrix,”
<< “ hence principal diagonal does not exit ";
}
BIPU Lab Manual: Cpp programs – 69
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

/* C++ program to check whether a matrix is identity or not */


# include <iomanip.h>
main( )
{
int m, n, i, j, a[10][10], identity;
cout << " Input order of the matrix ";
cin >> m >> n;
cout << " Input matrix elements " << endl;
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
cin >> a[i][j];
if(m == n)
{
identity = 1;
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
{
if(i == j)
{
if(a[i][j] != 1)
identity = 0;
}
else
{
if(a[i][j] != 0)
identity = 0;
}
}
if(identity == 1)
cout << " Matrix is identity(unit)";
else
cout << " Matrix is not an identity(unit) ";
}
else
cout << " Not a square matrix ";
return 0;
}
/* C++ program to find product of two matrix */
# include <iomanip.h>
main( )
{
int a[10][10], b[10][10], c[10][10], m, n, i, j, k;
cout << " Input order of matrix \n";
cin >> m >> n;
cout << " Input elements of first matrix \n";
for(i = 0;i < m; i++)
for(j = 0; j < n; j++)
cin >> a[i][j];
cout << " Input element of second matrix \n";
for(i = 0; i < m; i++)
for(j = 0;j < n; j++)
cin >> b[i][j];

BIPU Lab Manual: Cpp programs – 70


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

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


{
for(j = 0; j < n; j++)
{
c[i][j] = 0;
for(k = 0; k < n; k++)
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
cout << "Product of two matrix is \n";
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cout << setw(5) << c[i][j];
cout << endl;
}
return 0;
}
/* C++ program to read a string using cin */
#include<iostream.h>
void main( )
{
char str[100];
cout << " Enter a string \n";
cin >> str;
cout << " String is "<<str;
}
/* C++ program to read a string using cin.get( ) function */
# include <iostream.h>
void main( )
{
char ch;
cout << " Enter a string \n";
cin.get(ch);
while(ch != '\n')
{
cout << ch;
cin.get(ch);
}
}
/* c++ program to work with getline( ) and write( )functions */
# include <iostream.h>
void main()
{
char str[50];
cout<<" Input a text ";
cin.getline(str,10);
cout.write(str, 5);
cout << endl;
cout.write(str, 2);
}

BIPU Lab Manual: Cpp programs – 71


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

// c++ program to find the length of the string without using strlen( )
# include <iostream.h>
void main( )
{
char str[100];
int i, len;
cout << "Input a string ";
cin.getline(str, 100);
cout << "string is " << str << endl;
for(i = 0; str[i] != '\0'; i++); // terminate for loop by semicolon
len = i;
cout << "Length of the string is " << len;
}
// c++ program to copy one string to another without using strcpy( )
# include <iostream.h>
void main( )
{
char str[100],dstr[100];
int i;
cout << "Input a string ";
cin.getline(str, 100);
cout << "Source string is " << str << endl;
for(i = 0; str[i] != '\n'; i++)
dstr[i] = str[i];
dstr[i] = '\0';
cout << "Destination string is " << dstr;
}
/* c++ program to find the occurance of a specified character */
# include <iostream.h>
void main( )
{
int i, count;
char ch, str[100];
cout << "Input a string ";
cin.getline(str, 100);
cout << "String is " << str <<endl;
cout << "Input a character to be counted ";
cin.get(ch);
for(count = 0, i = 0;str[i] != '\0'; i++)
if(str[i] == ch)
count ++;
cout << ch << " occurs " << count << " times";
}
// C++ program to concatenate (join) two strings without using strcat( )
# include <iostream.h>
void main( )
{
char str1[50], str2[50], str3[100];
int i, j;
cout << "Input the first string ";
cin.getline(str1, 50);
cout << "Input the second string ";
cin.getline(str2, 50);
BIPU Lab Manual: Cpp programs – 72
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

for(i = 0; str1[i] != '\0'; i++)


str3[i] = str1[i];
for(j = 0; str2[j] != '\0'; j++, i++)
str3[i] = str2[j];
str3[i] = '\0';
cout << "First string = " << str1 << endl;
cout << "Second string = " << str2 << endl;
cout << "concatenated string is = " << str3;
}
/* C++ program to convert all character of a string from lower case to upper
case without using strupr function(method 1) */
# include <iostream.h>
void main( )
{
char str[100];
int i;
cout << "Input a string in lower case:";
cin.getline(str, 100);
cout << "Original string is " << str << endl;
for(i = 0; str[i] != '\0'; i++)
if(str[i] >= 'a' && str[i] <= 'z')
str[i] = str[i] – 32;
cout << "converted string in upper case is " << str;
}
/* C++ program to convert all character of a string from lower case to upper
case without using strupr( ) function (method 2) */
# include <iomanip.h>
main( )
{
char str[100];
int i;
cout << "Input a string in lower case:";
cin.getline(str, 100);
cout << "Original string is " << str << endl;
for(i = 0; str[i] != '\0'; i++)
if(str[i] >= 97 && str[i] <= 122)
str[i] = str[i] - 32;
cout << "converted string in upper case is " << str;
return 0;
}
// C++ program to comapre two strings without using strcmp( ) function
# include <iostream.h>
void main( )
{
char str1[100], str2[100];
int i, flag;
cout << "Input two strings of same size \n ";
cout << "Input first string ";
cin.getline(str1, 100);
cout << "Input second string ";
cin.getline(str2, 100);
flag = 1;

BIPU Lab Manual: Cpp programs – 73


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

for(i = 0; str1[i] != '\0'; i++)


if(str1[i] != str2[i])
{
flag = 0;
break;
}
if(flag == 1)
cout << "Two strings are equal ";
else
cout << "Two strings are not equal ";
}
/* C++ program to reverse a string without using strrev( ) function*/
# include <iostream.h>
void main( )
{
char str[100], temp;
int i, j, k;
cout << "Input a string ";
cin.getline(str, 100);
cout <<"Original string is " << str << endl;
for(i = 0; str[i] != '\0'; i++)
{
for(j = 0, k = i – 1;j < i / 2; j++, k--)
{
temp = str[j];
str[j] = str[k];
str[k] = temp;
}
}
cout << "Reversed string is " << str;
}
/* C++ program to read and display N names */
# include <iomanip.h>
main( )
{
int n,i;
char name[10][20];
cout << "Input number of names \n";
cin >> n;
cout << "Input " << n << " name ";
for(i = 0; i < n; i++)
cin >> name[i];
cout << "Name list is: " << endl;
for(i = 0; i < n; i++)
cout << "Name " << (i + 1) << " " << name[i] << endl;
return 0;
}

BIPU Lab Manual: Cpp programs – 74


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

// C++ program to check whether given charater is alphanumeric or digit


#include<iostream.h>
#include<ctype.h>
void main()
{
char ch;
cout << "Input character ";
cin.get(ch);
if(isalpha(ch))
cout << ch << " is alphabet ";
else if(isdigit(ch))
cout << ch << " is digit ";
else
cout << " It is special character ";
}
prgam to count no.of lower case,upper case,digits, special characters in a string
#include<iostream.h>
#include<ctype.h>
void main()
{
char str[100];
int ucase = 0, lcase = 0, dig = 0, spl = 0, i;
cout << "Input a string \n";
cin.getline(str, 100);
for(i = 0; str[i] != '\0'; i++)
{
if(isupper(str[i]))
ucase++;
else if(islower(str[i]))
lcase++;
else if(isdigit(str[i]))
dig++;
else
spl++;
}
cout << "No. of uppercase character= " << ucase << endl;
cout << "No. of lowercase character= " << lcase << endl;
cout << "NO. of digits = " << dig << endl;
cout << "No. special character = " << spl;
}
// C++ program to join to strings using strcat( )function
#include <stdio.h>
#include<iostream.h>
#include<string.h>
void main()
{
char str1[100], str2[100];
cout << "Enter first string \n";
cin.getline(str1, 100);
cout << "Enter second string\n";
cin.getline(str2, 100);
cout << "resultant string " << strcat(str1, str2);
}
BIPU Lab Manual: Cpp programs – 75
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

prg to find the location of a character in a given string using strchr( ) function
#include<iostream.h>
#include<string.h>
void main()
{
char name[50], ch;
cout << "Enter a string \n";
cin.getline(name, 50);
cout << "Enter the character to find its position in the string\n";
cin >> ch;
char *a = strchr(name, ch);
cout << "position of character " << ch << " in given string is "
<< (a - name + 1);
}
program to illustrate difference between strcmp() and strcmpi() functions
#include<iostream.h>
#include<string.h>
void main()
{
char s1[100], s2[100];
cout << "Input string1 \n";
cin.getline(s1, 100);
cout << "Input string2 \n";
cin.getline(s2, 100);
/* if(strcmp(s1, s2) == 0)
cout << "String are equal\n";
else
cout << "string are not equal\n"; */
if(strcmpi(s1, s2) == 0)
cout << "String are equal\n";
else
cout << "String are not equal";
}
// C++ program to illustrate strcpy, strlen and strrev
#include<iostream.h>
#include<string.h>
void main()
{
char str[100], str1[100];
cout << "Input a string\n " ;
cin.getline(str, 100);
strcpy(str1, str);
cout << "No. of characters = " << strlen(str) << endl;
cout << "Reversed string = " << strrev(str) << endl;
cout << "copied string = " << str1;
}
prg to join n characters from second string to first string using strncat() fun
#include<iostream.h>
#include<string.h>
void main()
{
char s1[100], s2[100];
cout << "Input string 1\n";
BIPU Lab Manual: Cpp programs – 76
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

cin.getline(s1, 100);
cout << "Input string 2\n";
cin.getline(s2, 100);
cout << "Enter number of characters to be joined\n"
<< "to first string from second string ";
int n;
cin >> n;
cout << "String after joining : " << strncat(s1, s2, n) << endl;
}
//C++ program to compare n characters in two strings using strncmp()
# include <string.h>
# include <iostream.h>
main( )
{
char str1[50], str2[50];
int n, res;
cout << "Enter string one characters \n";
cin.getline(str1, 50);
cout << "Enter string two characters \n";
cin.getline(str2, 50);
cout << "Enter number of characters to be compraed \n";
cin >> n;
res = strncmp(str1, str2, n);
if(res == 0)
cout << "Strings are equal";
else if(res > 0)
cout << "String 1 is greater than string 2 ";
else
cout << "String 2 is greater than string 1 ";
return 0;
}
program to copy n characters from string 2 to string 1 using strncpy function
#include<iostream.h>
#include<string.h>
main()
{
char str1[50], str2[25];
int n;
cout << "Enter the first string\n";
cin.getline(str1, 25);
cout << "Enter the second string\n";
cin.getline(str2, 25);
cout << "Enter number of characters to be copied"
<<" from string 2 to string 1 \n";
cin >> n;
strncpy(str1, str2, n);
cout << "First string after copying from second string is "<< str1;
return 0; }

BIPU Lab Manual: Cpp programs – 77


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

program to replace n characters in a given string using strnset function


# include <iostream.h>
# include <conio.h>
# include <string.h>
main( )
{
char str[50], ch;
int n;
cout << "Type a sentence : \n";
cin.getline(str, 49);
cout << "Type a character to be replaced in the above sentence : ";
cin >> ch;
cout << "Type a number to replace the "
<< "character in the above sentence : \n";
cin >> n;
clrscr( );
cout << "Sentence before replacing the characater : \n"
<< str;
cout << "\nSentence after replacing the character : \n"
<< strnset (str,ch,n);
return 0;
}
program to read a string and convert uppercase to lower case and vice-versa
# include <iostream.h>
# include <ctype.h>
void main( )
{ char str[100];
int i;
cout << "Input a string\n";
cin.getline(str, 100);
for(i = 0; str[i] != '\0'; i++)
{
if(isupper(str[i]))
str[i] = tolower(str[i]);
else if(islower(str[i]))
str[i] = toupper(str[i]);
}
cout << "converted string is " << str;
}
/* C++ program to check a word is palindrome using string library function */
# include <iostream.h>
# include <string.h>
void main( )
{ char str[100], rev[100];
cout << "Input a string\n";
cin.getline(str, 100);
strcpy(rev, str);
strrev(rev);
if(strcmp(str, rev) == 0)
cout << str << " is a palindrome ";
else
cout << str << " is not a palindrome ";
}
BIPU Lab Manual: Cpp programs – 78
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

/*C++ program to accept three names name 1 = "Mahatma" name 2 = "Gandhi"


name 3 = "Road" and display output as M.G.Road using library functions*/
# include <iostream.h>
# include <string.h>
# include <iomanip.h>
void main( )
{
char name1[15], name2[15], name3[15], name[50] = {'\0'};
cout << "Input first name\n";
cin.getline(name1, 15);
cout << "Input second name\n";
cin.getline(name2, 15);
cout << "Input third name\n";
cin.getline(name3, 15);
strncpy(name, name1, 1);
strcat(name, ".");
strncat(name, name2, 1);
strcat(name, ".");
strcat(name, name3);
cout << name;
}
//C++ program to find the square of a number using user defined function
# include <iostream.h>
double sqr(double x)
{
return (x * x);
}
void main( )
{
double a, asquare;
cout << "Input a number\n";
cin >> a;
asquare = sqr(a);
cout << "Square of " << a << " = " << asquare;
}
//C++ to find square of a number using user defined function method 2
# include <iostream.h>
void main( )
{
double n;
double sqr(double);
cout << " Input a number\n";
cin >> n;
cout << " Square of " << n << " is " << sqr(n);
}
double sqr(double a)
{
return (a * a);
}

BIPU Lab Manual: Cpp programs – 79


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

// C++ program to find the sum of two numbers using functions


# include <iostream.h>
int sum(int, int);
void main( )
{
int a, b;
cout << "Input two numbers\n";
cin >> a >> b;
cout << "Sum of two numbers = " << sum(a, b);
}
int sum(int x, int y)
{
return(x + y);
}
// C++ program to find sum of digits of a number using functions
# include <iostream.h>
int sumofdig(int);
void main( )
{
int num;
cout << "Input a number\n";
cin >> num;
cout << "Sum of digits is " << sumofdig(num);
}
int sumofdig(int n)
{
int d, s = 0;
while(n > 0)
{
d = n % 10;
n = n / 10;
s = s + d;
}
return (s);
}
// C++ program to check whether a number is even or odd using function
# include <iostream.h>
int evenodd(int);
void main( )
{
int n;
cout << "Input a number\n";
cin >> n;
if(evenodd(n) == 1)
cout << "Even number";
else
cout << "Odd number";
}
int evenodd(int x)
{
if(x % 2 == 0) return 1;
else return 0;
}
BIPU Lab Manual: Cpp programs – 80
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

// C++ program to find area of n triangles using functions.


# include <iostream.h>
float areaoftri(float, float);
void main( )
{ int b, h, i, n;
cout << "Enter how many trinagles area to be found\n";
cin >> n;
cout << "Enter " << n << " triangles base and height \n";
for(i = 1; i <= n; ++i)
{
cout << "Enter base and height for " << i << " triangle \n";
cout << "Base = ";
cin >> b;
cout << "Height = ";
cin >> h;
cout << "Area of triangle = " << areaoftri(b, h) << endl;
}
}
float areaoftri(float x, float y)
{
return (0.5 * x * y);
}
/* C++ program to find largest of five numbers using function
# include <iostream.h>
int big(int, int);
void main()
{ int n1, n2, n3, n4, n5, large;
cout << "Input five numbers\n";
cin >> n1 >> n2 >> n3 >> n4 >> n5;
large = big(big(big(n1, n2), big(n3, n4)), n5);
cout << "Largest of five numbers = " << large;
}
int big(int x, int y)
{
return(x > y ? x : y);
}
// C++ program to find ncr = n! / (n - r)! r! using functions
# include <iostream.h>
long factorial(int);
void main( )
{ int n, r, ncr;
cout << " Input n and r values (n >= r) ";
cin >> n >> r;
ncr = factorial(n) / (factorial(n – r) * factorial(r));
cout << " ncr value = " << ncr;
}
long factorial(int m)
{
long fact = 1;
for(int i = 1; i <= m; i++)
fact = fact * i;
return fact;
}
BIPU Lab Manual: Cpp programs – 81
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

/* C++ program to find the square of a number using function find the value of
sum 1^2 + 2^2 + 3^2 + 4^2 +.... n^2 */
# include <iostream.h>
int sqr(int);
void main( )
{
int n, sum = 0, i;
cout << " Input a number\n";
cin >> n;
for(i = 1; i <= n; i++)
sum = sum + sqr(i);
cout << " Sum of series = " << sum;
}
int sqr(int a)
{ return (a * a);
}
/* C++ program to find the average of three numbers using functions */
# include <iostream.h>
float avg(float, float, float);
void main( )
{
float a, b, c, avg1;
cout << " Input three numbers\n";
cin >> a >> b >> c;
avg1 = avg(a, b, c);
cout << " Average of three numbers = " << avg1;
}
float avg(float x, float y, float z)
{
return((x + y + z) / 3);
}
/* C++ program to find simple interest using default arguments */
# include <iostream.h>
float simpint(float p, int t = 1, float r = 10.25);
void main( )
{
float p, r, si;
int t;
cout << " Input principal amount\n";
cin >> p;
cout << " Input time\n";
cin >> t;
if(t == 1)
si = simpint(p);
else
{
cout << " Input rate\n";
cin >> r;
si = simpint(p, t, r);
}
cout << " Simple Interest = " << si;
}

BIPU Lab Manual: Cpp programs – 82


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

float simpint(float x, int y, float z)


{
return(x * y * z) / 100;
}
/* C++ program to find the distance between four pairs of points */
# include <iostream.h>
# include <math.h>
void distance(int x1 = 0, int y1 = 0, int x2 = 0, int y2 = 0);
void main( )
{
distance(2,3,6,3);
distance(2,0,4);
distance(3,4);
distance();
}
void distance(int x1, int y1, int x2, int y2)
{
float d = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
cout << " Distance =" << d << endl;
}
/* C++ program block scope rules */
# include <iostream.h>
void main( )
{
int n = 10;
int i = 0;
cout << "Value of n before while loop = " << n << endl;
while(i <= 2)
{
int n;
n = i * 10;
cout << " Value of n inside the loop = ";
cout << n <<endl;
i++;
}
cout << " value of n after the while loop = " << n << endl;
}
/* C++ program to find the largest of two number using a function by using a
local variables */
# include <iostream.h>
int largest( );
void main( )
{
int lar;
lar = largest();
cout << " Largest of two numbers = " << lar;
}
int largest( )
{
int num1, num2;
cout << " Input num1, num2 \n";
cin >> num1 >> num2;

BIPU Lab Manual: Cpp programs – 83


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

if(num1 > num2)


return num1;
else
return num2;
}
/* C++ program to find largest of two numbers using global variables */
# include <iostream.h>
int largest( );
int num1, num2, lar;
void main( )
{
cout << " Enter two numbers\n";
cin >> num1 >> num2;
cout << " Largest of two numbers="<<largest();
}
int largest( )
{
lar = (num1 > num2) ? num1 : num2;
return(lar);
}
/* C++ program to illustrate scope resolution operator */
# include <iostream.h>
int num = 589;
void main( )
{
int num = 372;
cout << " Value of local num = " << num << endl;
cout << " Value of global variable = " << :: num;
}

/* C++ program to illustrate a function with no arguments and no return


values*/
# include <iostream.h>
void display(void);
void main( )
{
display( );
}
void display( )
{
cout << " H.K gundu rao" << endl;
cout << " HOD department of comp Sci " << endl;
cout << " Germany";
}
/* C++ program to find GCD of two numbers using functions */
# include <iostream.h>
int GCD( );
void main( )
{
int g;
g = GCD( );
cout << " GCD = " << g;
}
BIPU Lab Manual: Cpp programs – 84
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

int GCD( )
{
int x, y;
cout << " Enter the numbers x and y\n";
cin >> x >> y;
while(x != y)
{
if(x > y)
x = x – y;
else
y = y – x;
}
return(x);
}
/* C++ program to find the type of a character entered using a function with
argument no return value*/
# include <iostream.h>
void chartype(char);
void main( )
{
char ch;
cout << " Enter a character\n";
cin >> ch;
chartype(ch);
}
void chartype(char c)
{
if((c >= 'a') && (c <= 'z'))
cout << c << " is lower case character ";
else if((c >= 'A') && (c <= 'Z'))
cout << c << " is upper case chracter ";
else if((c >= '0') && (c <= '9'))
cout << c << " is a number ";
else
cout << c << " is a special chracter ";
}
/* C++ program to reverse a number and check whether it is a palindrome or
not using a function with arguments and return value */
# include <iostream.h>
long reverse(long);
void main( )
{
long num, rev;
cout << " Enter a number\n";
cin >> num;
rev = reverse(num);
cout << "Reverse = " << rev << endl;
if(num == rev)
cout << num << " is a palindrome";
else
cout << num << " is not a palindrome";
}

BIPU Lab Manual: Cpp programs – 85


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

long reverse(long n)
{
int r;
long rev=0;
while(n != 0)
{
r = n % 10;
n = n / 10;
rev = rev * 10 + r;
}
return rev;
}
/* C++ program to check whether a number is palindrome or not using function
with arguments and no return value */
# include <iostream.h>
void reverse(long);
void main( )
{
long num;
cout << " Enter a number\n";
cin >> num;
reverse(num);
}
void reverse(long n)
{
int r;
long rev = 0, temp = n;
while(n != 0)
{
r = n % 10;
n = n / 10;
rev = rev * 10 + r;
}
if(rev == temp)
cout << temp << " is a palindrome ";
else
cout << temp << " is not a palindrome";
}
/* C++ program to find whether a number is palindrome or not using function
without arguments and return value */
# include <iostream.h>
int reverse(void);
void main( )
{
int n;
n = reverse();
if(n == 1)
cout << " is a palindrome";
else
cout << " is not a palindrome";
}

BIPU Lab Manual: Cpp programs – 86


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

int reverse(void)
{
long temp, num, rev = 0, r;
cout << " Input a number\n";
cin >> num;
temp = num;
while(num != 0)
{
r = num % 10;
num = num / 10;
rev = rev * 10 + r;
}
cout << temp;
if (rev == temp)
return 1;
else
return 0;
}
/* C++ program tocheck isprime() that returns 1 if the argument passed to it is
a prime number and 0 otherwise.Using this function generate all prime
numbers between m and n assuming m <= n */
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
int isprime(int);
void main( )
{
int m, n, x;
cout << " Input the range(m <= n):";
cin >> m >> n;
cout << " list of prime numbers between " << m << " and " << n;
for(int i = m; i <= n; i++)
{
x = isprime(i);
if(x == 1)
cout << setw(5) << i;
}
}
int isprime(int num)
{
for(int i = 2; i <= sqrt(num); i++)
if(num % i == 0)
return 0;
return 1;
}
/* C++ program to find the gcd and lcm of two numbers using functions with
arguments and return value */
# include <iostream.h>
int gcd(int, int);
int lcm(int, int);
void main( )
{
int n1, n2;
BIPU Lab Manual: Cpp programs – 87
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

cout << " Input two numbers\n";


cin >> n1 >> n2;
cout << "Gcd of " << n1 << " and " << n2 << " = " << gcd(n1, n2);
cout << "Lcm of " << n1 << " and " << n2 << " = " << lcm(n1, n2);
}
int gcd(int a, int b)
{
while(a != b)
{
if(a > b)
a = a – b;
else
b = b – a;
}
return a;
}
int lcm(int x, int y)
{
return(x * y / gcd(x, y));
}
/* C++ program to find factorial of a number using recursive function */
# include <iostream.h>
long fact(int);
void main( )
{
int n;
cout << " Input a number\n";
cin >> n;
if(n < 1)
cout << " Invalid number\n";
else
cout << " Factorial of " << n << " is " << fact(n);
}
long fact(int a)
{
if(a==0)
return 1;
else
return(a * fact(a – 1));
}
/* C++ program to find sum of natural numbers using recursion*/
# include <iostream.h>
long sum(int);
void main( )
{
int n;
cout << " Input a number\n";
cin >> n;
cout << " sum of " << n << " number " << " is " << sum(n);
}

BIPU Lab Manual: Cpp programs – 88


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

long sum(int n)
{
if(n == 0)
return 0;
else
return(n + sum(n – 1));
}
/* C++ program to find thex to the power of n (x ^ n) using recursive function */
#include<iostream.h>
float power(float,int);
void main( )
{ float x;
int n;
cout<<" Enter x and n values \n";
cin>>x>>n;
cout<< x <<" to the power of "<<n<<" is "<<power(x,n);
}
float power(float a,int b)
{
if(b==0)return 1;
if(b>0)
return (a*power(a,b-1));
else
return((1/a)*power(a,b+1));
}
//C++ program to illustrate the function using call by value mechanism
# include <iostream.h>
# include <iomanip.h>
void testfunc(int);
void main( )
{ int num;
num = 40;
cout << "Value of num before the function call " << num << endl;
testfunc(num);
cout << "Value of num after the function call " << num;
}
void testfunc(int x)
{
x = x + x;
}
/* C++ program to exchange the values of two numbers by passing reference to
the function */
# include <iostream.h>
void swap(int &, int &);
void main( )
{
int a, b;
cout << " Input two numbers\n";
cin >> a >> b;
cout << "Before swapping a = " << a << "and b = " << b << endl;
swap(a, b);
cout << "After swaping a = " << a << " and b = " << b << endl;
}
BIPU Lab Manual: Cpp programs – 89
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

void swap(int &x, int &y)


{
int temp;
temp = x;
x = y;
y = temp;
}
/* C++ program to read, print an array of N numbers and sort them in
ascending order using the technique of selection sort*/
# include <iostream.h>
# include <iomanip.h>
void readarray(int [ ], int);
void disparray(int [ ], int);
void selection(int [ ], int);
void main( )
{ int a[10], n;
cout << "Input number of elements in the array\n";
cin >> n;
cout << "Input array elements " << endl;
readarray(a, n);
cout << "Unsorted array elements are " << endl;
disparray(a, n);
selection(a, n);
cout << "\n Sorted array elements are " << endl;
disparray(a, n);
}
void readarray(int x[ ], int ele)
{
for(int i = 0; i < ele; i++)
cin >> x[i];
}
void disparray(int x[ ], int ele)
{
for(int i = 0; I < ele; i++)
cout << x[i] << setw(6);
}
void selection(int x[ ], int n)
{ int i, j, min, pos;
for(i = 0; I < n – 1; i++)
{
min = x[i];
pos = i;
for(j = i + 1; j < n; j++)
{
if(x[j] < min)
{
min = x[j];
pos = j;
}
}
x[pos] = x[i]; x[i] = min;
}
}
BIPU Lab Manual: Cpp programs – 90
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

/* C++ program to use the following functions:


1. Read Matrix. of a size M x N.
2. Display Matrix. to print a matrix of size M x N.
3. Scalar( ). to check whether a matrix is a scalar or not.
4. Main( ). to read a matrix of size M x N and then check is scalar */
# include <iostream.h>
# include <iomanip.h>
void ReadMatrix(int [ ][10], int, int);
void DisplayMatrix(int [ ][10], int, int);
int scalar(int[ ][10], int, int);
int i, j;
void main( )
{
int a[10][10], m, n;
cout << "Input order of the matrix \n";
cin >> m >> n;
cout << "Input matrix elements " << endl;
ReadMatrix(a, m, n);
cout << " Matrix elements are: " << endl;
DisplayMatrix(a, m, n);
if(m == n)
{
if(scalar(a, m, n) == 1)
cout << " Matrix is scalar ";
else
cout << " Matrix is not a scalar ";
}
else
cout << " Not a square matrix ";
}
void ReadMatrix(int x[10][10], int rows, int columns)
{
for(i = 0; i < rows; i++)
for(j = 0; j < columns; j++)
cin >> x[i][j];
}
void DisplayMatrix(int x[10][10], int rows, int columns)
{
for(i = 0; i < rows; i++)
{
for(j = 0; j < columns; j++)
cout << x[i][j] << " ";
cout << endl;
}
}
int scalar(int x[10][10], int rows, int columns)
{
int scalarelement;
scalarelement = x[0][0];
for(i = 0; i < rows; i++)
{
for(j = 0; j < columns; j++)
{
BIPU Lab Manual: Cpp programs – 91
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

if(i == j)
{
if(x[i][j] != scalarelement)
return 0;
}
else
{
if(x[i][j] != 0)
return 0;
}
}
}
return 1;
}
/* C++ program to find the length of a string */
# include <iostream.h>
int length(char [ ]);
void main( )
{
char str[100];
cout << " Input a string\n";
cin.getline(str, 100);
cout << " Length of string = " << length(str);
}
int length(char s[ ])
{
for(int i = 0; s[i] != '\0'; i++);
return i;
}
/* C++ program to reverse a string */
# include <iostream.h>
int length(char [ ]);
void reverse(char [ ]);
void main( )
{
char str[100];
cout << " Input a string\n";
cin.getline(str, 100);
cout << " Original string is" << str << endl;
reverse(str);
cout << " Reversed string is " << str << endl;
}
int length(char s[ ])
{
for(int i = 0; s[i] != '\0'; i++);
return i;
}
void reverse(char s[ ])
{
int n = length(s);
char temp;

BIPU Lab Manual: Cpp programs – 92


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

for(int i = 0, j = n – 1; i < n / 2; i++, j--)


{
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
/* C++ program to show the disadvantage of normal types of data */
# include <iostream.h>
# include <iomanip.h>
void main( )
{
char itemname[4][10];
float price[4];
int i;
cout << "Enter the name and price of four items " << endl;
for(i = 0; i < 4; i++)
{
cout << " Item " << (i + 1) << ":";
cin >> itemname[i] >> price[i];
}
cout << setprecision(4) << endl;
cout << "Price list is:" << endl;
cout << "Item Name price" << endl;
cout << "********************" << endl;
for(i = 0; i < 4; i++)
cout << setw(10) << itemname[i] << setw(10) << price[i] << endl;
}
/* C++ program to show the use of structures */
# include <iomanip.h>
struct item
{ char name[10];
float price;
} i1, i2, i3, i4;
void main( )
{ cout << " Input name and price of four items " << endl;
cout << " Item 1:";
cin >> i1.name >> i1.price;
cout << " Item 2:";
cin >> i2.name >> i2.price;
cout << " Item 3:";
cin >> i3.name >> i3.price;
cout << " Item 4:";
cin >> i4.name >> i4.price;
cout << setprecision(4) << endl;
cout << " Price List is:" << endl;
cout << " Item Name price" << endl;
cout << "***********************" << endl;
cout << setw(10) << i1.name << setw(10) << i1.price << endl;
cout << setw(10) << i2.name << setw(10) << i2.price << endl;
cout << setw(10) << i3.name << setw(10) << i3.price << endl;
cout << setw(10) << i4.name << setw(10) << i4.price << endl;
}
BIPU Lab Manual: Cpp programs – 93
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

/* C++ program to input user's date of birth and display the output in the date
format using structure */
# include <iostream.h>
void main( )
{
struct date
{
int day;
int month;
int year;
};
struct date dob;
cout << " Input your date ofbirth ";
cin >> dob.day >> dob.month >> dob.year;
cout << "Date of birth:" << dob.day << " - " << dob.month << " - " << dob.year ;
}
C++ program to find the distance between two points. program should define a
structure to hold information about a point in the coordinate system. The
defination should also declare two variables point1 and point2 of the same type
# include <iostream.h>
# include <math.h>
void main( )
{
struct point
{
int x, y;
} point1, point2;
float distance;
cout << " Input co-ordinate values for the first point " << endl;
cin >> point1.x >> point1.y;
cout << " Input co-ordinate values for the second point " << endl;
cin >> point2.x >> point2.y;
distance = sqrt(pow((point2.x - point1.x ),2) + pow((point2.y - point1.y), 2));
cout << " Distance between two points = " << distance << endl;
}
/* C++ program to input and display the information of a student */
#include<iostream.h>
void main( )
{
struct student
{
int regno;
char name[20];
char grade[10];
char comb[5];
float fees;
}nstud;
cout << " Enter student regno " << endl;
cin >> nstud.regno;
cout << " Enter name of the student " << endl;
cin >> nstud.name;
cout << " Enter the class student is studying " << endl;
cin >> nstud.grade;
BIPU Lab Manual: Cpp programs – 94
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

cout << " Enter the combination of the student " << endl;
cin >> nstud.comb;
cout << " Enter the fees " << endl;
cin >> nstud.fees;
cout << endl << " Student Information " << endl;
cout << " Student Register number is " << nstud.regno << endl;
cout << " Name of the student is " << nstud.name << endl;
cout << " Class studying is " << nstud.Class << endl;
cout << " Combination of the student is " << nstud.comb << endl;
cout << " Fees paid is " << nstud.fees << endl;
}
/* C++ program to illustrate the use of nested structures */
#include<iostream.h>
struct date
{
int year;
int month;
int day;
};
struct student
{
int regno;
char name[20];
struct date dob;
}std1;
void main( )
{
cout << "Enter student name ";
cin.getline(std1.name, 20);
cout << " Enter student regno \n";
cin >> std1.regno;
cout << " Enter the date of birth of student " << endl;
cin >> std1.dob.day >> std1.dob.month >> std1.dob.year;
cout << endl << " STUDENT INFORMATION " << endl;
cout << " Reg-no: " << std1.regno << endl;
cout << " Name : " << std1.name << endl;
cout << " Date of birth : ";
cout << std1.dob.day << " - " << std1.dob.month << " - " << std1.dob.year;
}
/* C++ program to accept N student information and to display all the students
information (to create a student database using structures) */
# include <iostream.h>
# include <iomanip.h>
# include <stdio.h>
struct info
{
int regno;
char name[20];
char class1[10];
char comb[5];
float fees;
};

BIPU Lab Manual: Cpp programs – 95


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

void main( )
{
struct info std[10];
int n, i;
cout << " Enter number of students ";
cin >> n;
for(i = 0; i < n; i++)
{
cout << " Enter regno" << endl;
cin >> std[i].regno;
cout << " Enter name ";
gets(std[i].name);
cout << " Enter class ";
gets(std[i].class1);
cout << " Enter combination ";
gets(std[i].comb);
cout << " Enter fees " << endl;
cin >> std[i].fees;
}
cout << endl << " STUDENT INFORMATION " << endl << endl;
for(i = 0; i < n; i++)
{
cout << std[i].regno << '\t';
cout << std[i].name << '\t';
cout << setw(15) << std[i].class1;
cout << setw(15) << std[i].comb;
cout << setw(15) << std[i].fees;
cout << endl;
}
}
/* C++ program to accept 5 people name, address and telephone number and to
search for the information of a particular person */
#include<iostream.h>
#include<string.h>
#include<stdio.h>
struct directory
{ char name[20];
char address[20];
long telephone;
};
void main( )
{
struct directory d[3];
int i, found=1;
char customername[20];
cout << " Enter name, address and telephone number\n";
for(i = 0; i < 3; i++)
{
gets(d[i].name);
gets(d[i].address);
cin>>d[i].telephone;
}

BIPU Lab Manual: Cpp programs – 96


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

cout << " Enter customer name to search " << endl;
gets(customername);
for(i = 0;i < 3; i++)
{
if(strcmp(d[i].name, customername) == 0)
{
cout << " Name = " << d[i].name << endl;
cout << " address = " << d[i].address << endl;
cout << " telephone = " << d[i].telephone << endl;
found = 0;
}
}
if(found)
cout << customername << " Does not have a telephone " << endl;
}
/* C++ program to illustrate the declaration and use of enumerated data type */
# include <iostream.h>
void main( )
{
enum weekdays { sunday = 2, monday, tuesday, wednesday = 7,
thursday, friday, saturday = 16};
cout << "Sunday = " << sunday << endl;
cout << " Monday = " << monday << endl;
cout << " Tuesday = " << tuesday << endl;
cout << " Wednesday = " << wednesday << endl;
cout << " Thursday = " << thursday << endl;
cout << " Friday = " << friday << endl;
cout << " Saturday = " << saturday << endl;
}
/* program to convert days into years, months and days */
#include<iostream.h>
void main( )
{
int nodays, days, years, month;
cout << " Enter the total days\n";
cin >> days;
nodays = days;
years = days / 365;
days = days % 365;
month = days / 30;
days = days % 30;
cout << nodays << " Days = " << days << "days - " << month << "months - "
<< years << " years " << endl;
}
/* program to convert seconds into hours,minutes and seconds */
#include<iostream.h>
void main( )
{
int totsec, seconds, hours, minutes;
cout << " Enter the total seconds\n";
cin >> totsec;
seconds = totsec;
hours = seconds / 3600;
BIPU Lab Manual: Cpp programs – 97
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

seconds = seconds % 3600;


minutes = seconds / 60;
seconds = seconds % 60;
cout << totsec << "seconds = " << hours << "hours - " << minutes
<< "minutes - " << seconds << "seconds ";
}
/* Program to determine shape is square or rectangle */
#include<iostream.h>
void main( )
{
int breadth, height;
cout << " Enter value of breadth and height\n";
cin >> breadth >> height;
if(breadth == height)
cout << "It is square";
else
cout << " It is rectangle";
}
prgram to check whether a given number is prime or not using break statement
#include<iostream.h>
void main( )
{
int n, i, status;
cout << " Enter a number\n";
cin >> n;
status = 1;
for(i = 2; i <= n / 2; i++)
{
if(n % i == 0)
{
status = 0;
cout << " It is not a prime number" << endl;
break;
}
}
if(status)
cout << "It is a prime number";
}
program to check whether a given number is prime or not using exit statement
#include<iostream.h>
#include<stdlib.h>
void main( )
{ int n,i;
cout << " Enter a number\n";
cin >> n;
for(i = 2; i <= n / 2; i++)
{
if(n % i == 0)
{ cout << " It is not a prime number";
exit(0);
}
} cout << " It is a prime number";
}
BIPU Lab Manual: Cpp programs – 98
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

II PUC
Text
book
programs

BIPU Lab Manual: Cpp programs – 99


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Program to perform traversal operation to count the number of odd and


even numbers in a one dimensional array.
# include <iostream.h>
void main ( )
{
int a[100], i, n;
int odd = 0, even = 0;
cout << "Enter number of elements in the array";
cin >> n;
cout << "Enter the array elements " << endl;
for (i = 0; i < n; i++)
cin >> a[i];
for (i = 0; i < n; i++)
if (a[i] % 2 == 0)
even++;
else
odd++;
cout << "Number of Odd elements = " << odd <<endl;
cout << "Number of Even elements = " << even <<endl;
}
Program to insert a number into an array at the given position.
# include <iostream.h>
void main ( )
{
int a[100], element, pos, j, n, i;

cout << " Input number of elements in the array:";


cin >> n;
cout << " Input array elements:" << endl;
for (j = 0; j < n; j++)
cin >> a[j];
cout << " Input element to be inserted :";
cin >> element;
cout << "Input position of the element in the array:";
cin >> pos;
if (pos >n )
cout <<"Invalid position ( Exceeds the range)!!!";
else
{
for (i = n; i > pos ; i--)
a[i] = a[i – 1];
a[pos] =element;
n = n + 1;
cout << " Array elements after insertion :" << endl;
for(j = 0; j < n; j++)
cout << a[j] << "\t";
}
}
Program to delete an element from an array from a given position.
# include <iostream.h>
void main ( )
{
int a[100], i, n, element, pos;
cout << " Enter number of elements" << endl;
cin >> n;
BIPU Lab Manual: Cpp programs – 100
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

cout << " Enter array elements " <<endl;


for (i = 0; i < n; i++)
cin >> a[i];
cout << " Enter the position of the element to be deleted";
cin >> pos;
if ( pos >= n)
cout << " Invalid position !!!";
else
{
element = a[pos];
cout << " Element to be Deleted is " << element;
for (i = pos; i < n; i++)
a[i] = a[i + 1];
n = n - 1;
cout <<" Array after Deletion " <<endl ;
for ( i = 0; i < n; i++)
cout << "\t" << a[i] ;
}
}
Program to perform LINEAR SEARCH operation: Method 1
# include <iostream.h>
void main ( )
{
int a[100], element, loc, n, i;
cout << " Enter size of the array : " << endl ;
cin>> n;
cout << "Enter array elements " << endl;
for ( i = 0 ; i < n ; i++)
cin >> a[i];
cout << " Enter the element to search : " <<endl ;
cin >> element ;
loc = -1;
for ( i=0 ; i<n ; i++)
if ( a[i] == element)
{
loc = i + 1;
break;
}
if( loc == -1)
cout << " Element not present " <<endl;
else
cout << " Element found in position :" << loc;
}
Program to perform LINEAR SEARCH operation: Method 2
#include <iostream.h>
void main ( )
{
int a[100], element, loc, n, i;

cout << " Enter size of the array : "<< endl ;


cin>> n;
cout << "Enter array elements " << endl;
for ( i = 0 ; i < n ; i++)
cin >> a[i];

BIPU Lab Manual: Cpp programs – 101


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

cout << " Enter the element to search : "<<endl ;


cin >> element ;
loc = -1;
for ( i = 0 ; i < n && loc == -1 ; i++)
if (a[i] == element ) loc = i + 1;
if( loc == -1)
cout <<" Element not present " <<endl;
else
cout << " Element found in position :" << loc;
}
Program to check an element exist in the list using BINARY SEARCH
technique. If exist , display the location of the ELEMENT ,
otherwisew display 'NOT FOUND'.
#include <iostream.h>
void main ( )
{
int a[100] , element , loc , n , i , beg , end , mid;
cout << " Enter the size of the array :" << endl ;
cin >> n;
cout <<" Enter array elements in sorted order "<< endl ;
for ( i = 0 ; i < n ; i++)
cin >> a[i];
cout << " Enter element to search : " << endl;
cin >> element;
loc = -1;
beg = 0;
end = n - 1;
while ( beg <= end)
{
mid = ( beg + end ) /2;
if ( element == a[mid] )
{
loc = mid + 1;
break ;
}
if ( element < a[mid] )
end = mid - 1;
else
beg = mid +1;
}
if ( loc == -1 )
cout <<" Element not found " << endl ;
else
cout <<" Element found in position : " << loc;
}
Prog arrange N elements in ascending order using INSERTION technique.
#include <iostream.h>
void main ( )
{
int a[100] , i , j, temp, n ;
cout << " Enter number of elements ";
cin >> n ;
cout << " Enter array elements " << endl ;
for ( i = 0 ; i < n ; i++ )
cin >> a[i];
BIPU Lab Manual: Cpp programs – 102
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

cout << " Unsorted list is " << endl ;


for ( i = 0 ; i < n ; i++)
cout << a[i] << "\t" ;
for ( i = 1 ; i < n ; i++ )
{
j = i;
while ( j >= 1 && a[j] < a[j - 1] )
{
temp = a[j] ;
a[j] = a [ j - 1 ] ;
a[j - 1] = temp ;
j = j - 1;
}
}
cout << " \n Sorted list is " << endl ;
for ( i = 0 ; i < n ; i++ )
cout << a[i] << " \t " ;
}
Program to find the sum of two numbers using a user defined function.
#include <iostream .h>
int sum ( int , int );
void main ( )
{
int a, b ,s ;
cout << " Input two numbers : ";
cin >> a >> b ;
s = sum ( a , b ) ;
cout << " Sum of two numbers = " << s ;
}
int sum ( int x , int y )
{
return x + y ;
}
Program to assign values to the data members of the class Date and
display the result on the screen in the proper format.
#include <iostream .h>
class Date
{
public :
int day, month, year ;
};
void main ( )
{
Date Today ;
Today . day = 12 ;
Today . month = 07 ;
Today . year = 2018 ;
cout << " Today is " ;
cout << Today . day << "/" << Today . month
<< "/" << Today . year ;
}

BIPU Lab Manual: Cpp programs – 103


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Program to accept Date from the user and display date on the screen
using Object-Oriented programming technigue.
#include <iostream.h>
class Date
{
int day , month , year ;
public :
void InputDate ( )
{
cout << " Enter a Date as DD MM YY : " ;
cin >> day >> month >> year ;
}
void DisplayDate ( )
{
cout << " Entered Date is " ;
cout << day << "/" << month << "/" << year ;
}
};
void main ( )
{
Date Today ;
Today . InputDate ( ) ;
Today . DisplayDate ( ) ;
}
Program to find the area and perimeter of a Rectangle using Object -
Oriented programming technique.
#include <iostream.h>
class Rectangle
{
float length , breadth ;
public :
void InputValues ( )
{
cout << " Input the length of the rectangle : " ;
cin >> length ;
cout << " Input the breadth of the rectangle : " ;
cin >> breadth ;
}
int Area ( )
{
return ( length * breadth ) ;
}
int Perimeter ( )
{
return ( 2 * ( length + breadth ) ) ;
}
void DisplayInfo ( )
{
cout << "\n Length of the rectangle is : " << length;
cout << "\n Breadth of the rectangle is : " << breadth;
cout << "\n Area of the rectangle is : " << Area( );
cout << "\n Perimeter of the rectangle is : " << Perimeter( );
}
};

BIPU Lab Manual: Cpp programs – 104


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

void main ( )
{
Rectangle R ;
R . InputValues ( ) ;
R . DisplayInfo ( ) ;
}
Program to read and write N numbers into one dimensional array using
object oriented technique .
#include <iostream .h>
class OneDimArray
{
int a[100] , m ;
public :
void SetNoofElements ( int n )
{
m = n ;
}
void ReadArray ( ) ;
void DisplayArray ( ) ;
};
void OneDimArray :: ReadArray ( )
{
cout << " Enter " << m << " array elements " << endl ;
for ( int i = 0 ; i < m ; i++ )
cin >> a[ i ];
}
void OneDimArray :: DisplayArray ( )
{
cout << " Array elements are : " << endl ;
for ( int i = 0 ; i < m ; i++ )
cout << a[i] << '\t' ;
}
void main ( )
{
int n ;
OneDimArray A ;
cout << " Input number of elements : " ;
cin >> n ;
A . SetNoofElements (n) ;
A . ReadArray ( ) ;
A . DisplayArray ( ) ;
}
Progarm tp display the size of the objects and a class .
#include <iostream.h>
class Student
{
long RegNo ;
char Name [20] ;
char comb [6] ;
int Marks ;
char Address
public :
void ReadStdInfo ( ) ;
void DisplayStdInfo ( ) ;
};
BIPU Lab Manual: Cpp programs – 105
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

void main ( )
{
Student S1, S2 ;
cout << "`Size of the object S1 is = " << sizeof (S1) << endl ;
cout << "`Size of the object S2 is = " << sizeof (S2) << endl ;
cout << "`Size of the class is = " << sizeof (Student) ;
}
Prg to creat a database for a college containing IdNo, Name and Marks
#include <iostream.h>
#include <iomanip.h>
class Student
{
int IdNo ;
char Name [20] ;
int Marks ;
public :
void ReadData ( )
{
cout << " Enter the following information " << endl ;
cout << " IdNo : " ; cin >> IdNo ;
cout << " Name : " ; cin >> Name;
cout << " Marks : " ; cin >> Marks ;
}
void DisplayData ( )
{
cout << setw (5) << IdNo << setw (15) << Name;
cout << setw (7) << Marks << endl ;
}
};
void main ( )
{
Student COLLEGE [100] ;
int N , i ;
cout << " Input number of student : " ;
cin >> N ;
for ( i = 0 ; i < N ; i++ )
COLLEGE [i] . ReadData ( ) ;
cout << " --------------------------------------------" << endl ;
cout << " LIST OF STUDENTS IN ABC COLLEGE " << endl ;
cout << " ---------------------------------------------"<< endl ;
cout << " IdNo " << setw (15) << " Name " << setw (9)
<< " Marks " << endl ;
for ( i=0 ; i < N ; i++ )
COLLEGE [i] . DisplayData ( ) ;
cout << " --------------------------------------------" << endl ;
}

BIPU Lab Manual: Cpp programs – 106


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Program to find the total marks of PUC and CET examinations in each
subject.
#include <iostream.h>
class Exam
{
float Physics , Chemistry , Maths ;
public :
void ReadMarks ( )
{
cout << " Input Physics marks : " ; cin >> Physics ;
cout << " Input Chemistry marks : " ; cin >> Chemistry ;
cout << " Input Maths marks : " ; cin >> Maths ;
}
void DisplayInfo ( )
{
cout << " Physics : " << Physics << endl ;
cout << " Chemistry : " << Chemistry << endl ;
cout << " Maths : " << Maths << endl ;
}
void Total ( Exam PU , Exam CT )
{
Physics = PU . Physics + CT . Physics ;
Chemistry = PU . Chemistry + CT . Chemistry ;
Maths = PU . Maths + CT . Maths ;
}
};
void main ( )
{
Exam PUC , CET , PUCplusCET ;
cout << " Enter PUC marks " << endl << endl ;
PUC . ReadMarks ( ) ; // read PUC marks
cout << endl << " Enter Common Entrance test marks " << endl ;
CET . ReadMarks ( ) ;
PUCplusCET . Total ( PUC , CET ) ;
cout << endl << " Total marks of PUC and CET is : " << endl ;
PUCplusCET . DisplayInfo ( );
}
Program to return objects from functions.
#include <iostream.h>
class Exam
{
float Physics , Chemistry , Maths ;
public :
void ReadMarks ( )
{
cout << " Input Physics marks : " ; cin >> Physics ;
cout << " Input Chemistry marks : " ; cin >> Chemistry ;
cout << " Input Maths marks : " ; cin >> Maths ;
}
void DisplayInfo ( )
{
cout << " Physics : " << Physics << endl ;
cout << " Chemistry : " << Chemistry << endl ;
cout << " Maths : " << Maths << endl ;
}
BIPU Lab Manual: Cpp programs – 107
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Exam Total ( Exam CT )


{
Exam Temp ;
Temp . Physics = Physics + CT . Physics ;
Temp . Chemistry = Chemistry + CT . Chemistry ;
Temp . Maths = Maths + CT . Maths ;
return Temp ;
}
};
void main ( )
{
Exam PUC , CET , PUCplusCET ;
cout << " Enter PUC marks " << endl;
PUC . ReadMarks ( ) ;
cout << endl << " Enter Common Entrance test marks " << endl ;
CET . ReadMarks ( ) ;
PUCplusCET = PUC. Total ( CET ) ;
cout << endl << " Total marks of PUC and CET is : " << endl;
PUCplusCET . DisplayInfo ( );
}
Program to illustrate how to use function overloading for displaying
different parameters , namely integer , float and character types .
# include <iostream.h>
void Display ( ) ;
void Display ( int ) ;
void Display ( float ) ;
void Display ( char ) ;
void main ( )
{ int IntVar ;
float FloatVar ;
char CharVar;
IntVar = 567;
FloatVar = 23.798;
CharVar = ‘A’;
Display( );
Display( IntVar );
Display( FloatVar );
Display( CharVar );
}
void Display ( )
{
cout << " Demostration of FUNCTION OVERLOADING " << endl ;
}
void Display( int i)
{
cout << " Integer data : " << i << endl ;
}
void Display( float f )
{
cout << " Float data : " << f << endl ;
}
void Display ( char c )
{
cout << " Character data :" << c << endl ;
}
BIPU Lab Manual: Cpp programs – 108
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Program to find the area of triangle, circle and rectangle using


function overloading technique .
#include <iostream.h>
#define PI 3.142857
float Area( float ) ;
int Area( int , int ) ;
float Area ( float , float ) ;
void main ( )
{
float Radius ;
cout << " Input radius of the circle : " ;
cin >> Radius ;
int width , breadth ;
cout << " Input rectangle data : " << endl ;
cout << " width : " ; cin >> width ;
cout << " breadth : " ; cin >> breadth ;
float Base , Height ;
cout << " Input triangle data : " << endl ;
cout << " Base : " ; cin >> Base ;
cout << " Height : " ; cin >> Height ;
cout << " Area of the circle : " << Area (Radius ) << endl ;
cout << " Area of the rectangle : " << Area (width , breadth) ;
cout << " \nArea of the triangle : " << Area ( Base , Height ) ;
}
float Area( float r)
{
return PI *r * r ;
}
int Area( int w , int b)
{
return w *b ;
}
float Area ( float b , float h )
{
return 0.5 * b * h ;
}
Progarm to illustrate how function overloading is carried out for
swapping of two variables of the various data types ( integer, float,
and character) .
#include <iostream.h>
void SWAP ( int & , int & ) ;
void SWAP ( float & , float & ) ;
void SWAP ( char & , char & ) ;
void main ( )
{
int x = 45 , y= 67 ;
cout << " Value of x= " << x << " and y = " << y
<< " before the swap " << endl ;
SWAP ( x , y ) ;
cout << " Value of x = " << x << " and y = " << y
<< " after the swap "<< endl ;
float a= 4.56 , b= 3.467 ;
cout << " \nValue of a= " << a << " and b = " << b
<< " before the swap " << endl ;
SWAP ( a , b ) ;
BIPU Lab Manual: Cpp programs – 109
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

cout << " Value of a = " << a << " and b = " << b
<< " after the swap "<< endl ;
char c1 = 'A' , c2 = 'B' ;
cout << " \nValue of c1 = " << c1 << " and c2 = " << c2
<< " before the swap " << endl ;
SWAP ( c1 , c2 ) ;
cout << " Value of c1 = " << c1 << " and c2 = " << c2
<< " after the swap "<< endl ;
}
void SWAP ( int &a , int &b )
{
int temp = a ;
a = b ;
b = temp ;
}
void SWAP ( char &a , char &b )
{
char temp = a ;
a = b ;
b = temp ;
}
void SWAP ( float &a , float &b )
{
float temp = a ;
a = b ;
b = temp ;
}
Program to find the cube of a number using inline function .
#include <iostream.h>
inline int CUBE ( int a)
{
return a * a * a ;
}
void main ( )
{
int n ;
cout << " Input a number : " ;
cin >> n ;
cout << " Cube of " << n << " = " << CUBE ( n) ;
}
Program to check a number is even or odd using a friend function.
#include <iostream.h>
class Number
{
int a ;
public :
void ReadData ( )
{
cout << " Input a number " ;
cin >> a ;
}
friend int IsEven ( Number ) ;
};

BIPU Lab Manual: Cpp programs – 110


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

int IsEven ( Number N )


{
if ( N . a % 2 == 0 )
return 1 ;
else
return 0 ;
}
void main ( )
{
Number Num1 ;
Num1 . ReadData ( ) ;
if ( IsEven ( Num1 ) )
cout << " Number is even " ;
else
cout << " Numbert is odd " ;
}
Program to access private data of a class using a friend function .
# include <iostream.h>
class Account
{
int AccNo ;
char Name [ 20] ;
float Balance ;
public :
void ReadData ( )
{
cout << " Enter account number : " ;
cin >> AccNo ;
cout << " Enter name of the customer : " ;
cin >> Name ;
cout << " Enter account balance : " ;
cin >> Balance ;
}
friend void ShowBalance ( Account ) ;
} ;
void ShowBalance ( Account A )
{
cout << " Balance amount = " << A . Balance ;
}
void main ( )
{
Account CusAcc;
cout << " Input customer details : " << endl ;
CusAcc . ReadData ( ) ;
ShowBalance ( CusAcc ) ;
}
Create a class to hold information of a husband and another for the
wife. Using friend function give the total salary of the family.
#include <iostream.h>
class Wife ; //forward declaration needed for friend function
class Husband
{
char name [20] ;
int salary ;
public :
BIPU Lab Manual: Cpp programs – 111
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

void ReadData ( )
{
cout << " Input husband name " ;
cin >> name ;
cout << " Input salary " ;
cin >> salary ;
}
friend int TotalSalary ( Husband , Wife );
};
class Wife
{
char name [20] ;
int salary ;
public :
void ReadData ( )
{
cout << " Input wife name " ;
cin >> name ;
cout << " Input salary " ;
cin >> salary ;
}
friend int TotalSalary ( Husband , Wife );
};
int TotalSalary ( Husband H , Wife W )
{
return ( H . salary + W. salary) ;
};
void main ( )
{
Husband H ;
Wife W ;
H . ReadData ( ) ;
W . ReadData ( ) ;
cout << "Total salary of the family is " << TotalSalary (H, W);
}
Program to show how to declare a constructor inside class definition.
#include <iostream.h>
class ABC
{
public :
ABC ( ) // Constructor
{
cout << " This is a class constructor " ;
}
};
void main ( )
{
ABC ConObj ; // object creation
}

BIPU Lab Manual: Cpp programs – 112


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Program to demostrate how a constructor is automatically executed at


the time of object creation .
#include <iostream .h>
class Student
{
public :
Student ( )
{
cout << " constructor called automatically " ;
cout << " at the time of object creation " << endl ;
}
};
void main ( )
{
Student S1;
Student S2;
Student S3;
}
Program to demostrate how a constructor is used to initialize data
member of an object .
#include <iostream.h>
class Number
{
int a ;
public :
Number ( ) // constructor definition
{
cout << " I am in the constructor " << endl ;
a= 100 ;
}
void Display ( )
{
cout << " Value of a is = " << a ;
}
};
void main ( )
{
Number N ;
N . Display ( ) ;
}
Prg to find the sum of N natural numbers using a class constructor.
#include <iostream.h>
class Sum
{
int n , S ;
public :
Sum ( )
{
S=0 ;
}
void ReadData ( )
{
cout << " Input`the limit ";
cin >> n ;
}
BIPU Lab Manual: Cpp programs – 113
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

int CalculateAndDisplay ( )
{
for ( int i=1 ; i <= n ; i++ ) S+=i ;
return S ;
}
};
void main ( )
{
Sum SumOfN ;
SumOfN . ReadData ( ) ;
cout << " Sum of natural numbers = "
<< SumOfN . CalculateAndDisplay ( ) ;
}
Prg for default constructors
#include <iostream.h>
class Point
{
int x , y ;
public :
Point ( )
{
x = 0 ;
y = 0 ;
}
void DisplayCoordinates ( )
{
cout << " X - coordinate = " << x << endl ;
cout << " Y - coordinate = " << y << endl ;
}
};
void main ()
{
Point P ;
P . DisplayCoordinates ( ) ;
}
Program to illustrate the passing of arguments to the constructor
using implicit call .
#include <iostream.h>
class Sum
{
int total ;
public :
Sum ( int n )
{
total = 0 ;
for ( int i = 1 ; i <= n ; i++ )
total+=i ;
}
void DisplaySum ( )
{
cout << " Total = " << total ;
}
};

BIPU Lab Manual: Cpp programs – 114


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

void main ( )
{
Sum S ( 100 ) ;
S . DisplaySum ( ) ;
}
Program to find the largest of two numbers using parameterized
constructor using explict call .
#include <iostream.h>
class Max
{
int big ;
public :
Max ( int n , int m )
{
if ( n > m )
big = n ;
else
big = m;
}
void DisplayBig ( )
{
cout << " Big = " << big << endl ;
}
};
void main ( )
{
Max N = Max ( 50 , 78 ) ;
Max M = Max ( -123 , -345 ) ;
N . DisplayBig ( ) ;
M . DisplayBig ( ) ;
}
Program to find the area of a circle calling a parameterized
constructor using operator.
#include <iostream.h>
class Area
{
float A , R ;
public :
Area ( float r )
{
R = r ;
}
void DisplayArea ( )
{
A = 3.142 * R * R ;
cout << " Area = " << A << endl ;
}
};
void main ( )
{
Area N = 5 ;
Area M = 10 ;
N . DisplayArea ( ) ;
M . DisplayArea ( ) ;
}
BIPU Lab Manual: Cpp programs – 115
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Program to illustrate the use of constructor overloading .


#include <iostream.h>
class Number
{ int n ;
public :
Number ( )
{
n=0 ;
}
Number ( int m )
{
n = m ;
}
void DisplayData ( )
{
cout << " Value of n = " << n << endl ;
}
};
void main ( )
{
Number num1;
Number num2 ( 100) ;
num1 . DisplayData ( ) ;
num2 . DisplayData ( ) ;
}
Program to find the area of a square, rectangle and a triangle using
constructor overloading.
#include <iostream.h>
#include <math.h>
class Area
{
public :
Area ( ) ;
Area ( int ) ;
Area ( int , int ) ;
Area ( int , int , int ) ;
};
Area :: Area ( )
{
cout << " Defaulot constructor being called " << endl ;
}
Area :: Area ( int a)
{
cout << "Area of square = " << a*a << endl ;
}
Area ::Area ( int l , int b)
{
cout << " area of rectangle = " << l*b << endl ;
}
Area :: Area ( int a , int b , int c )
{
float s = ( a+b+c)/2.0 ;
float area = sqrt ( s * (s - a ) * ( s - b ) * ( s - c) ) ;
cout << " Area of triangle = " << area << endl ;
}
BIPU Lab Manual: Cpp programs – 116
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

void main ( )
{
Area A1 ;
Area A2 ( 3) ;
Area A3 ( 4 ,5) ;
Area A4 ( 5 , 12 , 13) ;
}
Constructors with default arguments
#include <iostream.h>
class Point
{
int X , Y ;
public :
Point ( ) { }
Point ( int a , int b = 0 )
{
X = a ;
Y = b ;
}
void GetPoint ( )
{
cout << " Enter X - coordinates : " ; cin >> X ;
cout << " Enetr Y - coordinates : " ; cin >> Y ;
}
void DisplayPoint ( )
{
cout << "Point is ( " << X << " , " << Y << " ) " << endl;
}
} ;
void main ( )
{
Point P1 , P2 ( 3) , P3( 5 ,6) ;
cout << " Input coordinates of P1 point " << endl ;
P1 . GetPoint( );
P1 . DisplayPoint ( ) ;
P2 . DisplayPoint ( ) ;
P3 . DisplayPoint ( ) ;
}
Program to illustrate how a copy constructor can be overloaded .
#include <iostream.h>
class Number
{
int a ;
public :
Number ( ) { }
Number ( int N )
{
a = N ;
}
Number ( Number &X )
{
a = X . a;
cout << " Copy constructor is invoked " ;
}

BIPU Lab Manual: Cpp programs – 117


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

void Display ( )
{
cout << a << endl ;
}
};
void main ( )
{
Number N1 ( 345) ;
Number N2 ( N1) ;
N1 . Display ( ) ;
N2 . Display ( ) ;
}
Program to illustrate the use of destructor in C++ .
#include <iostream.h>
class Number
{
int data ;
public :
Number ( )
{
cout << " Constructor invoked " << endl ;
data = 80 ;
}
~ Number ( )
{
cout << " Destructor " << endl ;
}
void DisplayData ( )
{
cout << " Data is = " << data << endl ;
}
};
void main ( )
{
Number N ;
N . DisplayData ( ) ;
Number a ;
}
Program to illustrate public inheritance .
#include<iostream.h>
class Shape
{
public :
int side1 , side2 ;
};
class Rectangle : public Shape
{
public :
int area ;
void ComputeArea ( )
{
area = side1 * side2 ;
}
};

BIPU Lab Manual: Cpp programs – 118


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

void main ( )
{
Rectangle R ;
R . side1 = 5 ;
R . side2 = 6 ;
R . ComputeArea ( ) ;
cout << " Area of rectangle = " << R . area ;
}
Program to illustrate private inheritance .
#include <iostream.h>
class Shape
{
protected :
int side1 , side2 ;
public :
int area ;
void ComputeArea ( )
{
area = side1 * side2 ;
}
};
class Rectangle : private Shape
{
public :
void ReadData ( )
{
cout << " Input first side : " ; cin >> side1 ;
cout << " Input second side : " ; cin >> side2 ;
}
void DisplayOutput ( )
{
ComputeArea ( ) ;
cout << " Area of the Rectangle = " <<area ;
}
};
void main ( )
{
Rectangle R ;
R . ReadData ( ) ;
R . DisplayOutput ( ) ;
}
Program to illustrate protected inheritance with the base class
having protected and public members .
#include <iostream.h>
class Shape
{
protected :
int side1 , side2 ;
public :
int ComputeArea ( )
{
return (side1 * side2) ;
}
};

BIPU Lab Manual: Cpp programs – 119


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

class Rectangle : protected Shape


{
public :
void ReadData ( )
{
cout << " Input first side : " ; cin >> side1 ;
cout << " Input second side : " ; cin >> side2 ;
}
void DisplayOutput ( )
{
cout << " Area of the Rectangle = " << ComputeArea( );
}
};
void main ( )
{
Rectangle R ;
R . ReadData ( ) ;
R . DisplayOutput ( ) ;
}
Single Level Inheritance
#include <iostream.h>
class Date
{
protected :
int dd, mm ,yy ;
public :
void InputDate ( )
{
cout << " Input day : " ; cin >> dd ;
cout << " Input month : " ; cin >> mm ;
cout << " Input year : " ; cin >> yy ;
}
};
class Time : public Date
{
int s, m ,h ;
public :
void InputTime ( )
{
cout << " Input seconds : " ; cin >> s ;
cout << " Input minutes : " ; cin >> m ;
cout << " input hours : " ; cin >> h ;
}
void DisplayDateTime ( )
{
cout << " Date and time : " ;
cout << dd << "/" << mm << "/ " << yy << " / " ;
cout << s << " : " << m << " : " << h ;
}
};
void main ( )
{
Time DTobj ;
cout << " Input valid date : " << endl ;
DTobj . InputDate ( ) ;
BIPU Lab Manual: Cpp programs – 120
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

cout << " Input valid time : " << endl ;


DTobj . InputTime ( ) ;
DTobj . DisplayDateTime ( ) ;
}
Program to illustrate multilevel inheritance .
#include <iostream.h>
class A
{
public :
void DisplayA ( )
{
cout << " Base class A " << endl ;
}
};
class AB : public A
{
public :
void DisplayAB ( )
{
cout << " Intermediate Base class AB " << endl ;
cout << " Derived from A " << endl << endl ;
}
};
class ABC : public AB
{
public :
void DisplayABC ( )
{
cout << " Derived class ABC " << endl ;
cout << " Derived from AB " << endl ;
cout << " Inheritance path A - AB - ABC " << endl ;
}
void Output ( )
{
DisplayA ( ) ;
DisplayAB ( ) ;
DisplayABC( ) ;
}
};
void main ( )
{
ABC objABC ;
objABC . Output ( ) ;
}
Date and Time program
#include <iostream.h>
class Date
{
protected :
int dd , mm , yy ;
public :
void InputInfo ( )
{
cout << " Input day : " ;
cin >> dd ;
BIPU Lab Manual: Cpp programs – 121
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

cout << " Input month : " ;


cin >> mm ;
cout << " Input year : " ;
cin >> yy ;
}
void Display ( )
{
cout << dd << " -" << mm << " -" << yy << " -";
}
};
class Time
{
int s , m , h ;
public :
void InputInfo ( )
{
cout << " Input seconds : " ; cin >> s ;
cout << " Input minutes : " ; cin >> m ;
cout << " Input hours : " ; cin >> h ;
}
void Display ( )
{
cout << s << " : " << m << " : " << h ;
}
};
class DateAndTime : public Date, public Time
{
public :
void InputDateAndTime( )
{
Date :: InputInfo ( ) ;
Time :: InputInfo ( ) ;
}
void Display ( )
{
cout << " Date and Time : " ;
Date :: Display ( ) ;
Time :: Display ( ) ;
}
};
void main ( )
{
DateAndTime DTobj ;
cout << " Input valid date : " << endl ;
DTobj . InputDateAndTime ( ) ;
DTobj . Display ( ) ;
}

BIPU Lab Manual: Cpp programs – 122


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Hierarchical Inheritance
#include <iostream.h>
class Company
{
protected :
char Name [ 30] ;
char Address [ 50] ;
public :
void Input ( )
{
cout << " Enter the name of the company : " ;
cin >> Name ;
cout << " Enter the address of the company : " ;
cin >> Address ;
}
void Output ( )
{
cout << " Company Name : " << Name << endl ;
cout << " Company Address : " << Address << endl ;
}
};
class Item : public Company
{
char ItemName[ 30] ;
float Price ;
public :
void Input ( )
{
Company :: Input ( ) ;
cout << " Enter the name of the item : " ;
cin >> ItemName ;
cout << " Enter the cost of the item : " ;
cin >> Price ;
}
void Output ( )
{
Company :: Output ( ) ;
cout<< " Item Name : " << ItemName << endl ;
cout<< " Item Cost : " << Price << endl ;
}
};
class Electronic : public Company
{
char ItemName[ 30] , Capacity [ 20] ;
float Price ;
public : void Input ( )
{
Company :: Input ( ) ;
cout << " Enter the name of the item : " ;
cin >> ItemName ;
cout << " Enter the capacity of the system : " ;
cin >> Capacity ;
cout << " Enter the cost of the item : " ;
cin >> Price ;
}
BIPU Lab Manual: Cpp programs – 123
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

void Output ( )
{
Company :: Output ( ) ;
cout<< " Item Name : " << ItemName << endl ;
cout<< " Output capacity : "<<Capacity << endl ;
cout<< " Item Cost : " << Price << endl ;
}
};
void main ( )
{
Item I ;
Electronic E ;
cout << " Input Item information " << endl ;
I . Input ( ) ;
cout << " Input Electronic component information " << endl ;
E . Input ( ) ;
cout << endl << " ITEM INFORMATION " << endl << endl ;
I . Output ( ) ;
cout << endl << "ELECTRONIC COMPONENT INFORMATION " << endl ;
E . Output ( ) ;
}
Program to illustrate the technique of hierarchial inheritance .
#include <iostream.h>
class Employee
{
protected :
char Name [30 ] ;
char Address [50 ] ;
public :
void Input ( )
{
cout << "Enter the name of the employee : " ;
cin >> Name ;
cout << " Enter the address of the employee : " ;
cin >> Address ;
}
void Output ( )
{
cout << " Employee Name : " << Name << endl ;
cout << " Employee Address : " << Address << endl ;
}
};
class WorkDetails : public Employee
{
protected :
char Designation [ 30] ;
float Salary ;
public : void Input ( )
{
Employee :: Input ( );
cout << "Enter the designation of the employee:";
cin >> Designation ;
cout << " Enter the salary of the employee : " ;
cin >> Salary ;
}
BIPU Lab Manual: Cpp programs – 124
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

void Output ( )
{
Employee :: Output ( ) ;
cout << " Employee designation : " << Designation << endl ;
cout << " Employee salary : " << Salary << endl ;
}
};
class Sports
{
protected :
char GameName [ 20] ;
float Incentive ;
public :
void Input ( )
{
cout << "Enter name of the game : "; cin >> GameName;
cout << " Enter incentive amount: "; cin >> Incentive;
}
void Output ( )
{
cout << "Name of the game : " << GameName << endl ;
cout << " Incentive : " << Incentive << endl ;
}
};
class EmployeeDetails : public WorkDetails , public Sports
{
float TotalSalary ;
public :
void Input ( )
{
WorkDetails :: Input ( ) ;
Sports :: Input ( ) ;
}
void ComputeNetSalary ( )
{
TotalSalary = Salary + Incentive ;
}
void Output ( )
{
WorkDetails ::Output ( ) ;
Sports :: Output ( ) ;
ComputeNetSalary ( ) ;
cout << " Total Salary of the employee : "
<< TotalSalary ;
}
};
void main ( )
{
EmployeeDetails E ;
cout << " Input employee information " << endl ;
E . Input ( ) ;
cout << endl << " Employee details " << endl << endl ;
E . Output ( ) ;
}

BIPU Lab Manual: Cpp programs – 125


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Constructors and destructors in derived class.


# include <iostream.h>
class Base
{
public:
Base ( )
{
cout << "Inside Base constructor " << endl;
}
~Base( )
{
cout << "Inside Base destructor " << endl;
}
};
class Derived : public Base
{
public:
Derived ( )
{
cout << "Inside Derived constructor " << endl;
}
~Derived( )
{
cout << "Inside Derived destructor " << endl;
}
};
void main( )
{
Derived D;
}
Program to display the value and address of a variable .
#include <iostream.h>
void main ( )
{
int x ;
x = 68 ;

cout << " Value of x is : " << x << endl ;


cout << " Address of x is : " << &x << endl ;
}

#include <iostream.h>
void main ( )
{
int num ;
int *pInt ;

num = 468 ;
pInt = &num ;

cout << " Value of num is : " << num << endl ;
cout << " Address of num is : " << pInt << endl ;
}

BIPU Lab Manual: Cpp programs – 126


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Program to illustrate the dereferencing a pointer variable .


#include <iostream.h>
void main( )
{
int num ;
int *pInt ;

num = 468 ;
pInt = &num ;

cout << " Value of num is : " << *pInt << endl ;
cout << " Address of num is : " << pInt << endl ;
}
Program to illustrate pointer initialization .
#include <iostream.h>
void main ( )
{
int *pInt ;
int num ;

num = 45 ;
pInt = &num ;

cout << " Value of num is : " << num << endl ;
cout << " Address of num is : " << &num << endl ;
cout << " The contents of ptr is " << pInt << endl ;
cout << " Value of num is " << *( &num) << endl ;
cout << " Value pointed by ptr is " << *pInt << endl ;
cout << " The address of pointer is " << &pInt << endl ;
}
#include <iostream.h>
void main ( )
{
int iVar , *pInt ;
float fVar , *pFloat ;
char cVar , *pChar ;

iVar = 123 ;
fVar = 12.34 ;
cVar = 'A' ;

pInt = &iVar ;
pFloat = &fVar ;
pChar = &cVar ;

cout << " Integer number " << *pInt


<< " is stored at the address " << pInt << endl ;
cout << " Float number " << *pFloat
<< " is stored at the address " << pFloat << endl ;
cout << " Character value " << *pChar
<< " is stored at the address " << pChar << endl ;
}

BIPU Lab Manual: Cpp programs – 127


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Program to illustrate the pointer expression and pointer arithmetic.


#include <iostream.h>
void main ( )
{
int a, b, x, y;
int *ptr1, *ptr2 ;
a = 30 ;
b = 6 ;
ptr1 = &a ;
ptr2 = &b ;
x = *ptr1 = *ptr2 - 6 ;
y = 6 - *ptr1 / *ptr2 + 30 ;

cout << "Address of a = " << ptr1 << endl ;


cout << "Address of b = " << ptr2 << endl ;
cout << " a = " << a << " b = " << b << endl ;
cout << " x = " << x << " y = " << y << endl ;

*ptr1 = *ptr1 + 70 ;
*ptr2 = *ptr2 * 2 ;
cout << " a = " << a << " b = " << b << endl ;
}
Program to print an array of elements .
#include <iostream.h>
void main ( )
{
int A[5] = {15, 25, 67, 83, 12 };
for ( int i = 0 ; i < 5 ; i++ )
cout << A[i] << " \t " ;
}
Program to print an array array elements using constant pointer .
#include <iostream.h>
void main ( )
{
int A[ 5] = { 15 , 25 , 67 , 83 , 12 } ;
cout << * ( A ) << " \t " ;
cout << * ( A +1) << " \t " ;
cout << * ( A +2) << " \t " ;
cout << * ( A +3) << " \t " ;
cout << * ( A +4) << " \t " ;
}
Program to read and write array elements using a pointer variable .
#include <iostream.h>
void main ( )
{ int A [10] , n ;
int *ptr ;
cout << " Input number of elements in the array : " ;
cin >> n ;
cout << " Input Array elements : " << endl ;
for ( ptr = A ; ptr < A+n ; ptr++ )
cin >> *ptr ;
cout << " Array elements are : " << endl ;
for ( ptr =A ; ptr<A+n ; ptr ++)
cout << *ptr << " \t " ;
}
BIPU Lab Manual: Cpp programs – 128
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Program to illustrate the array of pointers of isolated variables .


#include <iostream.h>
void main ( )
{
int *pInt[5] ;
int var1 = 20 , var2 = 34 , var3 = -3 , var4 = 8 , var5 = 100 ;
pInt[0] = &var1 ;
pInt[1] = &var2 ;
pInt[2] = &var3 ;
pInt[3] = &var4 ;
pInt[4] = &var5 ;
for ( int i = 0 ; i <= 4 ; i++)
cout << " Value " << *pInt [i] << " stored at "
<< pInt [ i] << endl ;
}
Prg to illustrate the difference between strings as arrays and
pointers.
#include <iostream.h>
void main ( )
{
char str1 [ ] = "PROGRAM " ;
char *str2 = "PROGRAM " ;
cout << str1 << endl ;
cout << str2 << endl ;
str2 ++ ;
cout << str2 << endl ;
}
Program to display and find the length of the string .
#include <iostream.h>
void main ( )
{
char str[20] , *pStr ;
cout << " Input a string : " ;
cin . getline ( str , 20) ;
cout << " Display string using an array variable " << endl ;
cout << " String is : " << str << endl ;
pStr = str ;
cout << " Display string using string pointer " << endl ;
cout << " String is : " ;
while ( *pStr != '\0')
{
cout << *pStr ;
pStr ++ ;
}
cout << endl << "Length of the string is : " << (pStr – str) ;
}
Program to illustrate the function using call by reference mechanism.
#include <iostream.h>
void DataDouble ( int &);
void main ( )
{
int number = 4 ;
cout << " Value of number before function call " << number;

BIPU Lab Manual: Cpp programs – 129


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

DataDouble ( number );
cout << " New value of number is " << number ;
}
void DataDouble ( int &num )
{
num = num + num ;
return ;
}
Program to exchange the values of two numbers by passing reference to
the function .
#include <iostream.h>
void SWAP ( int &, int &) ;
void main ( )
{
int a , b ;
cout << " Input values for a and b : " ;
cin >> a >> b ;
cout << " Before swapping " << endl ;
cout << " a = " << a << " and b = " << b << endl ;

SWAP ( a , b ) ;

cout << " After swapping " << endl ;


cout << " a = " << a << " and b = " << b << endl ;
}
void SWAP ( int &x, int &y )
{
int temp ;
temp = x ;
x = y ;
y = temp ;
return ;
}
Program to illustrate the function using call by address mechanism.
#include <iostream.h>
void DataDouble ( int *);
void main ( )
{
int number = 4 ;
cout << " Value of number before function call " << number ;
DataDouble ( &number ) ;
cout << " New value of number is " << number ;
}
void DataDouble ( int *num )
{
*num = *num + *num ;
return ;
}

BIPU Lab Manual: Cpp programs – 130


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Program to exchange the values of two numbers by passing addresses of


two variables of the function.
#include <iostream.h>
void SWAP( int *, int * ) ;
void main ( )
{
int a , b ;
cout << " Input values for a and b : " ;
cin >> a >> b ;
cout << " Before swapping " << endl ;
cout << " a = " << a << " and b = " << b << endl ;

SWAP ( &a, &b) ;

cout << " After swapping " << endl ;


cout << " a = " << a << " and b = " << b << endl ;
}
void SWAP ( int *x , int *y )
{
int temp ;
temp = *x ;
*x = *y ;
*y = temp ;
return ;
}
Program to access members of a structure using a pointer .
#include <iostream.h>
void main ( )
{
struct Date
{
int day ;
int month ;
int year ;
};
struct Date DOB ;
struct Date *ptr ;
ptr = & DOB ;
cout << " Input a valid date " << endl ;
cout << " Input day : " ; cin >> ptr ->day ;
cout << " Input month : " ; cin >> ptr -> month ;
cout << " Input year : " ; cin >> ptr -> year ;
cout << " Date is : " << ptr->day << " - "
<< ptr -> month << " - "<< ptr -> year ;
}
Program to access members of an object using a pointer .
#include <iostream.h>
class Student
{
int RegNo ;
char Name[20];
int Marks ;

BIPU Lab Manual: Cpp programs – 131


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

public :
void ReadData ( )
{
cout << "input register number : " ; cin >> RegNo ;
cout << "input name of the student :" ; cin >> Name ;
cout << " input marks : " ; cin >> Marks ;
}
void Display( )
{
cout << "Register number : " << RegNo << endl;
cout << "Name of the student : " << Name << endl;
cout << "Marks : " << Marks;
}
};
void main ( )
{
Student *pStdObj ;
cout << " Enter the following information " << endl ;
pStdObj -> ReadData ( ) ;
pStdObj -> Display ( ) ;
}
Program to access members of a class using ‘this’ pointer .
#include <iostream.h>
class Time
{
int hh , mm , ss ;
public :
void InputTime ( )
{
cout << " Input hours : " ; cin >> hh ;
cout << " Input minutes : " ; cin >> this -> mm ;
cout << " Input seconds : " ; cin >> this -> ss ;
}
void DisplayTime ( )
{
cout << "Time : " ;
cout << hh << " : " ;
cout << " : " << ( *this) . mm ;
cout << " : " << this -> ss ;
}
void DisplayAddress ( )
{
cout << " Address of the object = " << this ;
}
};
void main ( )
{
Time T ;
cout << " Input time : " << endl ;
T . InputTime ( ) ;
T . DisplayTime ( ) ;
cout << endl << " Address of the object = " << &T << endl ;
T . DisplayAddress ( ) ;
}

BIPU Lab Manual: Cpp programs – 132


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Program to create a one dimensional array dynamically .


#include <iostream.h>
void main ( )
{
int size ;

cout << " input size of the integer array " ;


cin >> size ;

int *pnArray = new int [ size ] ;

cout << " Enter array elements : " << endl ;


for ( int i = 0 ; i < size ; i++)
cin >> pnArray [i] ;

cout << "Array elements are : " << endl ;


for ( i = 0 ; i < size ; i++)
cout << pnArray [ i ] << " \t " ;

delete [ ] pnArray ;
}
C++ file handling program.
# include <fstream.h>
void main ( )
{
ofstream myfile ;
myfile . open ( " example.txt ") ;
myfile << " Second Year PUC \n " ;
myfile . close ( ) ;
}
Program to store three names into a file called " NAMELIST.DAT " .
# include <fstream.h>
void main ( )
{
ofstream names ;
names . open ( "NAMELIST.DAT ") ;
names << " S . I . SANIYA \n " ;
names << " H . R . MANIK \n " ;
names << " Y . A . MALHOTRA \N " ;
names . close ( ) ;
}
Program to create a text file. (Note: Press ctrl + z to stop
inputting of characters)
# include <fstream.h>
void main ( )
{
char ch ;
ofstream TextFile ( "Text.Dat") ;
while (cin)
{
cin.get(ch ) ;
TextFile.put(ch) ;
}
TextFile.close ( ) ;
}
BIPU Lab Manual: Cpp programs – 133
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

Program to read a text file character by character, and display it


simultaneously on the screen.
# include <fstream.h>
void main ( )
{
char ch ;
ifstream TextFile ( "Text.Dat") ;
cout << "Content of the disk file ( Text.Dat ) " << " \n \n " ;
while (TextFile)
{
TextFile.get(ch) ;
cout << ch ;
}
TextFile.close ( ) ;
}
Program to read a text file line by line using getline( ) fuction,
and display it simultaneously on the screen ..
#include <fstream.h>
void main ( )
{
char StrLine[80 ] ;
ifstream TextFile ( "Text.Dat") ;
cout << "Content of the disk file ( Text . Dat) " << "\n \n " ;
while ( !TextFile.eof( ) )
{
TextFile.getline(StrLine, 80 ) ;
cout << StrLine ;
}
TextFile.close ( ) ;
}
Program to write and read the details of students from the disk file
and then display it on the screen .
# include <fstream.h>
void main ( )
{
int RegNo ;
char Name[20], ans = 'Y' ;
ofstream StdOut ( "STD.DAT" ) ;
while(1)
{
cout << " Input Register number " ;
cin >> RegNo ;
StdOut << RegNo << "\n" ;

cout << " Input name of the student " ;


cin >> Name ;
StdOut << Name << "\n" ;

cout << " Next record ( y / n ) ? " ;


cin >> ans ;
if ( ans == 'n' ) break ;
}
StdOut.close ( ) ;

BIPU Lab Manual: Cpp programs – 134


// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

ifstream StdIn ( "STD.DAT" );


cout << " Register No . " << '\t' << "Nname " << "\n\n" ;
StdIn >> RegNo ;
StdIn >> Name ;
while ( !StdIn.eof( ) )
{
cout << RegNo << '\t' << Name << "\n" ;
StdIn >> RegNo ;
StdIn >> Name ;
}
StdIn.close ( ) ;
}
Program to read characters from a file and convert all lower case
alphabets to upper case and vice versa. (Note: Press ctrl + z to stop
inputting of characters)
# include <fstream.h>
# include <ctype.h>
void main ( )
{
char Filename[20] , ch ;
cout << "Enter the name of the file" ;
cin >> Filename ;
fstream TextFile ;
TextFile.open ( Filename, ios::out ) ;
cout << "Enter the content for " << Filename << "\n \n";
while (cin)
{
cin.get ( ch ) ;
TextFile.put ( ch ) ;
}
TextFile.close ( ) ;

TextFile.open ( Filename, ios::in ) ;


cout << " \n\n " << "File content " << "\n" ;
while (TextFile)
{
TextFile.get ( ch) ;
if (islower(ch))
ch = toupper(ch) ;
else if (isupper(ch))
ch = tolower(ch) ;
cout << ch ;
}
TextFile.close( ) ;
}
Define a structure STUDENT containing three fields – regno, name and
marks. store student sturctures in a data file (student.dat) and
prints on the screen using binary format.
#include <fstream.h>
void main ( )
{
struct STUDENT
{ int RegNo ;
char Name[20] ;
int Marks ; };
BIPU Lab Manual: Cpp programs – 135
// Use clrscr( ) and getch( ) functions along with #include<conio.h> file wherever necessary.

struct STUDENT STD ;


char ans = 'Y' ;
fstream StdFile ;
StdFile.open ("STUDENT.DAT", ios::out, ios::binary) ;
while(ans == 'y')
{
cout << "Enter Register number " ; cin >> STD.RegNo ;
cout << "Enter Student name " ; cin >> STD.Name ;
cout << " Enter Marks " ; cin >> STD. Marks ;
StdFile.write( (char *) &STD, sizeof(STD) ) ;
cout << " Do you want next record ( Y / N) ? " ;
cin >> ans ;
}
StdFile.close( ) ;
StdFile.open ( "STUDENT.DAT", ios::in, ios::binary ) ;
cout << "STUDENT LIST \n \n ";
while(1)
{
StdFile.read ( (char *) &STD, sizeof(STD) ) ;
if ( StdFile.eof( ) )
break ;
cout << STD.RegNo << " " << STD.Name << " " << STD.Marks
<< "\n";
}
}
Prg to find the occurance and positions of a character in the file.
#include <fstream.h>
void main ( )
{
char FileName[20] ,FileCh ,ch ;
int count = 0 ;
cout << " Enter the file name " ;
cin >> FileName ;
cout << " Input a character to count and its position " ;
cin >> ch ;
fstream TextFile ;
TextFile.open ( FileName, ios::in ,ios::out ) ;
while ( TextFile )
{
TextFile.get (FileCh) ;
if (FileCh == ch)
{
count++ ;
cout << ch << " occur " << count << " time at position "
<< TextFile.tellg( ) << " \n" ;
}
}
if (count == 0)
cout << ch << " does not exist in the file " ;
TextFile . close ( ) ;
}

BIPU Lab Manual: Cpp programs – 136

You might also like