PROGRAMMING WITH C++ Program File Yash
PROGRAMMING WITH C++ Program File Yash
ASSIGNMENT
BY
YASH MAHESHWARI
240443
1. Program to Check if a Given Character is Vowel or Consonant:
#include <iostream>
int main() {
char ch;
} else {
return 0;
}
2. Program to Check Whether a Number is Positive, Negative, or Zero:
#include <iostream>
int main() {
int num;
if (num > 0) {
} else {
return 0;
}
3. Program to Check if the Input Character is an Alphabet, Digit, or Special Character:
#include <iostream>
int main() {
char ch;
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
} else {
return 0;
}
4. Create a Calculator Using the Switch Statement:
#include <iostream>
int main() {
char operation;
switch (operation) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/'
if (num2 != 0) {
cout << "Result: " << num1 / num2 << endl;
} else {
break;
default:
return 0;
}
5. Program to Print a Half-Pyramid Using Numbers:
#include <iostream>
int main() {
int n = 5;
return 0;
}
6. Program to Print a Half-Pyramid Using *:
#include <iostream>
int main() {
int n = 5;
return 0;
}
7. Print the Following Patterns:
#include <iostream>
int main() {
char ch = 'A';
ch++;
return 0;
#include <iostream>
int main() {
int n = 5;
return 0;
#include <iostream>
int main() {
int n = 8;
return 0;
int main() {
int num = 1;
num++;
return 0;
#include <iostream>
int main() {
cout << i;
return 0;
}
#include <iostream>
int main() {
int a, b, gcd;
while (b != 0) {
gcd = b;
b = a % b;
a = gcd;
cout << "The HCF/GCD is: " << gcd << endl;
return 0;
}
2. Program to Find LCM:
#include <iostream>
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>
int main() {
original = num;
while (num != 0) {
num /= 10;
if (original == reversed) {
} else {
return 0;
}
#include <iostream>
#include <cmath>
int main() {
original = num;
while (original != 0) {
original /= 10;
++n;
original = num;
while (original != 0) {
original /= 10;
}
if (result == num) {
} else {
cout << num << " is not an Armstrong number." << endl;
return 0;
#include <iostream>
int main() {
int n, sum = 0;
cin >> n;
sum = n * (n + 1) / 2;
cout << "The sum of natural numbers up to " << n << " is: " << sum << endl;
return 0;
#include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cin >> n;
if (i % 2 == 0) {
} else {
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:
#include <iostream>
double fact = 1;
fact *= i;
return fact;
int n;
if (argc > 1) {
n = atoi(argv[1]);
} else {
cin >> n;
if (sign) {
sum += 1 / factorial(i);
} else {
sum -= 1 / factorial(i);
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>
unordered_set<int> uniqueElements;
int index = 0;
if (uniqueElements.find(arr[i]) == uniqueElements.end()) {
arr[index++] = arr[i];
uniqueElements.insert(arr[i]);
int main() {
int n;
cin >> n;
vector<int> arr(n);
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>
if (isalpha(ch)) {
freq[ch - 'a']++;
if (freq[i] > 0) {
cout << char(i + 'a') << " | " << freq[i] << endl;
text += argv[i];
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>
while (*str) {
cout << "Address of character " << *str << ": " << (void*)str << endl;
str++;
while (*str1) {
str1++;
while (*str2) {
*str1 = *str2;
str1++;
str2++;
str1++;
str2++;
int length = 0;
while (*str++) {
length++;
return length;
while (*str) {
str++;
str[length - 1 - i] = temp;
str1[position + i] = str2[i];
int main() {
while (true) {
cout << "\nMenu:\n";
cout << "7. Insert string into another string at a specified position\n";
switch (choice) {
case 1:
cin.getline(str1, 100);
showAddresses(str1);
break;
case 2:
cin.getline(str1, 100);
cin.getline(str2, 100);
concatenateStrings(str1, str2);
break;
case 3:
cin.getline(str1, 100);
cin.getline(str2, 100);
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:
cin.getline(str1, 100);
cout << "Length of the string: " << stringLength(str1) << endl;
break;
case 5:
cin.getline(str1, 100);
toUppercase(str1);
cout << "String after conversion to uppercase: " << str1 << endl;
break;
case 6:
reverseString(str1);
break;
case 7:
cin.getline(str1, 100);
cin.getline(str2, 100);
cout << "String after insertion: " << str1 << endl;
break;
case 8:
return 0;
default:
5. Write a program to merge two ordered arrays to get a single ordered array.
#include <iostream>
#include <vector>
vector<int> mergedArray;
int i = 0, j = 0;
mergedArray.push_back(arr1[i]);
i++;
} else {
mergedArray.push_back(arr2[j]);
j++;
mergedArray.push_back(arr1[i]);
i++;
mergedArray.push_back(arr2[j]);
j++;
}
return mergedArray;
int main() {
cout << "Enter the number of elements in the first array: ";
vector<int> arr1(n1);
cout << "Enter the elements of the first array (sorted): ";
cout << "Enter the number of elements in the second array: ";
vector<int> arr2(n2);
cout << "Enter the elements of the second array (sorted): ";
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>
int binarySearchRecursive(const vector<int>& arr, int low, int high, int key) {
if (arr[mid] == key) {
}
if (arr[mid] > key) {
} else {
int main() {
int n, key;
cin >> n;
vector<int> arr(n);
if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
return 0;
b. Without Recursion
#include <iostream>
#include <vector>
#include <algorithm>
if (arr[mid] == key) {
} else {
low = mid + 1; // Search right half
int main() {
int n, key;
cin >> n;
vector<int> arr(n);
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>
if (b == 0) {
int main() {
int a, b;
cout << "GCD of " << a << " and " << b << " is: " << result << endl;
return 0;
b. Without Recursion
#include <iostream>
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
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>
class Matrix {
private:
vector<vector<int>> mat;
public:
void input() {
cout << "Enter elements for a " << rows << "x" << cols << " matrix:\n";
return result;
}
Matrix multiply(const Matrix& other) {
if (cols != other.rows) {
return result;
Matrix transpose() {
result.mat[j][i] = mat[i][j];
return result;
};
int main() {
int choice, rows1, cols1, rows2, cols2;
cout << "Enter the number of rows and columns for the first matrix: ";
mat1.input();
cout << "Enter the number of rows and columns for the second matrix: ";
mat2.input();
do {
try {
switch (choice) {
case 1:
// Addition
break;
case 2:
// Multiplication
mat1.multiply(mat2).display();
break;
case 3:
// Transpose of matrix 1
mat1.transpose().display();
break;
case 4:
// Transpose of matrix 2
mat2.transpose().display();
break;
case 5:
break;
default:
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>
class Person {
protected:
string name;
public:
Person(string n) : name(n) {}
};
private:
string course;
float marks;
int year;
public:
Person::display();
};
private:
string department;
float salary;
public:
Person::display();
};
int main() {
person1->display();
person2->display();
// Cleaning up memory
delete person1;
delete person2;
return 0;
10. Create a Triangle class. Add exception handling statements to ensure the following conditions:
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:
public:
throw invalid_argument("The sum of any two sides must be greater than the third side");
side1 = s1;
side2 = s2;
side3 = s3;
double areaRightAngled() {
void display() {
cout << "Sides of the Triangle: " << side1 << ", " << side2 << ", " << side3 << endl;
};
int main() {
try {
t.display();
if (s1 * s1 + s2 * s2 == s3 * s3 || s1 * s1 + s3 * s3 == s2 * s2 || s2 * s2 + s3 * s3 == s1 * s1) {
cout << "Area of the Right-Angled Triangle: " << t.areaRightAngled() << endl;
} else {
cout << "Area using Heron's formula: " << t.areaHeron() << endl;
}
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>
class Student {
private:
int rollNo;
string name;
string studentClass;
int year;
float totalMarks;
public:
// Constructor
outFile << rollNo << "\n" << name << "\n" << studentClass << "\n" << year << "\n" << totalMarks
<< "\n";
getline(inFile, name);
getline(inFile, studentClass);
};
int main() {
Student students[] = {
};
ofstream outFile("students.txt");
if (!outFile) {
return 1;
students[i].writeToFile(outFile);
outFile.close();
ifstream inFile("students.txt");
if (!inFile) {
return 1;
while (inFile) {
temp.readFromFile(inFile);
if (inFile) {
temp.display();
}
}
inFile.close();
return 0;
}
12. Copy the contents of one text file to another file, after removing all whitespaces.
#include <iostream>
#include <fstream>
#include <cctype>
ifstream inFile(inputFile);
if (!inFile) {
return;
ofstream outFile(outputFile);
if (!outFile) {
return;
char ch;
while (inFile.get(ch)) {
outFile.put(ch);
inFile.close();
outFile.close();
int main() {
removeWhitespaceAndCopy(inputFile, outputFile);
return 0;