0% found this document useful (0 votes)
7 views35 pages

BCA202 - LAB Manual PDF

The BCA-202 Lab Manual provides a comprehensive guide for students to learn C++ programming, covering topics such as opening and creating C++ programs, compiling and executing them, and understanding basic programming concepts like data types, operators, conditional statements, loops, and arrays. It includes numerous example programs and exercises for practical application of the concepts taught. Each lab section has specific objectives and exercises to reinforce learning.
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)
7 views35 pages

BCA202 - LAB Manual PDF

The BCA-202 Lab Manual provides a comprehensive guide for students to learn C++ programming, covering topics such as opening and creating C++ programs, compiling and executing them, and understanding basic programming concepts like data types, operators, conditional statements, loops, and arrays. It includes numerous example programs and exercises for practical application of the concepts taught. Each lab section has specific objectives and exercises to reinforce learning.
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/ 35

BCA-202

LAB MANUAL

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 1


C++ PROGRAMING

 To Open C++ program?

Start  programs  Microsoft Visual Studio 6.0  Microsoft Visual C++ 6.0

 To Make C++ program?

File  New  win 2 console application  write project name  OK  Finish  OK

OR

File  New c++ source File  write File name  OK

 To Compile the C++ program?

1. Bulid  Compile.cpp
2. select compile icon  from the menu bar or
3. press CTRL + F7

 ToExecute the C++ program?

1-Bulid  Execute .exe

2- select Execute icon  from the menu bar

3-press CTRL + F5

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 2


 Term used while writing Program

{ opening curly bracket

} closing curly bracket

# hash sign or number sign (shift+3)

() small brackets

“aaaa ” double quotes

‘ aaaa ‘ single quotes

, comma

; semicolon

void main( ) main function

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 3


LAB: 1

Objectives:

To understand the concept of input ,output statements and data types.

//PROGRAM- 1:Program to demonstrate cout... COMMENTS

#include<iostream.h> //PREPROCESSOR

void main( ) // FUNCTION WITH RETURN TYPE VOID

