0% found this document useful (0 votes)
2 views

c++

This document is a practical file for BCA First Year students at Sarojini Naidu Govt. Girls P.G. College, detailing various programming exercises in Programming Methodology & Data Structure. It includes a list of 25 programming tasks, such as swapping variables, checking even/odd numbers, matrix operations, and demonstrating different inheritance types in C++. Each task is accompanied by sample code and expected outputs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c++

This document is a practical file for BCA First Year students at Sarojini Naidu Govt. Girls P.G. College, detailing various programming exercises in Programming Methodology & Data Structure. It includes a list of 25 programming tasks, such as swapping variables, checking even/odd numbers, matrix operations, and demonstrating different inheritance types in C++. Each task is accompanied by sample code and expected outputs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Sarojini Naidu Govt. Girls P.G.

(Autonomous) College
Shivaji Nagar, Bhopal (M.P.)

SESSION : 2024 - 2025

BCA First Year


PRACTICAL FILE

MAJOR-I
Programming Methodology & Data Structure

Submitted to- Submitted by-


Dr. Neetu Sahu Ma’am Palak Tripathi
Roll no. :
24510045
Index

1. Write a program to swap the contents of two variables..


2. Write a program to check whether a given number is even or odd.
3. Write a program to print table of any number.
4. Write a program to print Fibonacci series.
5. Write a program to find factorial of given number.
6. Write a program to calculate the sum of two matrices.
7. Write a program to find transpose of matrix.
8. Write a program to generate even series from 1to
1to100.
9. Write a program to demonstrate use of default conductor.
10. Write a program
ogram whether given number is prime or not.
11. Write a program to demonstrate use of friend function.
12. Write a program to create a pyramid structure.

13. Write a program to input 5 numbers using array add them and find average.
14. Write a program to find largest eelement in an array.
15. Write a program to find sum of two numbers using call by value method.
16. Write a program to calculate power of a number.
17. Write a program to print digits of entered number in reverse order.
18. Write a program for Linear Search.
19. Write a program
m for Binary Search
20. Write a program for Insertion Sort.
21. Write a program to implement single inheritance
inheritance.
22. Write a program to implement multiple inheritance
inheritance.
23. Write a program to implement multilevel inheritance.
24. Write a program to implement hierarchical inher
inheritance.
25. Write a program to implement hybrid inheritance.
1. Write a program to swap the contents of two variables.

#include<iostream.h>
#include<conio.h>
void main( )
{
int a,b,c;
cout<<"enter two numbers";
cin>>a>>b;
cout<<"before swapping"<<endl;
cout<<"a="<<a<<""<<"b="<<b<<endl;
c=a;
a=b;
b=c;
cout<<"after swapping"<<endl;
cout<<"a="<<a<<""<<"b="<<b<<endl;
getch( );
}

Output :-
2. Write a program to check whether given number is even or odd.

#include<iostream.h>
#include<conio.h>
void main()
{
int num;
cout<<"enter number:";
cin>>num;
if(num%2==0)
cout<<"number is even";
else
cout<<"number is odd";
getch();
}

Output :-
3. Write a program to print table of any number.

#include<iostream.h>
#include<conio.h>
void main()
{
int i, num;
cout<<”enter number";
cin>>num;
for (i=1;i<=10;i++)
{
cout<<num<< “x”<<i<<
“=”<<num*i<<
}
getch();
}

Output :-
4. Write a program to print Fibonacci series.

#include<iostream.h>
#include<conio.h>
void main()
{
int n,x,y,z;
cout<<"enter number";
cin>>n;
x=0;
y=1;
z=0;
while (z<=n)
{
cout<<"n"<<z;
x=y;
y=z;
z=x+y;
}
getch();
}

Output :-
5. Write a program to find factorial of given numbers.

#include<iostream.h>
#include<conio.h>
void main()
{
int i,p=1,a;
cout<<"enter number";
cin>>a;
for(i=1;i<=a;i++)
{
p=p*i;
}
cout<<"factorial of"<<a<<"is"<<p;
getch();
}

