0% found this document useful (0 votes)
14 views20 pages

C++Lab Notes

c++ lab cycle

Uploaded by

sushmaraj.0216
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
14 views20 pages

C++Lab Notes

c++ lab cycle

Uploaded by

sushmaraj.0216
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 20

PART A

1. Program to swap 2 numbers with and without using temporary


variable.
a. Swap Numbers (Using Temporary Variable)
#include <iostream>
using namespace std;

int main()
{
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
int temp = num1;
num1 = num2;
num2 = temp;
cout << "After swapping (with temporary variable):\n";
cout << "First number: " << num1 << endl;
cout << "Second number: " << num2 << endl;
return 0;
}

b. Swapping without a Temporary Variable (using Arithmetic Operations)


#include <iostream>
using namespace std;

int main()
{
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
cout << "After swapping (without temporary variable):\n";
cout << "First number: " << num1 << endl;
cout << "Second number: " << num2 << endl;

return 0;
}
2. Program to convert the Fahrenheit to Celsius and vice-versa.
#include <iostream>
using namespace std;

int main()
{
int choice;
double fahrenheit, celsius;

cout << "Temperature Conversion Program" << endl;


cout << "1. Fahrenheit to Celsius" << endl;
cout << "2. Celsius to Fahrenheit" << endl;
cout << "Enter your choice (1 or 2): ";
cin >> choice;

if (choice == 1)
{
cout << "Enter temperature in Fahrenheit: ";
cin >> fahrenheit;
celsius = (fahrenheit - 32) * 5.0 / 9.0;
cout << "Temperature in Celsius: " << celsius << "°C" << endl;
} else if (choice == 2)
{
cout << "Enter temperature in Celsius: ";
cin >> celsius;
fahrenheit = (celsius * 9.0 / 5.0) + 32;
cout << "Temperature in Fahrenheit: " << fahrenheit << "°F" << endl;
}
else
{
cout << "Invalid choice. Please select 1 or 2." << endl;
}

return 0;
}
3. Program to compute to add and multiply two complex numbers.
#include <iostream>
#include <complex>

using namespace std;


int main()
{
complex<double> num1, num2, sum, product;
double real1, imag1;
cout << "Enter the real and imaginary parts of the first complex number: ";
cin >> real1 >> imag1;
num1 = complex<double>(real1, imag1);
double real2, imag2;
cout << "Enter the real and imaginary parts of the second complex number: ";
cin >> real2 >> imag2;
num2 = complex<double>(real2, imag2);
sum = num1 + num2;
product = num1 * num2;
cout << "Sum: " << sum.real() << " + " << sum.imag() << "i" << endl;
cout << "Product: " << product.real() << " + " << product.imag() << "i" << endl;

return 0;
}

Or

#include <iostream>
using namespace std;

int main()
{
int real1, real2, img1, img2, realsum, imgsum, realmul, imgmul;

cout << "Enter a and b where a + ib is the first complex number.";


cout << "\na = ";
cin >> real1;
cout << "\nb = ";
cin >> img1;
cout << "Entered complex number is " << real1 << " + " << img1 << "i";

cout << "Enter c and d where c + id is the second complex number.";


cout << "\nc = ";
cin >> real2;
cout << "\nd = ";
cin >> img2;
cout << "Entered complex number is " << real2 << " + " << img2 << "i \n";

realsum = (real1 + real2);


imgsum = (img1 + img2);

cout << "Entered complex number sum is " << realsum << " + " << imgsum << "i \n";
realmul = (real1 * real2);
imgmul = (img1 * img2);

cout << "Entered complex number sum is " << realmul << " + " << imgmul << "i \n";

return 0;
}

4. Program to demonstrate functions of simple calculator.


#include <iostream>
using namespace std;

int main() {

char op;
float num1, num2, result;
cout << "Select operation:\n";
cout << " + for addition\n";
cout << " - for subtraction\n";
cout << " * for multiplication\n";
cout << " / for division\n";
cout << "Enter operation: ";
cin >> op;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;

switch(op)
{
case '+':
result = num1 + num2;
cout << "Result: " << result << endl;
break;
case '-':
result = num1 - num2;
cout << "Result: " << result << endl;
break;
case '*':
result = num1 * num2;
cout << "Result: " << result << endl;
break;
case '/':

if (num2 != 0)
{
result = num1 / num2;
cout << "Result: " << result << endl;
}
else
{
cout << "Error: Division by zero!" << endl;
}
break;
default:
cout << "Error: Invalid operator!" << endl;
break;
}

return 0;
}

5. Program to display multiplication table of a given number.


#include <iostream>
using namespace std;

int main()
{
int number;

cout << "Enter a number to display its multiplication table: ";


cin >> number;

cout << "Multiplication Table of " << number << ":\n";


for (int i = 1; i <= 10; ++i)
{
cout << number << " * " << i << " = " << (number * i) << "\n";
}

return 0;
}

6. Program to check whether a number is a palindrome or not.


#include <iostream>
using namespace std;