{ // is comment

cout<<"WELCOME TO C++"<<endl;

// cout IS AN OBJECT IN C++, endl IS END OF LINE,

//; IS CALLED TERMINATOR.

//PROGRAM-2:Program to demonstrate cin cout (int, float,char)...

#include<iostream.h>

void main( )

int a;

float b;

char c;

cout<<"ENTER AN INTEGER"<<endl;

cin>>a;

cout<<"ENTER A FLOAT"<<endl;

cin>>b;

cout<<"ENTER A CHARECTER"<<endl;

cin>>c;

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 4


cout<<"INTEGER= "<<a<<"\tFLOAT= "<<b<<"\tCHARECTER= "<<c<<endl;

//PROGRAM-3:Program to find Sum and Avarage of two numbers.

#include<iostream.h>

void main( )

float a,b;

float sum,avg;

cout<<"ENTER TWO NUMBERS"<<endl;

cin>>a>>b;

sum = a + b;

avg = sum/2;

cout<<"SUM = "<<sum<<"\n AVARAGE = "<<avg<<endl;

//PROGRAM-4:Program to find Sum and Avarage of three numbers.

#include<iostream.h>

void main( )

folat a,b,c;

float sum,avg;

cout<<"ENTER THREE NUMBERS"<<endl;

cin>>a>>b>>C;

sum = a + b + c;

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 5


avg = sum/3;

cout<<"SUM = "<<sum<<"\n AVARAGE = "<<avg<<endl;

Exercises:

1. Write a C++ program to find the difference between two numbers.

2. Write a C++ program to find the product of three numbers.

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 6


LAB: 2

Objectives:

To implement the concept of operators

// PROGRAM-5: Program to find area of a circle.

#include<iostream.h>

void main( )

folat r,area;

const float pi=3.147;

cout<<"ENTER RADIUS OF CIRCLE"<<endl;

cin>>r;

area = pi*r*r;

cout<<"AREA OF CIRCLE = "<<area<<endl;

//PROGRAM-6:Program to find area of a rectangle.

#include<iostream.h>

void main( )

int len, wid, area;

cout<<"ENTER LENGTH OF RECTANGLE"<<endl;

cin>>len;

cout<<"ENTER WIDTH OF RECTANGLE"<<endl;

cin>>wid;

area = len*wid;

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 7


cout<<"AREA OF RECTANGLE = "<<area<<endl;

//PROGRAM-7:Program to find area of a square.

#include<iostream.h>

void main( )

int side, area;

cout<<"ENTER SIDE OF SQUARE"<<endl;

cin>>len;

area = side*side;

cout<<"AREA OF SQUARE = "<<area<<endl;

//Program 8 to display Pay slip of an employee

#include<iostream.h>

void main ()

double GSal,NSal,ded,basic,da;

const double Housing=1000.00, TA=500.00;

cout<<"Enter Basic Salary\n";

cin>>basic;

cout<<"Enter Deduction Amount\n";

cin>>ded;

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 8


da=basic*0.2;

GSal=basic+da+Housing+TA;

NSal=GSal-ded;

cout<<"\t\t\t\tBasic :\t"<<basic<<endl;

cout<<"\t\t\t\tDA :\t"<<da<<endl;

cout<<"\t\t\t\tHousing :\t"<<Housing<<endl;

cout<<"\t\t\t\tTravelling :\t"<<TA<<endl;

cout<<"\t\t\t\tGross salary :\t"<<GSal<<endl;

cout<<"\t\t\t\tDeduction :\t"<<ded<<endl;

cout<<"\t\t\t\tNet Salary :\t"<<NSal<<endl<<endl<<endl;

Exercises:
1. Write a C++ program to find the volume of Cylinder.

2. Write a C++ program to find the volume of sphere.

3. Write a C++ program to create an invoice of item.

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 9


LAB: 3

Objectives:

To understand the concepts of conditional statements.( if – else )

//Program 9 to find Number is MAX or MIN

#include<iostream.h>

void main()

int a,b;

cout<<"Enter two numbers\n";

cin>>a>>b;

if(a>b)

cout<<a<<" is MAXIMUM "<<b<<" is MINIMUM\n";

else

cout<<b<<" is MAXIMUM "<<a<<" is MINIMUM\n";

//Program No 10 to find number is ODD OR EVEN

#include<iostream.h>

void main()

int num;

cout<<"Enter a number\n";

cin>>num;

if(num%2==0)

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 10


cout<<num<<" is an Even Number\n";

else

cout<<num<<" is an Odd Number\n";

//Program 11 To find the Grade of student

#include<iostream.h>
void main()
{
int mark;
char grade;
cout<<"Enter mark";
cin >> mark;
if(mark >= 90 && mark <= 100 )
grade='A';
if(mark >= 80 && mark <= 89 )
grade='B';
if(mark >= 70 && mark <= 79 )
grade='C';
if(mark >= 60 && mark <= 69 )
grade='D';
if(mark < 60 )
grade='F';
cout<<"Mark"<<"\t"<<"Grade"<<endl;
cout<<mark<<"\t"<<grade<<endl;

Exercises:
1. Write a C++ program to find the largest among three numbers.

2. Write a C++ program to find whether the given number is Armstrong number or not.

3. Write a C++ program to find whether the given number is palindrome or not.

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 11


LAB: 4

Objectives:

To implement the concept switch case statements

//Program 12 to check the day of week by using SWITCH-CASE

#include<iostream.h>

void main()

int x;

cout<<"Enter number"<<endl;

cin>>x;

switch(x)

case 1:

cout<<"Saturday"<<endl;

break;

case 2:

cout<<"Sunday"<<endl;

break;

case 3:

cout<<"Monday"<<endl;

break;

case 4:

cout<<"Tuesday"<<endl;

break;

case 5:

cout<<"Wednesday"<<endl;

break;

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 12


case 6:

cout<<"Thursday"<<endl;

break;

case 7:

cout<<"Friday"<<endl;

break;

default:

cout<<"Error"<<endl;

// Program 13 to find month by using a switch case statement

#include<iostream.h>

void main()

int m;

cout<<"Enter Months Number :"<<endl;

cin>>m;

switch(m)

case 1:cout<<"JAN";break;

case 2:cout<<"FEB";break;

case 3:cout<<"MAR";break;

case 4:cout<<"APR";break;

case 5:cout<<"MAY";break;

case 6:cout<<"JUN";break;

case 7:cout<<"JUL";break;

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 13


case 8:cout<<"AUG";break;

case 9:cout<<"SEP";break;

case 10:cout<<"OCT";break;

case 11:cout<<"NOV";break;

case 12:cout<<"DEC";break;

default:cout<<"ERROR";

// Program 14 Menu driven program ( + , * , - , %) using switch case statement

#include<iostream.h>
void main()
{
int n1, n2;
char op;
cout<<"Enter first number"; cin >> n1;
cout<<"Enter second number"; cin >> n2;
cout<<"Enter operator(*, /, + -)"; cin >> op;
switch (op)
{
case '*':
cout<<n1<<op<<n2<<"="<<(n1*n2)<<endl;
break;
case '/':
cout<<n1<<op<<n2<<"="<<(n1/n2)<<endl;
break;
case '+':
cout<<n1<<op<<n2<<"="<<(n1+n2)<<endl;
break;
case '-':
cout<<n1<<op<<n2<<"="<<(n1-n2)<<endl;
break;
default:
cout<<"Invalid oprator"<<endl;
break;
} }
Exercises: Write a menu driven program to calculate different currency conversions.

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 14


LAB: 5

Objectives:

To implement the concept of loops .

// Program 15 to print natural numbers from 1 to 30 using for loop

#include <iostream.h>

void main()

int i;

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

cout<<" "<<"i=";

cout<<i<<endl;

// Program 16 to print natural numbers from 1 to 30 using WHILE loop

#include<iostream.h>

void main()

int a=1;

while(a<=30)

cout<<a<<endl;

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 15


a++;

// Program 17 to print natural numbers from 1 to 30 using DO WHILE loop

#include<iostream.h>

void main()

int a=1;

do

cout<<a<<endl;

a++;

while(a<=30);

//Program 18 to display first 10 Odd numbers using for loop


#include <iostream.h>
void main()
{
int i;
for (i=1;i<=20;i=i+2)
cout<<" i="<<i<<endl;
}

//Program 19 to display first 10 even numbers using for loop


#include <iostream.h>
void main()
{
int i;
for (i=2;i<=20;i=i+2)
cout<<" i="<<i<<endl;

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 16


}

//Program 20 to find the Factorial of Number using do while loop

// Program to find the factorial of a number

#include<iostream.h>

void main()

int n,i=1,fact=1;

cout<<" enter the number ";

cin>>n;

if(n>=0)

do

fact=fact*i;

i++;

while(i<=n);

cout<<"fact="<<fact<<"\n";

}}

