Experiment - 1 (a)
Objective:
Write a program to find the factorial of a number using
(i) While loop
Language Used – C++
Theory:
In C++, we can find the factorial of a number using a while loop by starting from 1 and
multiplying all numbers up to the given number. We keep repeating this multiplication until we
reach the number, and the final result is the factorial.
Code:
#include <iostream>
using namespace std;
class Factorial {
public:
int number;
unsigned long long result;
void getInput() {
cout << "Enter a positive integer: ";
cin >> number;
}
void calculateFactorial() {
result = 1;
int i = 1;
while (i <= number) {
result *= i;
i++;
}
}
void displayResult() {
cout << "Factorial of " << number << " is: " << result << endl;
}
};
int main() {
Factorial fact;
fact.getInput();
fact.calculateFactorial();
fact.displayResult();
return 0;
}
Output:
Experiment - 1 (b)
Objective:
Write a program to find the factorial of a number using
(ii) Do while loop
Language Used – C++
Theory:
In C++, we can find the factorial of a number using a do while by starting with 1 and
multiplying numbers one by one until we reach the given number. The do…while loop makes
sure the steps are done at least once, even if the number is small. For 0, the factorial is taken as
1.
Code:
#include <iostream>
using namespace std;
class Factorial {
public:
int number;
unsigned long long result;
void getInput() {
cout << "Enter a positive integer: ";
cin >> number;
}
void calculateFactorial() {
result = 1;
int i = 1;
if (number == 0) {
result = 1;
return;
}
do {
result *= i;
i++;
} while (i <= number);
}
void displayResult() {
cout << "Factorial of " << number << " is: " << result << endl;
}
};
int main() {
Factorial fact;
fact.getInput();
fact.calculateFactorial();
fact.displayResult();
return 0;
}
Output:
Experiment - 1 (c)
Objective:
Write a program to find the factorial of a number using
(iii) For loop
Language Used – C++
Theory:
In C++, the factorial of a number can be found using a for loop by starting from 1 and
multiplying all numbers up to the given number. The loop repeats this multiplication step-by-
step until it reaches the number, and the final product is the factorial.
Code:
#include <iostream>
using namespace std;
class Factorial {
public:
int number;
unsigned long long result;
void getInput() {
cout << "Enter a positive integer: ";
cin >> number;
}
void calculateFactorial() {
result = 1;
for (int i = 1; i <= number; ++i) {
result *= i;
}
}
void displayResult() {
cout << "Factorial of " << number << " is: " << result << endl;
}
};
int main() {
Factorial fact;
fact.getInput();
fact.calculateFactorial();
fact.displayResult();
return 0;
}
Output:
Experiment - 2
Objective:
Write a program to check whether the given number is leap year.
Language Used – C++
Theory:
A year is a leap year if it is divisible by 400.
Code:
#include <iostream>
using namespace std;
class Year {
public:
int y;
void input() {
cout<<"Enter the year you want to check- ";
cin >> y;
}
void check() {
if (y % 400 == 0){
cout << y << " is a leap year" << endl;
}
else{
cout << y << " is not a leap year" << endl;
}
}
};
int main() {
Year obj;
obj.input();
obj.check();
return 0;
}
Output:
Experiment - 3
Objective:
Write a program to find whether the given triangle is scalene, isosceles or equilateral
triangle.
Language Used – C++
Theory:
A triangle is Equilateral if all three sides are equal, Isosceles if exactly two sides are equal, and
Scalene if all sides are different.
Code:
#include <iostream>
using namespace std;
class Triangle {
public:
int a, b, c;
void input() {
cout << "Enter side a: ";
cin >> a;
cout << "Enter side b: ";
cin >> b;
cout << "Enter side c: ";
cin >> c;
}
void check() {
if (a == b && b == c)
cout << "It is a Equilateral triangle" << endl;
else if (a == b || b == c || a == c)
cout << "It is a Isosceles triangle" << endl;
else
cout << "It is a Scalene triangle" << endl;
}
};
int main() {
Triangle t;
t.input();
t.check();
return 0;
}
Output:
Experiment - 4
Objective:
Write a program to print Fibonacci series
Language Used – C++
Theory:
The Fibonacci series starts with 0 and 1, and each next number is the sum of the previous two
numbers.
Code:
#include <iostream>
using namespace std;
class Fib {
public:
int n, a, b, c;
void input() {
cout << "Enter number of terms: ";
cin >> n;
}
void printSeries() {
a = 0;
b = 1;
cout << "Fibonacci Series: ";
if (n >= 1) cout << a << " ";
if (n >= 2) cout << b << " ";
for (int i = 3; i <= n; i++) {
c = a + b;
cout << c << " ";
a = b;
b = c;
}
}
};
int main() {
Fib f;
f.input();
f.printSeries();
return 0;
}
Output:
Experiment - 5
Objective:
Write a program to create a 2D array which contains names and marks of students and
use following functions
(i) getmarks()
(ii) displaymarks()
(iii) displaypercentage()
Language Used – C++
Theory:
This program stores and processes students’ marks using a class. It keeps the names of students
in a 2D character array and their marks for different subjects in a 2D integer array. First, the
getmarks() function takes input for the number of students, number of subjects, each student’s
name, and their marks in all subjects. Then, the displaymarks() function prints each student’s
marks in a tabular format. Finally, the displaypercentage() function calculates each student’s
percentage by summing their marks and dividing by the number of subjects, and displays it
Code:
#include <iostream>
#include <cstring>
using namespace std;
class Students {
public:
char names[10][50];
int marks[10][50];
int n, sub;
void getmarks() {
cout << "Enter number of students: ";
cin >> n;
cout << "Enter number of subjects: ";
cin >> sub;
for (int i = 0; i < n; i++) {
cout << "Enter name of student " << i + 1 << ": ";
cin >> names[i];
cout << "Enter marks in " << sub << " subjects: ";
for (int j = 0; j < sub; j++) {
cin >> marks[i][j];
}
}
}
void displaymarks() {
cout << "\nName\t";
for (int i = 0; i < sub; i++) {
cout << "S" << i + 1 << "\t";
}
cout << "\n";
for (int i = 0; i < n; i++) {
cout << names[i] << "\t";
for (int j = 0; j < sub; j++) {
cout << marks[i][j] << "\t";
}
cout << endl;
}
}
void displaypercentage() {
cout << "\nName\tPercentage\t\n";
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < sub; j++) {
sum = sum + marks[i][j];
}
float percentage = sum / (float)sub;
cout << names[i] << "\t" << percentage << "%" << endl;
}
}
};
int main() {
Students s;
s.getmarks();
s.displaymarks();
s.displaypercentage();
return 0;
}
Output:
Experiment - 6
Objective:
Write a program to check number palindrome and string palindrome.
Language Used – C++
Theory:
This program checks whether a number or a string is a palindrome using two different classes.
One class takes a number, reverses its digits, and compares it with the original number, while the
other class takes a string and compares characters from both ends to see if they match. The user
chooses which type to check, and the program keeps running in a loop until they choose to exit.
Code:
#include <iostream>
#include <cstring>
using namespace std;
class NumberPalindrome {
public:
int number, n, digit, reverse, original;
void get_num() {
cout << "Enter the number: ";
cin >> number;
}
bool check_palindrome() {
n = number;
reverse = 0;
original = number;
while (n > 0) {
digit = n % 10;
reverse = (reverse * 10) + digit;
n = n / 10;
}
return (original == reverse);
}
void result() {
if (check_palindrome())
cout << "It is a palindrome" << endl;
else
cout << "It is not a palindrome" << endl;
}
};
class StringPalindrome {
public:
char str[100];
void get_string() {
cout << "Enter the string: ";
cin >> str;
}
bool check_palindrome() {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
return false;
}
}
return true;
}
void result() {
if (check_palindrome())
cout << "It is a palindrome" << endl;
else
cout << "It is not a palindrome" << endl;
}
};
int main() {
NumberPalindrome a;
StringPalindrome b;
int choice;
char ch;
do {
cout << "\nEnter choice:\n1. Check integer palindrome\n2. Check string palindrome\
nChoice: ";
cin >> choice;
if (choice == 1) {
a.get_num();
a.result();
}
else if (choice == 2) {
b.get_string();
b.result();
}
else {
cout << "Invalid choice!" << endl;
}
cout << "\nDo you want to check again? (y/n): ";
cin >> ch;
} while (ch== 'y' || ch == 'Y');
return 0;
}
Output:
Experiment - 7
Objective:
Write a program to show the effect of call by value and call by reference in functions.
Language Used – C++
Theory:
This program demonstrates the difference between call by value and call by reference in C++
using a class. In call by value, a copy of the variables is passed to the function, so swapping
inside the function does not change the original values. In call by reference, the actual variables
are passed, so swapping inside the function also changes the original values in main().
Code:
#include <iostream>
using namespace std;
class Swap {
public:
void byVal(int x, int y) {
int t = x;
x = y;
y = t;
cout << "\nCall by Value";
cout << "\nValue of a = " << x << "\nValue of b = " << y;
cout << "\nAddress of a = " << &x << "\nAddress of b = " << &y;
cout<<"\n";
}
void byRef(int &x, int &y) {
int t = x;
x = y;
y = t;
cout << "\nCall by Reference";
cout << "\nValue of a = " << x << "\nValue of b = " << y;
cout << "\nAddress of a = " << &x << "\nAddress of b = " << &y;
}
};
int main() {
Swap s;
int a, b;
cout << "Enter two numbers a and b: ";
cin >> a >> b;
cout << "\nOriginal values and addresses:";
cout << "\nValue of a = " << a << "\nValue of b = " << b;
cout << "\nAddress of a = " << &a << "\nAddress of b = " << &b;
cout<<"\n";
s.byVal(a, b);
s.byRef(a, b);
return 0;
}
Output:
Experiment – 8
Objective:
8. Write a program to perform following operations on matrix using functions and
switch case:
(a) Addition (b) subtraction (c) multiplication (d) transpose.
Language Used – C++
Theory:
This program performs basic matrix operations like addition, subtraction, multiplication,
and transpose using a class. It first takes input for two matrices along with their dimensions.
The program checks if addition or subtraction is possible by verifying if both matrices have
the same dimensions, and multiplication is possible if the number of columns in the first
matrix matches the number of rows in the second. For transpose, the program lets the user
choose which matrix's transpose to display. Transpose means swapping rows and columns of
the matrix.The program using switch case to perform different operations.
Code:
#include <iostream>
using namespace std;
class matrix {
public:
int a[10][10], b[10][10], result[10][10];
int r1, c1, r2, c2;
void inputMatrices() {
cout << "Enter number of rows for Matrix 1: ";
cin >> r1;
cout << "Enter number of columns for Matrix 1: ";
cin >> c1;
cout << "Enter number of rows for Matrix 2: ";
cin >> r2;
cout << "Enter number of columns for Matrix 2: ";
cin >> c2;
cout << "\nEnter elements for Matrix 1 (" << r1 << "x" << c1 << "):\n";
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
cin >> a[i][j];
}
}
cout << "\nEnter elements for Matrix 2 (" << r2 << "x" << c2 << "):\n";
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
cin >> b[i][j];
}
}
}
void add() {
if (r1 == r2 && c1 == c2) {
cout << "\nADDITION POSSIBLE\nResult:\n";
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
result[i][j] = a[i][j] + b[i][j];
cout << "| " << result[i][j] << " |";
}
cout << "\n";
}
} else {
cout << "\nADDITION NOT POSSIBLE\n";
}
}
void subtract() {
if (r1 == r2 && c1 == c2) {
cout << "\nSUBTRACTION POSSIBLE\nResult:\n";
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
result[i][j] = a[i][j] - b[i][j];
cout << "| " << result[i][j] << " |";
}
cout << "\n";
}
} else {
cout << "\nSUBTRACTION NOT POSSIBLE\n";
}
}
void multiply() {
if (c1 == r2) {
cout << "\nMULTIPLICATION POSSIBLE\nResult:\n";
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
result[i][j] = 0;
for (int k = 0; k < c1; k++) {
result[i][j] += a[i][k] * b[k][j];
}
cout << "| " << result[i][j] << " |";
}
cout << "\n";
}
} else {
cout << "\nMULTIPLICATION NOT POSSIBLE\n";
}
}
void transpose() {
int choice;
cout << "\nTranspose of:\n1. Matrix 1\n2. Matrix 2\nEnter your choice: ";
cin >> choice;
if (choice == 1) {
cout << "\nTRANSPOSE OF MATRIX 1 :\n";
for (int i = 0; i < c1; i++) {
for (int j = 0; j < r1; j++) {
cout << "| " << a[j][i] << " |";
}
cout << "\n";
}
}
else if (choice == 2) {
cout << "\nTRANSPOSE OF MATRIX :\n";
for (int i = 0; i < c2; i++) {
for (int j = 0; j < r2; j++) {
cout << "| " << b[j][i] << " |";
}
cout << "\n";
}
}
else {
cout << "Invalid choice for transpose.\n";
}
}
};
int main() {
matrix m;
int ch;
char again;
m.inputMatrices();
do {
cout << "\nChoose an operation:\n";
cout << "1. Add matrices\n";
cout << "2. Subtract matrices\n";
cout << "3. Multiply matrices\n";
cout << "4. Transpose of a Matrix\n";
cout << "Enter your choice: ";
cin >> ch;
switch (ch) {
case 1: m.add(); break;
case 2: m.subtract(); break;
case 3: m.multiply(); break;
case 4: m.transpose(); break;
default: cout << "Invalid choice.\n";
}
cout << "\nDo you want to perform another operation? (y/n): ";
cin >> again;
} while (again == 'y' || again == 'Y');
return 0;
Output