C++ Lab Practicle Assignment Solutions
C++ Lab Practicle Assignment Solutions
**Index**
initialization.
Answer:-
a. Algorithm
a. Program start with #include<iostream> and <conio.h> header file.
b. int find_facto() function defined, return type with argument.
c. In the main( ) function, declaration of variable with their data type.
d. find_facto() function called and pass them num, it return the factorial of
the given number which is going to store in fact variable.
e. Print the value of fact, then it will terminate.
b. Flowchart
Start
Variable declaration
as num
True False
if
(num< =0 )
find_facto() called
!! Error !!
True False
i <=n;
++i
Stop
5
#include<iostream>
#include<conio.h>
using namespace std;
int find_facto(int n) // function definition
{
int fact=1;
for(int i = 1; i <=n; ++i)
{ fact *= i; }
return fact;
}
int main()
{
int num;
cout<<"Enter a number which factorial you want : ";
cin>>num;
if (num <= 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else
cout<<"Factorial is : "<<find_facto(num)<<endl;
return 0;
}
d. Output screen:-
6
Answer:-
a. Algorithm
a. Program start with #include<iostream> and <conio.h> header file.
b. area_of_circle() defined as Inline function with argument.
c. Passing the radius as a function argument in area_of_circle() function.
d. It give us output then, program will terminate.
b. Flowchart
Start
Variable declaration as
radius
Stop
7
#include<iostream>
#include<conio.h>
using namespace std;
inline float area_of_circle(float radius) //inline function definition
{
cout<<"Area of circle is : "<<22/7.0 * radius * radius<<endl; //area of circle
}
int main()
{ float radius;
cout<<"Enter radius of circle : ";
cin>>radius;
area_of_circle(radius); //calculate the area of circle
return 0;
}
d. Output screen:-
8
Flowchart
Start
Read num
True False
num<=0
fact = find_facto(num);
True
n<1 Return 1
False
Return n * fact(n-1)
Print fact
Stop
9
#include<iostream>
#include<conio.h>
using namespace std;
find_facto(int n) // function definition
{
if( n < 1)
return 1;
else
return n*find_facto(n-1); //recursion
}
int main()
{
int num, fact;
cout<<"Enter a number which factorial you want : ";
cin>>num;
if (num <= 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else
cout<<"Factorial is : "<<find_facto(num)<<endl;
return 0;
}
Output screen:-
10
b. Flowchart
Start
Variable declaration
as num, flag, i
True Flase
if (num == 0
|| num == 1)
num% i ==
0
isPrime = false;
break;
False True
Stop
11
#include <iostream>
using namespace std;
int prime();
int prime() // Return type of function is int
{ int n;
Cout<<"Enter a positive integer to check: ";
cin >> n;
return n;
}
int main()
{ int num, i;
bool isPrime = true;
num = prime(); // No argument is passed to prime()
if (num == 0 || num == 1)
{ isPrime = false; }
else {
for (i = 2; i <= num/ 2; ++i) {
if (num% i == 0) {
isPrime = false; break;
}
}
}
if (isPrime)
cout << num << " is a prime number";
else
cout << num << " is not a prime number";
return 0;
}
d. Output screen:-
12
Answer:-
a. Coding for the program
#include<iostream>
using namespace std;
float sim_inter(int , int , int );
float sim_inter(int x, int y=1, int z=1) // A function with default arguments, it can be called with
{ // 2 arguments or 3 arguments or 4 arguments
float sim_inter;
sim_inter =(x*y*z)/100;
return sim_inter;
}
Output screen:-
13
int main()
{
Polar p1, p2, addition; //objects created
cout<<"Enter First object : "<<endl<<endl;
p1.input();
cout<<endl<<"Enter second object : "<<endl<<endl;
p2.input();
cout<<endl;
addition = add(p1, p2); //calling friend function add(Polar,Polar)
cout<<endl<<"First object : "<<endl;
p1.display();
cout<<endl<<"Second object : "<<endl;
p2.display();
cout<<endl<<"Addition of first & second : "<<endl;
addition.display();
return 0;
}
Output screen:-
15
b. Output screen:-
16
b. Output screen:-
17
b. Output screen:-
18
int main()
{
void* ptr;
float f = 2.3;
int a=12;
b. Output screen:-
19
Answer:-
a. Coding for the program
#include <iostream>
using namespace std;
int main ()
{
int var;
int *ptr;
int **pptr;
var = 3000;
return 0;
}
Output screen:-
20
b. Output screen:-
21
13. Write a program to create classes which have Static Member (Data member).
Answer:-
a. Coding for the program
#include <iostream>
using namespace std;
class Student { private:
int rollNo;
char name[10];
public:
static int objectCount;
Student() {
objectCount++;
}
void getdata() {
cout << endl<<"Enter roll number: ";
cin >> rollNo;
cout <<"Enter name: ";
cin >> name;
}
void putdata() {
cout<<endl<<"Roll Number = "<< rollNo <<endl;
cout<<"Name = "<< name <<endl;
}
};
int Student::objectCount = 0;
int main(void) {
Student s1, s2;
s1.getdata();
s2.getdata();
s1.putdata();
s2.putdata();
cout <<endl<< "Total "<<Student::objectCount <<" Record inserted " ;
return 0;
}
Output screen:-
22
14. Write a program to create function with no Return type and no Argument.
Answer:-
a. Coding for the program
# include <iostream>
using namespace std;
void prime();
int main()
{
prime(); // No argument is passed to prime()
return 0;
}
void prime() // Return type of function is void because value is not returned
{
int num, i, flag = 0;
cout << "Enter a positive integer enter to check: ";
cin >> num;
for(i = 2; i <= num/2; ++i)
{
if(num % i == 0)
{ flag = 1;
break;
}
}
if (flag == 1)
{ cout << num << " is not a prime number.";
}
else
{ cout << num << " is a prime number.";
}
}
Output:-
23
for(int i=0;i<=2;i++)
{
if(roll==stu[i].rollNo)
{ stu[i].display();
}
}
return 0;
}
c. Output screen:-
25
16. Write a program to create a class which has Array as a data member.
Answer:-
#include<iostream>
using namespace std;
const int size=5;
class student
{
int roll_no;
int marks[size]; //Array as data member
public:
void getdata ();
void tot_marks ();
};
cout<<endl<<endl;
return 0;
}
Output screen:-
26
}
};
int main()
{
A a1("Bhupendra_das" , 100); // Calling the parameterized
constructor.
A a2(a1); // Calling the copy constructor.
return 0;
}
Output screen:-
27
int main(){
cout<<"Using Function template :-"<<endl;
func(9, 5); // func(int, int);
func(3.7, 5.6); //func(double, double);
A <float>add_float(4.6, 8.9);
A <double>add_double(3.145, 5.268);
add_int.show();
cout<<endl;
add_float.show();
cout<<endl;
add_double.show();
return 0;
}
Output screen:-
29
int main()
{
derived d;
cout<<endl;
return 0;
}
Output screen:-
30
Answer:-
#include<iostream>
using namespace std;
class arithmetic
{
protected:
int num1, num2;
public:
void getdata()
{
cout<<"For Addition:";
cout<<"\nEnter the first number: ";
cin>>num1;
cout<<"Enter the second number: ";
cin>>num2;
cout<<endl;
}
};
class minus1
{
protected:
int n1,n2,diff;
public:
void sub()
{
cout<<"\nFor Subtraction:";
cout<<"\nEnter the first number: ";
cin>>n1;
cout<<"Enter the second number: ";
31
cin>>n2;
diff=n1-n2;
}
};
int main()
{
result z;
z.getdata();
z.add();
z.sub();
z.display();
return 0;
}
Output screen:-
32
int main(void) {
Box Box1(3.3, 1.2, 1.5, 1); // Declare box1
Box Box2(8.5, 6.0, 2.0, 2); // Declare box2
if(Box1.compare(Box2))
cout << "Box2 is smaller than Box1" <<endl;
else
cout << "Box2 is equal to or larger than Box1" <<endl;
cout<<endl;
cout<<endl;
return 0;
}
Output screen:-
33
Output screen:-
34
Answer:-
#include <iostream>
int main () {
int var[MAX] = {4126, 2091, 2001};
int *ptr[MAX];
return 0;
}
Output screen:-
35
Output screen:-
36
void show()
{
cout << "Show derived class" << endl;
}
};
int main()
{
base* bptr;
derived d;
bptr = &d;
bptr->print(); // virtual function, binded at runtime
bptr->show(); // Non-virtual function, binded at compile time
return 0;
}
Output screen:-
37
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {real = r; imag = i;}
int main()
{
Complex c1(10, 5), c2(2, 4);
cout<<"First Complex number is : ";
c1.print();
cout<<"second Complex number is : ";
c2.print();
Complex c3 = c1 + c2; // An example call to "operator+"
cout<< endl<<"Sum of the given Complex number is: ";
c3.print();
}
Output screen:-
38
Output screen:-
39
#include <iostream>
using namespace std;
// function with 2 parameters
void display(int var1, double var2)
{
cout << "Integer number: " << var1;
cout << " and double number: " << var2 << endl;
}
// function with double type single
parameter
void display(double var)
{
cout << "Double number: " << var << endl;
}
int main()
{
int a = 5;
double b = 5.5;
// call function with int type parameter
display(a);
// call function with double type parameter
display(b);
// call function with 2 parameters
display(a, b);
return 0;
}
Output screen:-
40
#include <iostream>
using namespace std;
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
int main() {
Derived derived1, derived2;
derived1.print();
derived2.Base::print();
return 0;
}
Output screen:-
41
#include<string.h>
#include<iostream>
using namespace std;
int main ()
{
char str1[]= "Test"; //Array of characters
char *p1;
p1= &str1[0];
cout<<"Display str1 using the Array of characters:-"<<endl;
int len = strlen(str1);
for(int i =0; i<len;i++)
{
cout<<str1[i] ;
}
cout<<endl<<endl<<"Display the str1 value by using pointer : "<<endl;
for(int i =0; i<len;i++)
cout<<*(p1+i);
cout<<endl;
cout << ptr << "\n"; // Output the memory address of str2 with the pointer
return 0;
Output screen: -