diff --git a/week1/question3.cpp b/week1/question3.cpp index 449ad6a..336bf54 100644 --- a/week1/question3.cpp +++ b/week1/question3.cpp @@ -1,3 +1,4 @@ + #include #include diff --git a/week2/question1.cpp b/week2/question1.cpp new file mode 100644 index 0000000..6e8096c --- /dev/null +++ b/week2/question1.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; + +int main() { + int n1, n2; + + cout << "Enter size of first array: "; + cin >> n1; + int arr1[n1]; + + cout << "Enter elements of first array:\n"; + for (int i = 0; i < n1; i++) { + cin >> arr1[i]; + } + + cout << "Enter size of second array: "; + cin >> n2; + int arr2[n2]; + + cout << "Enter elements of second array:\n"; + for (int i = 0; i < n2; i++) { + cin >> arr2[i]; + } + + int merged[n1 + n2]; + for (int i = 0; i < n1; i++) { + merged[i] = arr1[i]; + } + for (int i = 0; i < n2; i++) { + merged[n1 + i] = arr2[i]; + } + + cout << "Merged array in reverse order:\n"; + for (int i = n1 + n2 - 1; i >= 0; i--) { + cout << merged[i] << " "; + } + cout << endl; + + return 0; +} diff --git a/week2/question2.cpp b/week2/question2.cpp new file mode 100644 index 0000000..59e3c9b --- /dev/null +++ b/week2/question2.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; + +void sortArray(int arr[], int size) { + for (int i = 0; i < size - 1; i++) { + for (int j = i + 1; j < size; j++) { + if (arr[i] > arr[j]) { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + } +} + +void printArray(int arr[], int size) { + for (int i = 0; i < size; i++) { + cout << arr[i] << " "; + } + cout << endl; +} + +int main() { + int n; + + cout << "Enter the size of the array: "; + cin >> n; + + int arr[n]; + + cout << "Enter " << n << " elements:\n"; + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + + sortArray(arr, n); + + cout << "Sorted array in ascending order:\n"; + printArray(arr, n); + + return 0; +} diff --git a/week2/question3.cpp b/week2/question3.cpp new file mode 100644 index 0000000..439e9f1 --- /dev/null +++ b/week2/question3.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +int main() { + int n; + + cout << "Enter the size of the array: "; + cin >> n; + + int original[n], copy[n]; + + cout << "Enter " << n << " elements:\n"; + for (int i = 0; i < n; i++) { + cin >> original[i]; + } + + for (int i = 0; i < n; i++) { + copy[i] = original[i]; + } + + cout << "Copied array elements:\n"; + for (int i = 0; i < n; i++) { + cout << copy[i] << " "; + } + cout << endl; + + return 0; +} diff --git a/week2/question4.cpp b/week2/question4.cpp new file mode 100644 index 0000000..821f1d7 --- /dev/null +++ b/week2/question4.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; + +int main() { + int n; + cout << "Enter the size of the array: "; + cin >> n; + + int arr[n]; + cout << "Enter " << n << " elements:\n"; + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + int maxCount = 0; + int maxElement = arr[0]; + + for (int i = 0; i < n; i++) { + int count = 1; + for (int j = i + 1; j < n; j++) { + if (arr[i] == arr[j]) { + count++; + } + } + if (count > maxCount) { + maxCount = count; + maxElement = arr[i]; + } + } + + cout << "Maximum occurring integer is: " << maxElement << endl; + cout << "It occurs " << maxCount << " times." << endl; + + return 0; +} diff --git a/week2/question5.cpp b/week2/question5.cpp new file mode 100644 index 0000000..90b9c3c --- /dev/null +++ b/week2/question5.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std; + +int main() { + int n; + + cout << "Enter the size of the array: "; + cin >> n; + + if (n < 2) { + cout << "Array must contain at least two elements." << endl; + return 1; + } + + int arr[n]; + cout << "Enter " << n << " distinct elements:\n"; + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + + int smallest = INT_MAX; + int secondSmallest = INT_MAX; + + for (int i = 0; i < n; i++) { + if (arr[i] < smallest) { + secondSmallest = smallest; + smallest = arr[i]; + } else if (arr[i] < secondSmallest && arr[i] != smallest) { + secondSmallest = arr[i]; + } + } + + if (secondSmallest == INT_MAX) { + cout << "There is no second smallest element (all elements may be equal)." << endl; + } else { + cout << "Second smallest element is: " << secondSmallest << endl; + } + + return 0; +} diff --git a/week2/question6.cpp b/week2/question6.cpp new file mode 100644 index 0000000..92b31fd --- /dev/null +++ b/week2/question6.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +const int SIZE = 3; + +void inputMatrix(int matrix[SIZE][SIZE], const string& name) { + cout << "Enter elements of " << name << " matrix (3x3):\n"; + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + cin >> matrix[i][j]; + } + } +} + +void displayMatrix(int matrix[SIZE][SIZE]) { + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + cout << matrix[i][j] << "\t"; + } + cout << endl; + } +} + +void addMatrices(int a[SIZE][SIZE], int b[SIZE][SIZE], int result[SIZE][SIZE]) { + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + result[i][j] = a[i][j] + b[i][j]; + } + } +} + +void transposeMatrix(int matrix[SIZE][SIZE], int transpose[SIZE][SIZE]) { + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + transpose[j][i] = matrix[i][j]; + } + } +} + +int main() { + int mat1[SIZE][SIZE], mat2[SIZE][SIZE]; + int sumOriginal[SIZE][SIZE]; + int trans1[SIZE][SIZE], trans2[SIZE][SIZE], sumTranspose[SIZE][SIZE]; + + inputMatrix(mat1, "first"); + inputMatrix(mat2, "second"); + + addMatrices(mat1, mat2, sumOriginal); + cout << "\nSum of the original matrices:\n"; + displayMatrix(sumOriginal); + + transposeMatrix(mat1, trans1); + transposeMatrix(mat2, trans2); + + cout << "\nTranspose of first matrix:\n"; + displayMatrix(trans1); + + cout << "\nTranspose of second matrix:\n"; + displayMatrix(trans2); + + addMatrices(trans1, trans2, sumTranspose); + cout << "\nSum of the transposed matrices:\n"; + displayMatrix(sumTranspose); + + return 0; +} diff --git a/week2/question7.cpp b/week2/question7.cpp new file mode 100644 index 0000000..c78ebc4 --- /dev/null +++ b/week2/question7.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +int fibonacci(int n) { + if (n == 0) return 0; + if (n == 1) return 1; + return fibonacci(n - 1) + fibonacci(n - 2); +} + +int main() { + int n; + cout << "Enter the position (n) to find the nth Fibonacci number: "; + cin >> n; + + if (n < 0) { + cout << "Please enter a non-negative integer." << endl; + } else { + int result = fibonacci(n); + cout << "Fibonacci number at position " << n << " is: " << result << endl; + } + + return 0; +} diff --git a/week2/question8.cpp b/week2/question8.cpp new file mode 100644 index 0000000..c4603c9 --- /dev/null +++ b/week2/question8.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +// Recursive function to find sum of digits +int sumOfDigits(int n) { + if (n == 0) + return 0; + return (n % 10) + sumOfDigits(n / 10); +} + +int main() { + int number; + + cout << "Enter a number: "; + cin >> number; + + if (number < 0) number = -number; // Handle negative input + + int result = sumOfDigits(number); + + cout << "Sum of digits: " << result << endl; + + return 0; +} diff --git a/week2/question9.cpp b/week2/question9.cpp new file mode 100644 index 0000000..b02a94e --- /dev/null +++ b/week2/question9.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +void printArray(int arr[], int index, int size) { + if (index == size) { + return; + } + + cout << arr[index] << " "; + printArray(arr, index + 1, size); +} + +int main() { + int n; + + cout << "Enter the size of the array: "; + cin >> n; + + int arr[n]; + + cout << "Enter " << n << " elements:\n"; + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + + cout << "Array elements are:\n"; + printArray(arr, 0, n); + + return 0; +} diff --git a/week3/question1.cpp b/week3/question1.cpp new file mode 100644 index 0000000..04b7aad --- /dev/null +++ b/week3/question1.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +void moveNegatives(int arr[], int n) { + int j = 0; + for (int i = 0; i < n; i++) { + if (arr[i] < 0) { + swap(arr[i], arr[j]); + j++; + } + } +} + +void printArray(int arr[], int n) { + for (int i = 0; i < n; i++) { + cout << arr[i] << " "; + } + cout << endl; +} + +int main() { + int n; + cin >> n; + int arr[n]; + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + + moveNegatives(arr, n); + printArray(arr, n); + + return 0; +} diff --git a/week3/question2.cpp b/week3/question2.cpp new file mode 100644 index 0000000..2669ed0 --- /dev/null +++ b/week3/question2.cpp @@ -0,0 +1,31 @@ +#include +#include +using namespace std; + +void countCharacters(const string& str) { + int alphabets = 0, digits = 0, specials = 0; + + for (char ch : str) { + if (isalpha(ch)) { + alphabets++; + } else if (isdigit(ch)) { + digits++; + } else if (ispunct(ch)) { + specials++; + } + } + + cout << "Alphabets: " << alphabets << endl; + cout << "Digits: " << digits << endl; + cout << "Special Characters: " << specials << endl; +} + +int main() { + string input; + cout << "Enter a string: "; + getline(cin, input); + + countCharacters(input); + + return 0; +} diff --git a/week3/question3.cpp b/week3/question3.cpp new file mode 100644 index 0000000..5affc4a --- /dev/null +++ b/week3/question3.cpp @@ -0,0 +1,21 @@ +#include +#include +using namespace std; + +void removeNonAlphabets(string& str) { + int index = 0; + for (int i = 0; i < str.length(); i++) { + if (isalpha(str[i])) { + str[index++] = str[i]; + } + } + str.resize(index); +} + +int main() { + string input; + getline(cin, input); + removeNonAlphabets(input); + cout << input << endl; + return 0; +} diff --git a/week3/question4.cpp b/week3/question4.cpp new file mode 100644 index 0000000..e81c8a3 --- /dev/null +++ b/week3/question4.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; + +void rotateArray(int arr[], int n, int k) { + k = k % n; + int temp[k]; + + for (int i = 0; i < k; i++) { + temp[i] = arr[n - k + i]; + } + + for (int i = n - 1; i >= k; i--) { + arr[i] = arr[i - k]; + } + + for (int i = 0; i < k; i++) { + arr[i] = temp[i]; + } +} + +void printArray(int arr[], int n) { + for (int i = 0; i < n; i++) { + cout << arr[i] << " "; + } + cout << endl; +} + +int main() { + int n, k; + cin >> n >> k; + int arr[n]; + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + + rotateArray(arr, n, k); + printArray(arr, n); + + return 0; +} diff --git a/week3/question5.cpp b/week3/question5.cpp new file mode 100644 index 0000000..50b48e2 --- /dev/null +++ b/week3/question5.cpp @@ -0,0 +1,37 @@ +#include +#include +using namespace std; + +void removeDuplicates(int arr[], int n, int newArr[], int& newSize) { + unordered_set seen; + newSize = 0; + + for (int i = 0; i < n; i++) { + if (seen.find(arr[i]) == seen.end()) { + newArr[newSize++] = arr[i]; + seen.insert(arr[i]); + } + } +} + +void printArray(int arr[], int n) { + for (int i = 0; i < n; i++) { + cout << arr[i] << " "; + } + cout << endl; +} + +int main() { + int n; + cin >> n; + int arr[n]; + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + + int newArr[n], newSize; + removeDuplicates(arr, n, newArr, newSize); + printArray(newArr, newSize); + + return 0; +} diff --git a/week3/question6.cpp b/week3/question6.cpp new file mode 100644 index 0000000..c7b4b44 --- /dev/null +++ b/week3/question6.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; + +void toLowerCase(string& str) { + for (int i = 0; i < str.length(); i++) { + if (str[i] >= 'A' && str[i] <= 'Z') { + str[i] = str[i] + ('a' - 'A'); + } + } +} + +int main() { + string input; + getline(cin, input); + toLowerCase(input); + cout << input << endl; + return 0; +} diff --git a/week3/question7.cpp b/week3/question7.cpp new file mode 100644 index 0000000..6a15c89 --- /dev/null +++ b/week3/question7.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +void removeCharacter(string& str, char ch) { + int index = 0; + for (int i = 0; i < str.length(); i++) { + if (str[i] != ch) { + str[index++] = str[i]; + } + } + str.resize(index); +} + +int main() { + string input; + char ch; + getline(cin, input); + cin >> ch; + removeCharacter(input, ch); + cout << input << endl; + return 0; +} diff --git a/week3/question8.cpp b/week3/question8.cpp new file mode 100644 index 0000000..6f4fed2 --- /dev/null +++ b/week3/question8.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +bool isPalindrome(string str) { + int start = 0, end = str.length() - 1; + while (start < end) { + if (str[start] != str[end]) { + return false; + } + start++; + end--; + } + return true; +} + +int main() { + string input; + getline(cin, input); + if (isPalindrome(input)) { + cout << "Yes" << endl; + } else { + cout << "No" << endl; + } + return 0; +}