int main()
{
int num, reversed = 0, original, remainder;
cout << "Enter an integer: ";
cin >> num;
original = num;

while (num != 0)
{
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

if (original == reversed)
{
cout << original << " is a palindrome." << endl;
} else
{
cout << original << " is not a palindrome." << endl;
}

return 0;
}

7. Program to generate Fibonacci series.


#include <iostream>
using namespace std;

int main()
{
int n;
cout << "Enter the number of terms: ";
cin >> n;

if (n <= 0)
{
cout << "Please enter a positive integer." << endl;
return 1;
}

int first = 0, second = 1, next;


cout << "Fibonacci Series: ";

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


if (i == 0) {
cout << first << " ";
continue;
}
if (i == 1) {
cout << second << " ";
continue;
}
next = first + second;
first = second;
second = next;
cout << next << " ";
}

cout << endl;


return 0;
}

8. Program to compute sum of principle diagonal, lower diagonal and


upper diagonal elements of a matrix.
#include <iostream>
using namespace std;

int main()
{
int n;
cout << "Enter the size of the square matrix: ";
cin >> n;

int matrix[n][n];

cout << "Enter the elements of the matrix:\n";


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
int sumPrincipalDiagonal = 0;
int sumLowerDiagonal = 0;
int sumUpperDiagonal = 0;

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


for (int j = 0; j < n; j++) {
if (i == j) {
sumPrincipalDiagonal += matrix[i][j];
}
if (i > j) {
sumLowerDiagonal += matrix[i][j];
}
if (i < j) {
sumUpperDiagonal += matrix[i][j];
}
}
}

cout << "Sum of principal diagonal elements: " << sumPrincipalDiagonal << endl;
cout << "Sum of lower diagonal elements: " << sumLowerDiagonal << endl;
cout << "Sum of upper diagonal elements: " << sumUpperDiagonal << endl;

return 0;
}

9. Program to reverse a given string without using built-in function.


#include <iostream>
using namespace std;

int main()
{
string str;
cout << "Enter a string: ";
cin >> str;

int start = 0;
int end = str.length() - 1;
char temp;
while (start < end)

temp = str[start];
str[start] = str[end];
str[end] = temp;

start++;
end--;
}

cout << "Reversed string: " << str << endl;

return 0;
}
10.Program to demonstrate the usage of any five Math.h library functions.

#include <iostream>
#include <cmath> // Include the cmath library for math functions
using namespace std;

int main()
{

// 1. Square root function: sqrt() Returns square root of x.


int a;
cout<<"enter the number to calculate square root"<<"\n";
cin>>a;
cout << "Square root of " << a << " is " << sqrt(a) << endl;

// 2. Power function: pow() Returns the base raised to power exponent.


int num1,num2;
cout<<"enter the number to calculate power of number"<<"\n";
cin>>num1>>num2;
cout << num2 << " raised to the power of " << num1 << " is " << pow(num2, num1) << endl;

//3. absolute value of a given number : abs() Returns absolute value of x.


double num;
cout<<"enter the number to find absolute value"<<"\n";
cin>>num;
cout << " absalute value of " << num << " is " << abs(num) << endl;

//4. to find the maximum of two numbers fmax() Returns larger value of the arguments x and
y. If one number is NaN, other is returned.
double x,y;
cout<<"enter the 2 number to find maximun value"<<"\n";
cin>>x>>y;
cout << " maximum value of " << x << " and " << y << "is" << fmax(x,y) << endl;

//5. round of a number round() Returns integral value that is nearest to x.


double value;
cout<<"enter the number to find round of value"<<"\n";
cin>>value;
cout << " round of value of " << value << " is " << round(value) << endl;

// 6.to find the remainder Returns floating point remainder of numer/denom rounded to nearest
value. remainder();
double p,q;
cout<<"enter the number to find remainder of value"<<"\n";
cin>>p>>q;
cout << "remainder of " << p << " and " << q << " is " << remainder(p,q) << endl;
return 0;
}
PART B
1. Program to demonstrate call by value and call by reference.
#include <iostream>
using namespace std;

void callByValue(int a);


void callByReference(int &a);

int main()
{
int value = 10;
int reference = 20;

cout << "Before call by value: value = " << value << endl;
callByValue(value);
cout << "After call by value: value = " << value << endl;

cout << "Before call by reference: reference = " << reference << endl;
callByReference(reference);
cout << "After call by reference: reference = " << reference << endl;

return 0;
}
void callByValue(int a)
{
a = a * 2;
cout << "Inside callByValue function: a = " << a << endl;
}

void callByReference(int &a)


{
a = a * 2;
cout << "Inside callByReference function: a = " << a << endl;
}
2. Program to generate factorial of a given number using recursion.
#include <iostream>

int factorial(int n) {
if (n <= 1)
return 1;
else
return n * factorial(n - 1);
}

int main()
{
int number;
std::cout << "Enter a positive integer: ";
std::cin >> number;

if (number < 0) {
std::cout << "Factorial is not defined for negative numbers." << std::endl;
} else {
int result = factorial(number);
std::cout << "The factorial of " << number << " is " << result << "." << std::endl;
}

return 0;
}

