Computer Assignment
Name - Peeyush Verma
Roll No. - 241043
Subject - Programming using C++
Teacher’s signature
Q1. #include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
cout << ch << " is a vowel." << endl;
} else {
cout << ch << " is a consonant." << endl;
}
return 0;
}
Q2. #include <iostream>
using namespace std;
int main() {
oat num;
cout << "Enter a number: ";
cin >> num;
if (num > 0) {
cout << num << " is positive." << endl;
} else if (num < 0) {
cout << num << " is negative." << endl;
} else {
cout << "The number is zero." << endl;
}
return 0;
}
Q3. #include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
if (isalpha(ch)) {
cout << ch << " is an alphabet." << endl;
} else if (isdigit(ch)) {
cout << ch << " is a digit." << endl;
} else {
cout << ch << " is a special character." << endl;
}
return 0;
}
Q4. #include <iostream>
using namespace std;
int main() {
fl
double num1, num2;
int choice;
cout << "Enter rst number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << "Select operation: \n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n";
cin >> choice;
switch(choice) {
case 1:
cout << "Result: " << num1 + num2 << endl;
break;
case 2:
cout << "Result: " << num1 - num2 << endl;
break;
case 3:
cout << "Result: " << num1 * num2 << endl;
break;
case 4:
if(num2 != 0)
cout << "Result: " << num1 / num2 << endl;
else
cout << "Error! Division by zero." << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
return 0;
}
Q5. #include <iostream>
using namespace std;
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;
}
Q6. #include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
fi
}
Q7.
a) #include <iostream>
using namespace std;
int main() {
int n = 5;
char letter = 'A';
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << letter << " ";
}
cout << endl;
letter++;
}
return 0;
}
b) #include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
C) #include <iostream>
using namespace std;
int main() {
int n = 8;
for (int i = 0; i < n; i += 2) {
for (int j = 0; j < i; j++) {
cout << " ";
}
for (int j = 0; j < (n - i); j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
D) #include <iostream>
using namespace std;
int main() {
int n = 4, num = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << num++ << " ";
}
cout << endl;
}
return 0;
}
E) #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;
}
Q8. #include <iostream>
using namespace std;
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "GCD of " << num1 << " and " << num2 << " is " << gcd(num1, num2) << endl;
return 0;
}
Q9. #include <iostream>
using namespace std;
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "LCM of " << num1 << " and " << num2 << " is " << lcm(num1, num2) << endl;
return 0;
}
Q10. #include <iostream>
using namespace std;
int main() {
int num, reversed = 0, remainder, original;
cout << "Enter a number: ";
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;
}
Q11. #include <iostream>
#include <cmath>
using namespace std;
int main() {
int num, original, remainder, sum = 0, digits = 0;
cout << "Enter a number: ";
cin >> num;
original = num;
while (original != 0) {
original /= 10;
digits++;
}
original = num;
while (original != 0) {
remainder = original % 10;
sum += pow(remainder, digits);
original /= 10;
}
if (sum == num)
cout << num << " is an Armstrong number." << endl;
else
cout << num << " is not an Armstrong number." << endl;
return 0;
}
Q12. #include <iostream>
using namespace std;
int main() {
int n, sum;
cout << "Enter a number: ";
cin >> n;
sum = n * (n + 1) / 2;
cout << "The sum of rst " << n << " natural numbers is " << sum << endl;
return 0;
}
Q13. #include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the value of n: ";
cin >> n;
int sum = 0;
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) {
sum -= i;
} else {
sum += i;
}
}
cout << "The sum of the series is " << sum << endl;
return 0;
}
Q14. #include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of terms (n): ";
cin >> n;
double sum = 0;
for (int i = 1; i <= n; i++) {
if (i % 2 != 0) {
sum += 1.0 / i;
} else {
sum -= 1.0 / i;
}
}
cout << "The sum of the series is " << sum << endl;
return 0;
}
Q15. #include <iostream>
using namespace std;
void mergeOrderedArrays(int arr1[], int size1, int arr2[], int size2, int merged[], int& mergedSize) {
int i = 0, j = 0, k = 0;
while (i < size1 && j < size2) {
if (arr1[i] < arr2[j]) {
merged[k++] = arr1[i++];
} else {
fi
merged[k++] = arr2[j++];
}
}
while (i < size1) {
merged[k++] = arr1[i++];
}
while (j < size2) {
merged[k++] = arr2[j++];
}
mergedSize = k;}
int main() {
int arr1[100], arr2[100], merged[200];
int size1, size2, mergedSize;
cout << "Enter the size of the rst array: ";
cin >> size1;
cout << "Enter " << size1 << " sorted elements for the rst array:\n";
for (int i = 0; i < size1; i++) {
cin >> arr1[i];
}
cout << "Enter the size of the second array: ";
cin >> size2;
cout << "Enter " << size2 << " sorted elements for the second array:\n";
for (int i = 0; i < size2; i++) {
cin >> arr2[i];
}
mergeOrderedArrays(arr1, size1, arr2, size2, merged, mergedSize);
cout << "Merged ordered array:\n";
for (int i = 0; i < mergedSize; i++) {
cout << merged[i] << " ";
}
cout << endl;
return 0;
}
Q16. #include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
int main(int argc, char* argv[]) {
if (argc < 2) {
cout << "Please provide a text as a command line argument." << endl;
return 1;
}
char* text = argv[1];
int freq[26] = {0};
for (int i = 0; text[i] != '\0'; i++) {
if (isalpha(text[i])) {
fi
fi
text[i] = tolower(text[i]);
freq[text[i] - 'a']++;
}
}
cout << "Occurrences of each letter: " << endl;
for (int i = 0; i < 26; i++) {
if (freq[i] > 0) {
cout << (char)(i + 'a') << ": " << freq[i] << endl;
}
}
return 0;
}
Q17. #include <iostream>
#include <cstring>
using namespace std;
void showAddress(char* str) {
for (int i = 0; str[i] != '\0'; i++) {
cout << "Address of " << str[i] << ": " << (void*)&str[i] << endl;
}
}
void concatenateStrings(char* str1, char* str2) {
strcat(str1, str2);
cout << "Concatenated string: " << str1 << endl;
}
int compareStrings(char* str1, char* str2) {
return strcmp(str1, str2);
}
int stringLength(char* str) {
int len = 0;
while (str[len] != '\0') len++;
return len;
}
void convertToUpper(char* str) {
for (int i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]);
}
cout << "String in uppercase: " << str << endl;
}
void reverseString(char* str) {
int len = stringLength(str);
for (int i = 0; i < len / 2; i++) {
swap(str[i], str[len - i - 1]);
}
cout << "Reversed string: " << str << endl;
}
void insertString(char* str, char* toInsert, int pos) {
int len = stringLength(str);
for (int i = len; i >= pos; i--) {
str[i + strlen(toInsert)] = str[i];
}
for (int i = 0; toInsert[i] != '\0'; i++) {
str[pos + i] = toInsert[i];
}
cout << "String after insertion: " << str << endl;
}
int main() {
char str1[100], str2[100];
int choice, pos;
cout << "Enter rst string: ";
cin.getline(str1, 100);
cout << "Enter second string: ";
cin.getline(str2, 100);
cout << "Menu: \n";
cout << "1. Show address of each character\n";
cout << "2. Concatenate two strings\n";
cout << "3. Compare two strings\n";
cout << "4. Calculate length of string\n";
cout << "5. Convert to uppercase\n";
cout << "6. Reverse string\n";
cout << "7. Insert string at position\n";
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(); // To ignore the trailing newline character
switch(choice) {
case 1:
showAddress(str1);
break;
case 2:
concatenateStrings(str1, str2);
break;
case 3:
cout << "Comparison result: " << compareStrings(str1, str2) << endl;
break;
case 4:
cout << "Length of rst string: " << stringLength(str1) << endl;
break;
case 5:
convertToUpper(str1);
break;
case 6:
reverseString(str1);
break;
case 7:
cout << "Enter position to insert: ";
cin >> pos;
insertString(str1, str2, pos);
break;
default:
cout << "Invalid choice!" << endl;
}
return 0;
}
Q18. #include <iostream>
#include <vector>
fi
fi
#include <algorithm>
using namespace std;
void mergeArrays(int arr1[], int arr2[], int n1, int n2) {
vector<int> merged;
int i = 0, j = 0;
while (i < n1 && j < n2) {
if (arr1[i] < arr2[j]) {
merged.push_back(arr1[i++]);
} else {
merged.push_back(arr2[j++]);
}
}
while (i < n1) merged.push_back(arr1[i++]);
while (j < n2) merged.push_back(arr2[j++]);
cout << "Merged array: ";
for (int k = 0; k < merged.size(); k++) {
cout << merged[k] << " ";
}
cout << endl;
}
int main() {
int arr1[] = {1, 3, 5, 7};
int arr2[] = {2, 4, 6, 8};
mergeArrays(arr1, arr2, 4, 4);
return 0;
}
Q19. A) #include <iostream>
using namespace std;
int binarySearch(int arr[], int low, int high, int target) {
if (low > high) return -1; // Element not found
int mid = low + (high - low) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] > target) return binarySearch(arr, low, mid - 1, target);
else return binarySearch(arr, mid + 1, high, target);
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
int target;
cout << "Enter the element to search: ";
cin >> target;
int result = binarySearch(arr, 0, n - 1, target);
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found!" << endl;
}
return 0;
}
B) #include <iostream>
using namespace std;
int binarySearch(int arr[], int n, int target) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] > target) high = mid - 1;
else low = mid + 1;
}
return -1; // Element not found
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
int target;
cout << "Enter the element to search: ";
cin >> target;
int result = binarySearch(arr, n, target);
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found!" << endl;
}
return 0;
}
Q20. A) #include <iostream>
using namespace std;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "GCD of " << num1 << " and " << num2 << " is " << gcd(num1, num2) << endl;
return 0;
}
B) #include <iostream>
using namespace std;
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "GCD of " << num1 << " and " << num2 << " is " << gcd(num1, num2) << endl;
return 0;
}
Q21. #include <iostream>
#include <stdexcept>
using namespace std;
class Matrix {
public:
int rows, cols;
int** data;
Matrix(int r, int c) {
rows = r;
cols = c;
data = new int*[r];
for (int i = 0; i < r; i++) {
data[i] = new int[c];
}
}
void inputMatrix() {
cout << "Enter elements for the matrix (" << rows << "x" << cols << "):\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> data[i][j];
}
}
}
void displayMatrix() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << data[i][j] << " ";
}
cout << endl;
}
}
Matrix sum(const Matrix& other) {
if (rows != other.rows || cols != other.cols) {
throw invalid_argument("Matrices are incompatible for addition.");
}
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.data[i][j] = data[i][j] + other.data[i][j];
}
}
return result;
}
Matrix product(const Matrix& other) {
if (cols != other.rows) {
throw invalid_argument("Matrices are incompatible for multiplication.");
}
Matrix result(rows, other.cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < other.cols; j++) {
result.data[i][j] = 0;
for (int k = 0; k < cols; k++) {
result.data[i][j] += data[i][k] * other.data[k][j];
}
}
}
return result;
}
Matrix transpose() {
Matrix result(cols, rows);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.data[j][i] = data[i][j];
}
}
return result;
}
~Matrix() {
for (int i = 0; i < rows; i++) {
delete[] data[i];
}
delete[] data;
}
};
int main() {
int r1, c1, r2, c2;
cout << "Enter rows and columns for the rst matrix: ";
cin >> r1 >> c1;
Matrix mat1(r1, c1);
mat1.inputMatrix();
cout << "Enter rows and columns for the second matrix: ";
cin >> r2 >> c2;
Matrix mat2(r2, c2);
mat2.inputMatrix();
try {
Matrix sumResult = mat1.sum(mat2);
cout << "Sum of matrices: \n";
sumResult.displayMatrix();
} catch (const exception& e) {
cout << e.what() << endl;
}
fi
try {
Matrix productResult = mat1.product(mat2);
cout << "Product of matrices: \n";
productResult.displayMatrix();
} catch (const exception& e) {
cout << e.what() << endl;
}
cout << "Transpose of rst matrix: \n";
Matrix transposeResult1 = mat1.transpose();
transposeResult1.displayMatrix();
cout << "Transpose of second matrix: \n";
Matrix transposeResult2 = mat2.transpose();
transposeResult2.displayMatrix();
return 0;
}
Q22. #include <iostream>
#include <string>
using namespace std;
class Person {
public:
string name;
Person(string n) : name(n) {}
virtual void display() {
cout << "Name: " << name << endl;
}
};
class Student : public Person {
public:
string course;
int marks;
int year;
Student(string n, string c, int m, int y) : Person(n), course(c), marks(m), year(y) {}
void display() override {
Person::display();
cout << "Course: " << course << "\nMarks: " << marks << "\nYear: " << year << endl;
}
};
class Employee : public Person {
public:
string department;
double salary;
Employee(string n, string d, double s) : Person(n), department(d), salary(s) {}
void display() override {
Person::display();
cout << "Department: " << department << "\nSalary: " << salary << endl;
}
};
int main() {
Person* person1 = new Student("John", "Computer Science", 90, 2023);
fi
Person* person2 = new Employee("Alice", "HR", 50000);
person1->display();
person2->display();
delete person1;
delete person2;
return 0;
}
Q23. #include <iostream>
#include <stdexcept>
#include <cmath>
using namespace std;
class Triangle {
public:
double a, b, c;
Triangle(double x, double y, double z) : a(x), b(y), c(z) {
if (a <= 0 || b <= 0 || c <= 0) {
throw invalid_argument("Sides must be greater than zero.");
}
if (a + b <= c || a + c <= b || b + c <= a) {
throw invalid_argument("Sum of any two sides must be greater than the third side.");
}
}
double areaRightAngled() {
return 0.5 * a * b; // Assuming a and b are perpendicular sides
}
double areaHeron() {
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c)); // Heron's formula
}
};
int main() {
try {
Triangle t(3, 4, 5);
cout << "Area of right-angled triangle: " << t.areaRightAngled() << endl;
cout << "Area of triangle using Heron's formula: " << t.areaHeron() << endl;
} catch (const exception& e) {
cout << e.what() << endl;
}
return 0;
}
Q24. #include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student {
public:
int rollNo;
string name;
string className;
int year;
double totalMarks;
Student(int r, string n, string c, int y, double m) : rollNo(r), name(n), className(c), year(y),
totalMarks(m) {}
void display() {
cout << "Roll No: " << rollNo << "\nName: " << name << "\nClass: " << className
<< "\nYear: " << year << "\nTotal Marks: " << totalMarks << endl;
}
void writeToFile() {
ofstream outFile("students.txt", ios::app);
outFile << rollNo << " " << name << " " << className << " " << year << " " << totalMarks <<
endl;
outFile.close();
}
static void readFromFile() {
ifstream inFile("students.txt");
int rollNo;
string name, className;
int year;
double totalMarks;
while (inFile >> rollNo >> name >> className >> year >> totalMarks) {
Student s(rollNo, name, className, year, totalMarks);
s.display();
}
inFile.close();
}
};
int main() {
Student s1(1, "John", "CS", 2023, 85.5);
s1.writeToFile();
cout << "Stored student records:\n";
Student::readFromFile();
return 0;
}
Q25. #include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
int main() {
ifstream inFile("input.txt");
ofstream outFile("output.txt");
if (!inFile || !outFile) {
cout << "Error opening le!" << endl;
return 1;
}
char ch;
while (inFile.get(ch)) {
if (!isspace(ch)) {
fi
outFile.put(ch);
}
}
inFile.close();
outFile.close();
cout << "Content copied and whitespaces removed!" << endl;
return 0;
}