Exercises:

1. Write a C++ program to generate Fibonacci numbers up to 100 using for loop.

2. Write a C++ program to generate multiplication table of any number using while loop.

3. Write a C++ program to print all prime numbers between two limits by using do while
loop.

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 17


LAB: 6 ( Revision of loop and nested loop )

Objectives:

To understand the concepts of nested loops

// Program 21 Program to display stars in a triangle shape

#include <iostream.h>

void main()

int i,j,k,n,m;

cout<<"Enter a number";

cin>>n;

m=n;

for(i=0;i<n;i++) // * * *

{ // * *

for(j=0;j<i;j++) // *

cout<<" ";

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

cout<<" *";

cout<<endl;

m--;

Exercises:
1. Write a C++ program to display Pascal’s triangle

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 18


LAB: 7

Objectives:

To understand the concept of arrays

// Program 22 Program to store 5 numbers in an array then print them

#include<iostream.h>

void main()

int a[5],i;

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

cout<<"enter any Number :"<<endl;

cin>>a[i];

cout<<"The Elements are as below : "<<endl;

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

cout<<a[i]<<endl;

// Program 23 Program to store 5 numbers in an array then print them in reverse order

#include<iostream.h>

void main()

int a[5],i;

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 19


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

cout<<"enter any Number :"<<endl;

cin>>a[i];

cout<<"The elements are as below : "<<endl;

for(i=4;i>=0;i--) // Reverse Order

cout<<a[i]<<endl;

// Program 24 program using arrays for checking Maximum Number and Minimum number among
elements

#include <iostream.h>

void main()

int a[8],i,max,min;

cout<<"Enter 8 number:"<<endl;

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

cin>>a[i];

max=min=a[0];

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

{ if (a[i]>max)

max=a[i];

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 20


if (a[i]<min)

min=a[i];

cout<<"Maximum number="<<max<<endl;

cout<<"Minimum number="<<min<<endl;

// Program 25 to input data into two different arrays and then add two arrays and store result in
third Array

#include<iostream.h>

main()

float a[5],b[5],s[5];

int i;

cout<<"Enter value in first array"<<endl;

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

cout<<"Enter value in element : "<<i<<" = ";

cin>>a[i];

cout<<"Enter value in 2nd array"<<endl;

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

cout<<"Enter value in element : "<<i<<" = ";

cin>>b[i];

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 21


cout<<"First + Second = Sum" <<endl;

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

s[i]=a[i]+b[i];

cout<<a[i]<<" + "<<b[i]<<" = "<<s[i]<<endl;

// Program 26 Write a program to read some values into array and sort them using bubble sort.

#include<iostream.h>

void main()

int x[10],i,n,j;

cout<<"enter count";

cin>>n;

cout<<"enter the elements";

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

cin>>x[i];

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

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

if (x[i]>x[j])

int temp=x[i];

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 22


x[i]=x[j];

x[j]=temp;

cout<<"Bubble sorted list \n";

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

cout<<x[i]<<"\n";

Exercises:
1. Write a C++ program to search an element by using binary search.

2. Write a C++ program to sort array elements by using insertion sort.

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 23


LAB: 8

Objectives:

To implement the concept of function.

// Program 27 to Add two numbers using function

#include <iostream.h>

int add(int x, int y);

void main()

int a, b;

cout<<" Enter value of a ";

cin>>a;

cout<<" Enter value of b ";

cin>>b;

add(a,b);

int add(int x, int y)

int res;

res = x+y;

cout<<" Sum = "<<res<<endl;

return res;

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 24


// Program28 function for calculating the square of any integer

#include<iostream.h>

int sq(int);

void main()

int x;

cout<<"enter the value of x ";

cin>>x;

cout<<"the square of "<<x<<"="<<sq(x)<<endl;

int sq(int x)

return x*x;

// Program 29 Write a function program to find the factorial of a number.

#include<iostream.h>

int fact(int);

void main()

int x,f;

cout<<"enter the number";

cin>>x;

f=fact(x);

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 25


cout<<"The factorial"<<f;

int fact(int a)

int i,f=1;

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

f=f*i;

return f;

Exercises:
1. Write a C++ program to interchange two numbers by using function.

2. Write a C++ program to calculate nCr using function.

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 26


LAB: 9

Objectives:

To understand the concept of files.

// Program 30 to Read a file

#include<iostream.h>

#include<fstream.h>

#include<iomanip.h>

#include<stdlib.h>

void main()

ifstream student("marks.dat",ios::in);

if (!student)

cerr<<"file could not be opened"<<endl;

exit(1);

int account;

char name[40];

double balance;

cout<<"account"<<setw(10)<<"name"<<setw(10)<<"balance"<<endl;

while(student>>account>>name>>balance)

cout<<account<<setw(15)<<name<<setw(10)<<balance<<endl;

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 27


}

}//end of main