3. Program to create a Class for representing student details with


appropriate member functions to accept and display the details.
#include <iostream>
#include <string>
using namespace std;

class Student
{
private:
string name;
int rollNumber;
float grade;

public:
void acceptDetails()
{
cout << "Enter student name: ";
getline(cin, name);
cout << "Enter roll number: ";
cin >> rollNumber;
cout << "Enter grade: ";
cin >> grade;
cin.ignore();
}

void displayDetails() const


{
cout << "Student Name: " << name << endl;
cout << "Roll Number: " << rollNumber << endl;
cout << "Grade: " << grade << endl;
}
};

int main()
{
Student student;
student.acceptDetails();
cout << "\nStudent Details:\n";
student.displayDetails();

return 0;
}

4. C++ Program to demonstrate function overloading.


#include <iostream>
using namespace std;

int add(int a, int b)


{
return a + b;
}

int add(int a, int b, int c)


{
return a + b + c;
}
double add(double a, double b)
{
return a + b;
}

double add(int a, double b)


{
return a + b;
}

int main() {
int int1 = 5, int2 = 10, int3 = 15;
double double1 = 5.5, double2 = 6.5;

cout << "Sum of two integers (5 + 10): " << add(int1, int2) << endl;
cout << "Sum of three integers (5 + 10 + 15): " << add(int1, int2, int3) << endl;
cout << "Sum of two doubles (5.5 + 6.5): " << add(double1, double2) << endl;
cout << "Sum of an integer and a double (5 + 6.5): " << add(int1, double2) << endl;

return 0;
}

5. Program to demonstrate friend function.

#include <iostream>
using namespace std;

class Box;

void displayVolume(const Box& b);

class Box
{
private:
double length;
double width;
double height;

public:
Box(double l, double w, double h) : length(l), width(w), height(h) {}
friend void displayVolume(const Box& b);
};

void displayVolume(const Box& b)


{
double volume = b.length * b.width * b.height;
cout << "Volume of the box: " << volume << endl;
}

int main() {
Box box(3.5, 2.0, 1.5);

displayVolume(box);

return 0;
}

6. Program for single inheritance.


#include <iostream>
using namespace std;

class Animal
{
public:
void eat()
{
cout << "This animal is eating." << endl;
}

void sleep()
{
cout << "This animal is sleeping." << endl;
}
};

class Dog : public Animal {


public:

void bark()
{
cout << "The dog is barking." << endl;
}
};

int main()
{

Dog myDog;

myDog.eat();
myDog.sleep();

myDog.bark();

return 0;
}

7. Program to demonstrate multilevel inheritance.


#include <iostream>
using namespace std;

class Animal
{
public:

void eat()
{
cout << "This animal is eating." << endl;
}

void sleep() {
cout << "This animal is sleeping." << endl;
}
};

class Mammal : public Animal {


public:
void walk()
{
cout << "This mammal is walking." << endl;
}
};
class Dog : public Mammal
{
public:

void bark()
{
cout << "The dog is barking." << endl;
}
};

int main() {

Dog myDog;

myDog.eat();
myDog.sleep();
myDog.walk();
myDog.bark();

return 0;
}

8. Program to demonstrate operator overloading.


#include <iostream>
using namespace std;

class Complex
{
private:
float real;
float imag;

public:

Complex(float r = 0, float i = 0) : real(r), imag(i) {}

Complex operator+(const Complex& other) const {

return Complex(real + other.real, imag + other.imag);


}

void display() const


{
cout << real << " + " << imag << "i" << endl;
}
};

int main() {

Complex c1(3.5, 2.5);


Complex c2(1.5, 4.5);

Complex result = c1 + c2;

cout << "Sum of ";


c1.display();
cout << "and ";
c2.display();
cout << "is ";
result.display();

return 0;
}

9. Program to demonstrate the usage of default and parameterized


constructors.
#include <iostream>
#include <cmath>
using namespace std;

class Circle
{
private:
double radius;

public:

Circle() : radius(1.0)
{
cout << "Default constructor called: Circle with radius " << radius << endl;
}

Circle(double r) : radius(r)
{
cout << "Parameterized constructor called: Circle with radius " << radius << endl;
}
double area() const
{
return M_PI * radius * radius;
}

void display() const {


cout << "Radius: " << radius << ", Area: " << area() << endl;
}
};

int main()
{

Circle circle1;
Circle circle2(5.0);

cout << "Details of circle1:" << endl;


circle1.display();

cout << "Details of circle2:" << endl;


circle2.display();

return 0;
}

10.Program to read and display the contents of a text file.

#include <iostream>
#include <fstream> // For file stream operations
#include <string> // For std::string

using namespace std;

int main() {
// Define the filename
const string filename = "example.txt";

// Create an input file stream object


ifstream file(filename);

// Check if the file was opened successfully


if (!file) {
cerr << "Error: Could not open the file " << filename << endl;
return 1; // Return an error code
}

// Read and display the contents of the file


string line;
while (getline(file, line)) {
cout << line << endl;
}

// Close the file


file.close();

return 0; // Success
}

You might also like