Csassp
Csassp
Teacher’s signature
Q1. #include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
return 0;
}
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;
}
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;
}
int main() {
fl
double num1, num2;
int 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;
}
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;
fi
}
Q7.
a) #include <iostream>
using namespace std;
int main() {
int n = 5;
char letter = 'A';
return 0;
}
b) #include <iostream>
using namespace std;
int main() {
int n = 5;
return 0;
}
C) #include <iostream>
using namespace std;
int main() {
int n = 8;
return 0;
}
D) #include <iostream>
using namespace std;
int main() {
int n = 4, num = 1;
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
void mergeOrderedArrays(int arr1[], int size1, int arr2[], int size2, int merged[], int& mergedSize) {
int i = 0, j = 0, k = 0;
mergedSize = k;}
int main() {
int arr1[100], arr2[100], merged[200];
int size1, size2, mergedSize;
return 0;
}
return 0;
}
int main() {
char str1[100], str2[100];
int choice, pos;
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;
}
int main() {
int arr1[] = {1, 3, 5, 7};
int arr2[] = {2, 4, 6, 8};
return 0;
}
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;
B) #include <iostream>
using namespace std;
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;
return 0;
}
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 main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "GCD of " << num1 << " and " << num2 << " is " << gcd(num1, num2) << endl;
return 0;
}
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 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;
}
return 0;
}
class Person {
public:
string name;
Person(string n) : name(n) {}
virtual void display() {
cout << "Name: " << name << 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;
}
class Triangle {
public:
double a, b, c;
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;
}
class Student {
public:
int rollNo;
string name;
string className;
int year;
double totalMarks;
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();
}
int main() {
Student s1(1, "John", "CS", 2023, 85.5);
s1.writeToFile();
return 0;
}
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;
}