Output :-
6. Write a program to calculate the sum of two matrices.
#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
inta[3][3],b[3][3],c[3][3],i,j;
cout<<"enter nine elements of first matrices";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
}
cout<<"enter nine elements of second matrix";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>b[i][j];
}
cout<<"sum of matrix a and b is"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
cout<<c[i][j]<<"\t";
}
cout<<endl;
}
getch();
}

Output :-
7. Write a program to find transpose of given matrices.

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a[3][3],b[3][3],i,j;
cout<<"enter nine elements of first matrices";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
}
cout<<"transpose of matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[j][i]<<"\t";
}
cout<<endl;
}
getch();
}

Output :-
8. Write a program to generate even series from 1 to 100.
#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int i;
for(i=1;i<=100;i++)
{
if(i%2==0)
{
cout<<i<<"\t";
}
}
getch();
}

Output :-
9.Write a program to demonstrate use of default constructor.

#include <iostream.h>
#include<conio.h>
class myclass
{
clrscr( );
private:
int num;
public:
myclass ( )
{
num=0;
cout<< “ Default constructor called!”<<endl;
}
void display( )
{
cout<< “ num:” <<num<<endl;
}
};
void main ( )
{
myclass obj1;
obj1.display( );
getch( );
}

Output :-
10.Write a program whether a given number is prime or not.

#include<iostream.h>
#include<conio.h>

void main()
{
int num , i , s , f=0;
cout<<"enter numbers";
cin>>num;
for (i=2;i<num;i++)
{
s=num%i;
if(s==0)
{
f=1;
break;
}
}
if(f==0)
cout<<"no. is prime"<<endl;
else
cout<<"no. is not prime"<<endl;
getch();
}

Output :-
11.Write a program to demonstrate use of friend function.
function

#include<iostream.h>
#include<conio.h>
class Box
{
private:
double width;
public:
Box(double w)
{
width = w;
}
friend void showWidth(Box b);
};
void showWidth(Box b)
{
cout<< “Width of box :”<<b.width<<endl;
}
void main ()
{
clrscr ();
Box myBox (10.5)
showWidth(myBox);
getch();
}

Output :-
12.Write a program to create a pyramid structure.

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr( );
int i , j;
for (i=1;i<6;i++)
{
for(j=1;j<;j++)
{
cout<< “ *”;
}
cout<<endl;
}
getch();
}

Output :-
13.Write a program to input 5 numbers using array add them and find average
average.

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int numbers[5];
int i , sum = 0 , average;
cout<< “enter elements”;
for(i=0;i<5;i++)
{
cin>>numbers[i];
}
for(i=0;i<5;i++)
{
sum += numbers[i];
}
average=sum/5;
cout<< “The sum of the numbers is :”<<sum<<endl;
cout<< “The average of the number is :”<<average<<endl;
getch();
}

Output :-
14. Write a program to find largest element in an array.

#include<iostream.h>
#include<conio.h>
void main()
{
int i,a[5];
cout<<"enter five elements";
for(i=0;i<5;i++)
{
cin>>a[i];
}
for(i=0;i<5;i++)
{
if(a[0]<a[i])
a[0]=a[i];
}
cout<<"largest elements is:"<<a[0];
getch();
}

Output :-
15.Write a program to find sum of two numbers using call by value method.
meth
#include<iostream.h>
#include<conio.h>
void add(int a , int b)
{
int sum=a+b;
cout<< “ the sum is :”<<sum<<endl;
}
void main()
{
int num1 , num2;
cout<< “ Enter first number :”<<endl;
cin >>num1;
cout<< “Enter second number :”<<endl;
cin >>num2;
add(num1 , num2);
getch();
}

Output :-
16.Write a program to calculate power of a number
number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
double base , exponent , result = 1.0;
cout<< “ Enter base :”<<endl;
cin>> base ;
cout>> “ Enter exponent :”<<endl;
cin>> exponent ;
for (int i=1;i<=exponent;i++)
{
result *=base;
}
cout<<base<<(“ ”)<< “ raised to the power ”<<(“ ”)<<exponent<<(“ ”)<< “is”<<(“ ”)<<result<<endl;
getch();
}

