Parteek 240424
Parteek 240424
NAME-PARTEEK SINGHAL
240424
1. Program to Check if a Given Character is Vowel or Consonant:
#include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
return 0;
}
2. Program to Check Whether a Number is Positive, Negative, or Zero:
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num > 0) {
cout << "The number is positive." << endl;
} else if (num < 0) {
cout << "The number is negative." << endl;
} else {
cout << "The number is zero." << endl;
}
return 0;
}
3. Program to Check if the Input Character is an Alphabet, Digit, or Special
Character:
#include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
return 0;
}
4. Create a Calculator Using the Switch Statement:
#include <iostream>
using namespace std;
int main() {
int num1, num2;
char operation;
switch (operation) {
case '+':
cout << "Result: " << num1 + num2 << endl;
break;
case '-':
cout << "Result: " << num1 - num2 << endl;
break;
case '*':
cout << "Result: " << num1 * num2 << endl;
break;
case '/':
if (num2 != 0) {
cout << "Result: " << num1 / num2 << endl;
} else {
cout << "Division by zero is not allowed." << endl;
}
break;
default:
cout << "Invalid operator." << endl;
}
return 0;
}
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
7. Print the Following Patterns:
int main() {
char ch = 'A';
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
cout << ch << " ";
}
ch++;
cout << endl;
}
return 0;
}
int main() {
int n = 5;
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
int main() {
int n = 8;
for (int i = 0; i < n; i += 2) {
for (int j = 0; j < i; j++) {
cout << " ";
}
for (int j = i; j < n; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
(d) Number Pyramid:
#include <iostream>
using namespace std;
int main() {
int num = 1;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
cout << num << " ";
num++;
}
cout << endl;
}
return 0;
}
(e) Number Triangle:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
cout << i;
}
cout << endl;
}
return 0;
}
Let me know if you need further explanation or modifications!
1. Program to Find HCF/GCD:
#include <iostream>
using namespace std;
int main() {
int a, b, gcd;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "The HCF/GCD is: " << gcd << endl;
return 0;
}
2. Program to Find LCM:
#include <iostream>
using namespace std;
int main() {
int a, b, lcm;
cout << "The LCM is: " << lcm << endl;
return 0;
}
3. Program to Check Whether a Number is Palindrome or Not:
#include <iostream>
using namespace std;
int main() {
int num, reversed = 0, remainder, original;
original = num;
int main() {
int num, original, remainder, result = 0, n = 0;
original = num;
original = num;
if (result == num) {
cout << num << " is an Armstrong number." << endl;
} else {
cout << num << " is not an Armstrong number." << endl;
}
return 0;
}
return 0;
}
cout << "The sum of the series is: " << sum << endl;
return 0;
}
List Programs
1. Compute the sum of the first n terms of the following series:
S = 1 - 1/2! + 1/3! - 1/4! + ... + 1/n!
#include <iostream>
using namespace std;
cout << "The sum of the series for " << n << " terms is: " << sum << endl;
return 0;
}
2. Write a program to remove duplicates from an array.
#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
vector<int> arr(n);
cout << "Enter the elements: ";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
removeDuplicates(arr);
return 0;
}
3. Write a program that prints a table indicating the number of occurrences of each alphabet
in the text entered as command line arguments.
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
if (text.empty()) {
cout << "Please provide text as command line arguments." << endl;
} else {
countAlphabets(text);
}
return 0;
}
4. Write a menu-driven program to perform string manipulation (without using inbuilt string
functions):
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str1[100], str2[100];
int choice, position;
while (true) {
cout << "\nMenu:\n";
cout << "1. Show address of each character in string\n";
cout << "2. Concatenate two strings\n";
cout << "3. Compare two strings\n";
cout << "4. Calculate length of the string\n";
cout << "5. Convert lowercase to uppercase\n";
cout << "6. Reverse the string\n";
cout << "7. Insert string into another string at a specified position\n";
cout << "8. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(); // To ignore newline character after choice input
switch (choice) {
case 1:
cout << "Enter a string: ";
cin.getline(str1, 100);
showAddresses(str1);
break;
case 2:
cout << "Enter first string: ";
cin.getline(str1, 100);
cout << "Enter second string: ";
cin.getline(str2, 100);
concatenateStrings(str1, str2);
cout << "Concatenated string: " << str1 << endl;
break;
case 3:
cout << "Enter first string: ";
cin.getline(str1, 100);
cout << "Enter second string: ";
cin.getline(str2, 100);
int result = compareStrings(str1, str2);
if (result == 0) {
cout << "The strings are equal." << endl;
} else if (result < 0) {
cout << "The first string is less than the second string." << endl;
} else {
cout << "The first string is greater than the second string." << endl;
}
break;
case 4:
cout << "Enter a string: ";
cin.getline(str1, 100);
cout << "Length of the string: " << stringLength(str1) << endl;
break;
case 5:
cout << "Enter a string: ";
cin.getline(str1, 100);
toUppercase(str1);
cout << "String after conversion to uppercase: " << str1 << endl;
break;
case 6:
cout << "Enter a string: ";
cin.getline(str1, 100);
reverseString(str1);
cout << "Reversed string: " << str1 << endl;
break;
case 7:
cout << "Enter the first string: ";
cin.getline(str1, 100);
cout << "Enter the second string: ";
cin.getline(str2, 100);
cout << "Enter the position to insert: ";
cin >> position;
insertString(str1, str2, position);
cout << "String after insertion: " << str1 << endl;
break;
case 8:
cout << "Exiting program." << endl;
return 0;
default:
cout << "Invalid choice! Please try again." << endl;
}
}
}
5. Write a program to merge two ordered arrays to get a single ordered array.
#include <iostream>
#include <vector>
using namespace std;
return mergedArray;
}
int main() {
int n1, n2;
return 0;
}
6. Write a program to search a given element in a set of N numbers using Binary Search:
a. With Recursion
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
if (arr[mid] == key) {
return mid; // Element found
}
int main() {
int n, key;
vector<int> arr(n);
cout << "Enter the elements (sorted): ";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found!" << endl;
}
return 0;
}
b. Without Recursion
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
if (arr[mid] == key) {
return mid; // Element found
}
int main() {
int n, key;
cout << "Enter the number of elements: ";
cin >> n;
vector<int> arr(n);
cout << "Enter the elements (sorted): ";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found!" << endl;
}
return 0;
}
7. Write a program to calculate the GCD (Greatest Common Divisor) of two numbers:
a. With Recursion
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "GCD of " << a << " and " << b << " is: " << result << endl;
return 0;
}
b. Without Recursion
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "GCD of " << a << " and " << b << " is: " << result << endl;
return 0;
}
8. Create a Matrix class. Write a menu-driven program to perform the following matrix
operations (exceptions should be thrown by the functions if matrices passed to them are
incompatible and handled by the main() function):
a. Sum
b. Product
c. Transpose
#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;
class Matrix {
private:
vector<vector<int>> mat;
int rows, cols;
public:
Matrix(int r, int c) : rows(r), cols(c) {
mat.resize(r, vector<int>(c, 0));
}
void input() {
cout << "Enter elements for a " << rows << "x" << cols << " matrix:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> mat[i][j];
}
}
}
cout << "Enter the number of rows and columns for the first matrix: ";
cin >> rows1 >> cols1;
Matrix mat1(rows1, cols1);
mat1.input();
cout << "Enter the number of rows and columns for the second matrix: ";
cin >> rows2 >> cols2;
Matrix mat2(rows2, cols2);
mat2.input();
do {
cout << "\nMatrix Operations Menu\n";
cout << "1. Add Matrices\n";
cout << "2. Multiply Matrices\n";
cout << "3. Transpose of First Matrix\n";
cout << "4. Transpose of Second Matrix\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
try {
switch (choice) {
case 1:
// Addition
cout << "Matrix 1 + Matrix 2:\n";
mat1.add(mat2).display();
break;
case 2:
// Multiplication
cout << "Matrix 1 * Matrix 2:\n";
mat1.multiply(mat2).display();
break;
case 3:
// Transpose of matrix 1
cout << "Transpose of Matrix 1:\n";
mat1.transpose().display();
break;
case 4:
// Transpose of matrix 2
cout << "Transpose of Matrix 2:\n";
mat2.transpose().display();
break;
case 5:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice! Please try again.\n";
}
} catch (const invalid_argument& e) {
cout << "Error: " << e.what() << endl;
}
} while (choice != 5);
return 0;
}
9. Define a class Person having name as a data member. Inherit two classes Student and
Employee from Person. Student has additional attributes as course, marks, and year, while
Employee has department and salary. Write the display() method in all three classes to
display the corresponding attributes. Provide the necessary methods to show runtime
polymorphism.
#include <iostream>
#include <string>
using namespace std;
public:
Person(string n) : name(n) {}
public:
Employee(string n, string d, float s) : Person(n), department(d), salary(s) {}
// Cleaning up memory
delete person1;
delete person2;
return 0;
}
10. Create a Triangle class. Add exception handling statements to ensure the following
conditions:
• All sides are greater than 0.
• The sum of any two sides is greater than the third side. The class should also have
overloaded functions for calculating the area of a right-angled triangle as well as using
Heron's formula to calculate the area of any type of triangle.
#include <iostream>
#include <cmath>
#include <stdexcept>
using namespace std;
class Triangle {
private:
double side1, side2, side3;
public:
// Constructor to initialize sides
Triangle(double s1, double s2, double s3) {
if (s1 <= 0 || s2 <= 0 || s3 <= 0) {
throw invalid_argument("Sides must be greater than 0");
}
if (s1 + s2 <= s3 || s1 + s3 <= s2 || s2 + s3 <= s1) {
throw invalid_argument("The sum of any two sides must be greater than the third side");
}
side1 = s1;
side2 = s2;
side3 = s3;
}
void display() {
cout << "Sides of the Triangle: " << side1 << ", " << side2 << ", " << side3 << endl;
}
};
int main() {
double s1, s2, s3;
try {
// Creating an object of the Triangle class
Triangle t(s1, s2, s3);
t.display();
return 0;
}
11. Create a Student class containing fields for Roll No., Name, Class, Year, and Total Marks.
Write a program to store 5 objects of the Student class in a file. Retrieve these records from
the file and display them.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student {
private:
int rollNo;
string name;
string studentClass;
int year;
float totalMarks;
public:
// Constructor
Student(int r, string n, string c, int y, float m) :
rollNo(r), name(n), studentClass(c), year(y), totalMarks(m) {}
// Function to display student information
void display() const {
cout << "Roll No: " << rollNo << "\n"
<< "Name: " << name << "\n"
<< "Class: " << studentClass << "\n"
<< "Year: " << year << "\n"
<< "Total Marks: " << totalMarks << "\n\n";
}
// Function to write student data to a file
void writeToFile(ofstream& outFile) const {
outFile << rollNo << "\n" << name << "\n" << studentClass << "\n" << year << "\n" <<
totalMarks << "\n";
}
// Function to read student data from a file
void readFromFile(ifstream& inFile) {
inFile >> rollNo;
inFile.ignore(); // To ignore newline after the integer
getline(inFile, name);
getline(inFile, studentClass);
inFile >> year;
inFile >> totalMarks;
}
};
int main() {
// Creating 5 student objects
Student students[] = {
Student(1, "Alice", "A", 1, 85.5),
Student(2, "Bob", "B", 2, 90.0),
Student(3, "Charlie", "C", 3, 78.0),
Student(4, "David", "D", 1, 88.5),
Student(5, "Eva", "E", 2, 92.0)
};
// Writing student data to a file
ofstream outFile("students.txt");
if (!outFile) {
cout << "Error opening file for writing.\n";
return 1;
}
for (int i = 0; i < 5; i++) {
students[i].writeToFile(outFile);
}
outFile.close();
return 0;
}
12. Copy the contents of one text file to another file, after removing all whitespaces.
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
ofstream outFile(outputFile);
if (!outFile) {
cout << "Error opening output file.\n";
return;
}
char ch;
while (inFile.get(ch)) {
if (!isspace(ch)) { // Check if the character is not a whitespace
outFile.put(ch);
}
}
inFile.close();
outFile.close();
}
int main() {
string inputFile = "input.txt"; // Input file
string outputFile = "output.txt"; // Output file
return 0;
}