C++Lab Notes
C++Lab Notes
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;
}
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;
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>
return 0;
}
Or
#include <iostream>
using namespace std;
int main()
{
int real1, real2, img1, img2, realsum, imgsum, realmul, imgmul;
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;
}
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;
}
int main()
{
int number;
return 0;
}
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;
}
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 main()
{
int n;
cout << "Enter the size of the square matrix: ";
cin >> n;
int matrix[n][n];
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;
}
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--;
}
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()
{
//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;
// 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;
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;
}
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;
}
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();
}
int main()
{
Student student;
student.acceptDetails();
cout << "\nStudent Details:\n";
student.displayDetails();
return 0;
}
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;
}
#include <iostream>
using namespace std;
class Box;
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);
};
int main() {
Box box(3.5, 2.0, 1.5);
displayVolume(box);
return 0;
}
class Animal
{
public:
void eat()
{
cout << "This animal is eating." << endl;
}
void sleep()
{
cout << "This animal is sleeping." << endl;
}
};
void bark()
{
cout << "The dog is barking." << endl;
}
};
int main()
{
Dog myDog;
myDog.eat();
myDog.sleep();
myDog.bark();
return 0;
}
class Animal
{
public:
void eat()
{
cout << "This animal is eating." << endl;
}
void sleep() {
cout << "This animal is sleeping." << endl;
}
};
void bark()
{
cout << "The dog is barking." << endl;
}
};
int main() {
Dog myDog;
myDog.eat();
myDog.sleep();
myDog.walk();
myDog.bark();
return 0;
}
class Complex
{
private:
float real;
float imag;
public:
int main() {
return 0;
}
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;
}
int main()
{
Circle circle1;
Circle circle2(5.0);
return 0;
}
#include <iostream>
#include <fstream> // For file stream operations
#include <string> // For std::string
int main() {
// Define the filename
const string filename = "example.txt";
return 0; // Success
}