Programming lab
STUDENT’S DETAILS: TEACHER’S DETAILS:
Name: PUNIT KUMAR Name: Mr. B.P Singh
Course: B-TECH/CST
Enrollment No: 00527912722
S.N CONTENT SIGNA
 PAG
O TURE
 E
 No.
1. Write a program in c++ to
 find the area and perimeter of
 a rectangle.
2. Write a program in c++ to
 calculate the square of a
 number using math.h header
 file
3. Write a program in c++ to
 find that a no. is even or odd.
4. Write a program in c++ to
 find greater no. between two
 no.
5. Write a program in c++ to
 display the square series
 using loop.
6. Write a program in c++ to
 display reverse natural series
 using loop.
7. Write a program in c++ to
 calculate the factorial of a
 given number.
8. Write a program in c++ to
 display call by value function.
9. Write a program in c++ to
 display function overloading
10. Write a program in c++ to
 display the class.
11. Write a program in c++ to
 display a class using scope
 resolution operator.
12. Write a program in c++
 display constructor and
 destructor.
13. Write a program in c++ to
INDEX
 PROGRAM.1
#include<iostream>
using namespace std;
int areaRectangle(int a, int b)
{
int area = a * b;
return area;
}
int perimeterRectangle(int a, int b)
{
int perimeter = 2*(a + b);
return perimeter;
}
int main()
{
int a = 5;
int b = 6;
cout << "Area = " <<
 areaRectangle(a, b) <<
 endl;
cout << "Perimeter = " <<
 perimeterRectangle(a, b);
return 0;
}
 OUTPUT.1
Area = 30
Perimeter = 22
 PROGRAM.2
#include <iostream>
#include <cmath>
int main() {
 double number, square;
 std::cout << "Enter a number: ";
 std::cin >> number;
 square = pow(number, 2);
 std::cout << "The square of " << number << " is: " << square << std::endl;
 return 0;
}
 OUTPUT.2
Enter a number: 5
The square of 5 is: 25
 PROGRAM.3
#include <iostream>
using namespace std;
int main() {
 int n;
 cout << "Enter an integer: ";
 cin >> n;
 if ( n % 2 == 0)
 cout << n << " is even.";
 else
 cout << n << " is odd.";
 return 0;
}
 OUTPUT.3
Enter an integer: 4
4 is even.
 PROGRAM.4
#include <iostream>
int main() {
 double num1, num2;
 std::cout << "Enter the first number: ";
 std::cin >> num1;
 std::cout << "Enter the second number: ";
 std::cin >> num2;
 if (num1 > num2) {
 std::cout << num1 << " is the largest number." << std::endl;
 } else if (num2 > num1) {
 std::cout << num2 << " is the largest number." << std::endl;
 } else {
 std::cout << "Both numbers are equal." << std::endl;
 }
 return 0;
}
 OUTPUT.4
Enter the first number: 50
Enter the second number: 20
50 is the largest number.
 PROGRAM.5
#include <iostream>
int main() {
 int n;
 std::cout << "Enter the number of terms in the square series: ";
 std::cin >> n;
 std::cout << "Square Series:" << std::endl;
 for (int i = 1; i <= n; ++i) {
 std::cout << i * i << " ";
 }
 std::cout << std::endl;
 return 0;
}
 OUTPUT.5
Enter the number of terms in the square series: 9
Square Series:
1 4 9 16 25 36 49 64 81
 PROGRAM.6
#include<iostream>
using namespace std;
int main()
 int number;
 cout << "\nPlease Enter Maximum Value to print Natural Numbers = ";
 cin >> number;
 cout << "\nList of Natural Numbers from " << number << " to 1 are\n";
 for(int i = number; i >= 1; i--)
 cout << i <<" ";
 return 0;
}
 OUTPUT.6
Please Enter Maximum Value to print Natural Numbers
= 10
List of Natural Numbers from 10 to 1 are
10 9 8 7 6 5 4 3 2 1
 PROGRAM.7