Output :-
17.Write a program to print digits of entered number in reverse order.
#include<iostream.h>
#include<conio.h>
void main ( )
{
int n , reversed = 0 , remainder;
cout<< “ enter number ” ;
cin>>n;
while(n!=0)
{
remainder = n%10;
reversed = reversed * 10+ remainder;
n=n/10;
}
cout<< “ reversed number =”<<reversed;
getch();
}

Output :-
18.Write a program for Linear search.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10] , i , n , search;
cout<< “enter no.”<<endl;
cin>>n;
cout<< “enter”<<n<< “number”;
for(i=0;i<n;i++)
{
cout<< “enter search element”<<endl;
cin >>search;
for(i=0;i<n;i++)
{
if(a[i]== search)
break;
}
cout<<search<< “”<< “presentart”<<i+1<< “” << “position”;
getch ();
}

Output :-
19.Write a program for Binary search.
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr();
int [a] , i , F , L , Mid , search;
cout<< “enter ten elements”<<endl;
for(i=0;i<10;i++)
{
cin>>a[i];
}
Cin>>search;
F=0,L=9;
Mid=(F+L)/2;
while(F<=L)
{
if(a[i]==search)
{
cout<<search<< “”<< “is present at index”<< “”<<Mid+1;
break;
}
else if(a[Mid]<search)
F=Mid+1;
else
L=Mid-1;
Mid(F+L)/2;
}
if(F>L)
cout<< “not found”;
getch();
}

Output :-
20. Write a program for Insertion sort.

#include<iostream.h>
#include<conio.h>voi
d main()
{
clrscr();
inta[5],i,j, t;
cout<<"enterfiveelements";
for(i=0;i<5;i++)
cin>>a[i];
for(i=1;i<5;i++)
{
j=i;
while(j>0&&a[j-1]>a[j])
{
t=a[j];
a[j]=a[j-1];
a[j-1]=t;
j--;
}
}
cout<<"sortedlistare"<<endl;
for(i=0;i<5;i++)
{
cout<<a[i]<<"";
}
getch();
}

Output :-
21. Write a program to implement single inheritance .

#include<iostream.h>
#include<conio.h>
class animal
{
public:
void eat()
{
cout<< “I can eat !”<<endl;
}
void sleep()
{
cout<< “I can sleep !”<<endl;
}
};
class dog:public animal
{
public:
void bark()
{
cout<< “I can bark !”<<endl;
}
};
void main()
{
dog dog1;
dog1.eat();
dog1.sleep();
dog1.bark();
getch();
}

Output :-
22. Write a program to implement multiple inheritance.
#include<iostream.h>
#include<conio.h>
class A
{
public:
A()
{
cout<< “ constructor of class A”<<endl;
}
};
class B
{
public:
B()
{
cout << “constructor of class B”<<endl;
}
};
class C: public A, public B
{
public:
C()
{
cout<< “constructor of class C”<<endl;
}
};
void main()
{
C obj;
getch();
}

Output :-
23. Write a program to implement multilevel inheritance.
#include<iostream.h>
#include<conio.h>
class A
{
public:
A()
{
cout<< “constructor of A”<<endl;
}
};
class B:public A
{
public:
B()
{
cout<< “constructor of class B”;
}
};
class C:public B
{
public:
C()
{
cout<< “constructor of class C”;
}
};
void main()
{
clrscr();
C obj;
getch();
}

Output :-
24. Write a program to implement hierarchical inheritance.

#include<iostream.h>
#include<conio.h>
class A
{
public:
A()
{
cout<< “constructor of class A”<<endl;
}
};
class B:public A
{
public :
B()
{
cout<< “constructor of class B”<<endl;
}
};
class C:public A
C()
{
cout<< “ constructor of class C”;
}
};
void main()
{
clrscr();
B obj1;
C obj2;
getch();
}

Output :-
25. Write a program to implement hybrid inheritance .
#include<iostream.h>
#include<conio.h>
class A
{
public:
int x;
};
class B:public A
{
public:
B()
{
x=10;
}
};
class C;
{
public
int y;
C()
{
y=4;
}
};
class D:public,public C
{
public:
void sum()
{
cout<< “sum=”<<x+y;
}
};
void main()
{
clrscr();
D obj;
obj.sum();
getch();
}

Output :-

You might also like