Exercise:
1. Write a C++ program to create a file and store the details of a student.

LAB: 10

Objectives:

Understand the concepts of objects and classes.

// Program 31 Create class student read and show the details by using scope resolution operator

#include<iostream.h>

class student

int id,m1,m2,m3,tot;

char sname[10];

float avg;

public:

void read();

void show();

};

void student::read()

cout<<"enter id, student name and three marks";

cin>>id>>sname>>m1>>m2>>m3;

void student::show()

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 28


{

tot=m1+m2+m3;

avg=tot/3;

cout<<"\n Total mark"<<tot;

cout<<"\n Average mark"<<avg;

void main()

student st;

st.read();

st.show();

// Program 32 Create class student read and show the details without scope resolution operator

#include<iostream.h>

class student

int id,m1,m2,m3,tot;

char sname[10];

float avg;

public:

void read()

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 29


{

cout<<"enter id, student name and three marks";

cin>>id>>sname>>m1>>m2>>m3;

void show()

tot=m1+m2+m3;

avg=tot/3;

cout<<"\n Total mark"<<tot;

cout<<"\n Average mark"<<avg;

};

void main()

student st;

st.read();

st.show();

// Program 33 Create class result read, total and show the details by using scope resolution
operator

#include<iostream.h>

class result

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 30


{

int id,m1,m2,m3,tot;

char sname[10];

public:

void read();

void total();

void show();

};

void result::read()

cout<<"enter id, student name and three marks";

cin>>id>>sname>>m1>>m2>>m3;

void result::total()

tot=m1+m2+m3;

void result::show()

cout<<"\n Student Id"<<id;

cout<<"\n Total mark"<<tot;

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 31


void main()

result st;

st.read();

st.total();

st.show();

// Program 34 Create class result read, total and show the details without scope resolution operator

#include<iostream.h>

class result

int id,m1,m2,m3,tot;

char sname[10];

public:

void read()

cout<<"enter id, student name and three marks";

cin>>id>>sname>>m1>>m2>>m3;

void total()

tot=m1+m2+m3;

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 32


}

void show()

cout<<"\n Student Id"<<id;

cout<<"\n Total mark"<<tot;

};

void main()

result st;

st.read();

st.total();

st.show();

// Program 35 Create class employee read and show the details by using scope resolution operator

#include<iostream.h>

class employee

int eno,sal;

char ename[10];

public:

void accept();

void disp();

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 33


};

void employee::accept()

cout<<"enter emp id, emp name and salary";

cin>>eno>>ename>>sal;

void employee::disp()

cout<<"\n The employee details";

cout<<eno<<"\n"<<ename<<"\n"<<sal;

void main()

employee e;

e.accept();

e.disp();

// Program 36 Create class employee read and show the details without scope resolution operator

#include<iostream.h>

class employee

int eno,sal;

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 34


char ename[10];

public:

void accept()

cout<<"enter emp id, emp name and salary";

cin>>eno>>ename>>sal;

void disp()

cout<<"\n The employee details";

cout<<eno<<"\n"<<ename<<"\n"<<sal;

};

void main()

employee e;

e.accept();

e.disp();

Exercises:
1. Create class item read and display the details of an item by using class.

LAB: 11

Revision

--------------------**************************----------------------------**************-------------------------

BCA202 – Lab Manual : By Ishtiyaq Ahmed Page 35

You might also like