#include <iostream>
using namespace std;
int main() {
 int n;
 long factorial = 1.0;
 cout << "Enter a positive integer: ";
 cin >> n;
 if (n < 0)
 cout << "Error! Factorial of a negative number doesn't exist.";
 else {
 for(int i = 1; i <= n; ++i) {
 factorial *= i;
 cout << "Factorial of " << n << " = " << factorial;
 return 0;
}
 OUTPUT.7
Enter a positive integer: 5
Factorial of 5 = 120
 PROGRAM.8
#include <iostream>
using namespace std;
void swap(int x, int y){
 int t = x;
 x = y;
 y = t;
 cout << "After Swapping in function x: " << x
 << ", y: " << y << endl;
int main(){
 int x = 1, y = 2;
 cout << "Before Swapping: ";
 cout << "x: " << x << ", y: " << y << endl;
 swap(x, y);
 cout << "After Swapping: ";
 cout << "x: " << x << ", y: " << y << endl;
 return 0;
}
 OUTPUT.8
Before Swapping: x: 1, y: 2
After Swapping in function x: 2, y: 1
After Swapping: x: 1, y: 2
 PROGRAM.9
#include <iostream>
using namespace std;
float absolute(float var){
 if (var < 0.0)
 var = -var;
 return var;
int absolute(int var) {
 if (var < 0)
 var = -var;
 return var;
int main() {
 cout << "Absolute value of -5 = " << absolute(-5) << endl;
 cout << "Absolute value of 5.5 = " << absolute(5.5f) << endl;
 return 0;
}
 OUTPUT.9
Absolute value of -5 = 5
Absolute value of 5.5 = 5.5
 PROGRAM.10
#include <iostream>
using namespace std;
class Room {
 public:
 double length;
 double breadth;
 double height;
 double calculateArea() {
 return length * breadth;
 double calculateVolume() {
 return length * breadth * height;
 }
};
int main() {
 Room room1;
 room1.length = 42.5;
 room1.breadth = 30.8;
 room1.height = 19.2;
 cout << "Area of Room = " << room1.calculateArea() << endl;
 cout << "Volume of Room = " << room1.calculateVolume() << endl;
 return 0;
}
 OUTPUT.10
Area of Room = 1309
Volume of Room = 25132.8
 PROGRAM.11
#include <iostream>
using namespace std;
class Operate
public:
 void fun();
};
void Operate::fun() /* return_type Class_Name::function_name */
cout << " It is the member function of the class. ";
int main ()
Operate op;
op.fun();
return 0;
}
 OUTPUT.11
It is the member function of the class.
 PROGRAM.12
#include <iostream>
using namespace std;
class Line {
 public:
 void setLength( double len );
 double getLength( void );
 Line(); // This is the constructor declaration
 ~Line(); // This is the destructor: declaration
 private:
 double length;
};
Line::Line(void) {
 cout << "Object is being created" << endl;
Line::~Line(void) {
 cout << "Object is being deleted" << endl;
}
void Line::setLength( double len ) {
 length = len;
double Line::getLength( void ) {
 return length;
int main() {
 Line line;
 line.setLength(6.0);
 cout << "Length of line : " << line.getLength() <<endl;
 return 0;
 OUTPUT.12
Object is being created
Length of line : 6
Object is being deleted
 PROGRAM.13
#include <iostream>
using namespace std;
class Count {
 private:
 int value;
 public:
 Count() : value(5) {}
 void operator ++ () {
 ++value;
 void operator ++ (int) {
 value++;
 }
 void display() {
 cout << "Count: " << value << endl;
};
int main() {
 Count count1;
 count1++;
 count1.display();
 ++count1;
 count1.display();
 return 0;
 OUTPUT.13
Count: 6
Count: 7
 PROGRAM.14
#include <iostream>
using namespace std;
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! Woof woof!!" << endl;
};
int main() {
 Dog dog1;
 dog1.eat();
 dog1.sleep();
 dog1.bark();
 return 0;
 OUTPUT.14
I can eat!
I can sleep!
I can bark! Woof woof!!
 PROGRAM.15
#include <iostream>
using namespace std;
class A {
 protected:
 int a;
 public:
 void get_a(int n) {
 a = n;
};
class B {
 protected:
 int b;
 public:
 void get_b(int n) {
 b = n;
};
class C : public A,public B
{
 public:
 void display()
 std::cout << "The value of a is : " <<a<< std::endl;
 std::cout << "The value of b is : " <<b<< std::endl;
 cout<<"Addition of a and b is : "<<a+b;
};
int main()
 C c;
 c.get_a(10);
 c.get_b(20);
 c.display();
 return 0;
 OUTPUT.15
The value of a is : 10
The value of b is : 20
Addition of a and b is : 30
 PROGRAM.16
#include <iostream>
using namespace std;
class Base {
 public:
 virtual void print() {
 cout << "Base Function" << endl;
 }};
class Derived : public Base {
 public:
 void print() {
 cout << "Derived Function" << endl;
 }};
int main() {
 Derived derived1;
 Base* base1 = &derived1;
 base1->print();
 return 0;
 OUTPUT.16
Derived Function
 PROGRAM.17
#include<iostream>
#include<fstream>
using namespace std;
main()
 int rno,fee;
 char name[50];
 cout<<"Enter the Roll Number:";
 cin>>rno;
 cout<<"\nEnter the Name:";
 cin>>name;
 cout<<"\nEnter the Fee:";
 cin>>fee;
 ofstream fout("d:/student.doc");
 fout<<rno<<"\t"<<name<<"\t"<<fee; //write data to the file student
 fout.close();
 ifstream fin("d:/student.doc");
 fin>>rno>>name>>fee; //read data from the file student
 fin.close();
 cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
 return 0;
 OUTPUT.17
Enter the Roll Number:23
Enter the Name:mayank
Enter the Fee:90000
23 mayank 90000
 PROGRAM.18
#include <iostream>
using namespace std;
template <typename T> T myMax(T x, T y)
 return (x > y) ? x : y;
int main()
 // Call myMax for int
 cout << myMax<int>(3, 7) << endl;
 // call myMax for double
 cout << myMax<double>(3.0, 7.0) << endl;
 // call myMax for char
 cout << myMax<char>('g', 'e') << endl;
 return 0;
 OUTPUT.18
7
 PROGRAM.19
#include <iostream>
using namespace std;
int sum(int num1, int num2) {
 return num1 + num2;
double sum(double num1, double num2) {
 return num1 + num2;
int sum(int num1, int num2, int num3) {
 return num1 + num2 + num3;
int main() {
 cout << "Sum 1 = " << sum(5, 6) << endl;
 cout << "Sum 2 = " << sum(5.5, 6.6) << endl;
 cout << "Sum 3 = " << sum(5, 6, 7) << endl;
 return 0;
 OUTPUT.19
Sum 1 = 11
Sum 2 = 12.1
Sum 3 = 18