From 436ff70b9f985ca9e4235826e75da7fb5eaab2fb Mon Sep 17 00:00:00 2001 From: NITISH KUMAR GIRI Date: Sat, 5 Apr 2025 17:16:34 +0530 Subject: [PATCH 01/11] Rename week1/.gitkeep to NITISH_KUMAR_GIRI --- NITISH_KUMAR_GIRI | 1 + week1/.gitkeep | 0 2 files changed, 1 insertion(+) create mode 100644 NITISH_KUMAR_GIRI delete mode 100644 week1/.gitkeep diff --git a/NITISH_KUMAR_GIRI b/NITISH_KUMAR_GIRI new file mode 100644 index 0000000..d3f5a12 --- /dev/null +++ b/NITISH_KUMAR_GIRI @@ -0,0 +1 @@ + diff --git a/week1/.gitkeep b/week1/.gitkeep deleted file mode 100644 index e69de29..0000000 From b0885fca8cf5275b257765e730e289e9661b45fc Mon Sep 17 00:00:00 2001 From: NITISH KUMAR GIRI Date: Sat, 5 Apr 2025 17:48:03 +0530 Subject: [PATCH 02/11] Update and rename NITISH_KUMAR_GIRI to week1 --- NITISH_KUMAR_GIRI => week1 | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename NITISH_KUMAR_GIRI => week1 (100%) diff --git a/NITISH_KUMAR_GIRI b/week1 similarity index 100% rename from NITISH_KUMAR_GIRI rename to week1 From 8b9de5e8cf86e5e508b0e4c74b859bca05984165 Mon Sep 17 00:00:00 2001 From: NITISH KUMAR GIRI Date: Sat, 5 Apr 2025 17:50:29 +0530 Subject: [PATCH 03/11] Rename .gitkeep to week4 --- .gitkeep | 0 week4 | 1 + 2 files changed, 1 insertion(+) delete mode 100644 .gitkeep create mode 100644 week4 diff --git a/.gitkeep b/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/week4 b/week4 new file mode 100644 index 0000000..d3f5a12 --- /dev/null +++ b/week4 @@ -0,0 +1 @@ + From 8076c8a0b46238d64da72246ad73aec1bb46697c Mon Sep 17 00:00:00 2001 From: NITISH KUMAR GIRI Date: Sat, 5 Apr 2025 17:54:05 +0530 Subject: [PATCH 04/11] Update and rename week1 to week11 --- week1 => week11 | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename week1 => week11 (100%) diff --git a/week1 b/week11 similarity index 100% rename from week1 rename to week11 From 6e4f2edf0171c3add0f6de2a4cdae5f94df540c0 Mon Sep 17 00:00:00 2001 From: NITISH KUMAR GIRI Date: Sat, 5 Apr 2025 18:01:59 +0530 Subject: [PATCH 05/11] Update and rename week11 to week1/.gitkeep --- week11 => week1/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename week11 => week1/.gitkeep (100%) diff --git a/week11 b/week1/.gitkeep similarity index 100% rename from week11 rename to week1/.gitkeep From b15ee6204e714423de5d39063c76df53a58104ef Mon Sep 17 00:00:00 2001 From: NITISH KUMAR GIRI Date: Sat, 5 Apr 2025 18:03:09 +0530 Subject: [PATCH 06/11] Update and rename week4 to week/.gitkeep --- week4 => week/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename week4 => week/.gitkeep (100%) diff --git a/week4 b/week/.gitkeep similarity index 100% rename from week4 rename to week/.gitkeep From bfda54aa28f27d04c12a19e7bff96be77169f3b7 Mon Sep 17 00:00:00 2001 From: NITISH KUMAR GIRI Date: Sat, 5 Apr 2025 23:08:51 +0530 Subject: [PATCH 07/11] Update and rename .gitkeep to .gitkeep --- {week => week4}/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {week => week4}/.gitkeep (100%) diff --git a/week/.gitkeep b/week4/.gitkeep similarity index 100% rename from week/.gitkeep rename to week4/.gitkeep From 797a4eb799e9052fa56fa69df51a53d4492fba81 Mon Sep 17 00:00:00 2001 From: Nitish89847 Date: Mon, 7 Apr 2025 21:42:55 +0530 Subject: [PATCH 08/11] new task added --- week1/.gitkeep | 20 +++++++++++++++ week1/question1.cpp | 20 +++++++++++++++ week1/question2.cpp | 25 +++++++++++++++++++ week1/question3.cpp | 29 +++++++++++++++++++++ week1/question4.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++ week1/question5.cpp | 31 +++++++++++++++++++++++ week1/question6.cpp | 20 +++++++++++++++ week1/question7.cpp | 24 ++++++++++++++++++ week1/question8.cpp | 22 ++++++++++++++++ 9 files changed, 252 insertions(+) create mode 100644 week1/question1.cpp create mode 100644 week1/question2.cpp create mode 100644 week1/question3.cpp create mode 100644 week1/question4.cpp create mode 100644 week1/question5.cpp create mode 100644 week1/question6.cpp create mode 100644 week1/question7.cpp create mode 100644 week1/question8.cpp diff --git a/week1/.gitkeep b/week1/.gitkeep index d3f5a12..c37275a 100644 --- a/week1/.gitkeep +++ b/week1/.gitkeep @@ -1 +1,21 @@ +#include +using namespace std; + +int main() { + for (int i = 1; i <= 100; ++i) { + if (i % 15 == 0) + cout << "FizzBuzz"; + else if (i % 3 == 0) + cout << "Fizz"; + else if (i % 5 == 0) + cout << "Buzz"; + else + cout << i; + + cout<<"\n"; + + } + + return 0; +} diff --git a/week1/question1.cpp b/week1/question1.cpp new file mode 100644 index 0000000..8c0f687 --- /dev/null +++ b/week1/question1.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; + +int main() { + for (int i = 1; i <= 100; ++i) { + if (i % 15 == 0) + cout << "FizzBuzz"; + else if (i % 3 == 0) + cout << "Fizz"; + else if (i % 5 == 0) + cout << "Buzz"; + else + cout << i; + + cout<<"\n"; + + } + + return 0; +} diff --git a/week1/question2.cpp b/week1/question2.cpp new file mode 100644 index 0000000..1bca459 --- /dev/null +++ b/week1/question2.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +int main() { + float a, b, c; + + cout << "Enter the three sides of the triangle: "; + cin >> a >> b >> c; + + if (a + b > c && a + c > b && b + c > a) { + cout << "Triangle can be formed." << endl; + + if (a == b && b == c) { + cout << "It is an Equilateral triangle." << endl; + } else if (a == b || a == c || b == c) { + cout << "It is an Isosceles triangle." << endl; + } else { + cout << "It is a Scalene triangle." << endl; + } + } else { + cout << "Triangle cannot be formed with the given sides." << endl; + } + + return 0; +} diff --git a/week1/question3.cpp b/week1/question3.cpp new file mode 100644 index 0000000..449ad6a --- /dev/null +++ b/week1/question3.cpp @@ -0,0 +1,29 @@ +#include +#include + +using namespace std; + +int main() { + int num, digit, largest = 0; + + cout << "Enter a number: "; + cin >> num; + + num = abs(num); + + if (num == 0) { + largest = 0; + } else { + while (num > 0) { + digit = num % 10; + if (digit > largest) { + largest = digit; + } + num = num / 10; + } + } + + cout << "The largest digit is: " << largest << endl; + + return 0; +} diff --git a/week1/question4.cpp b/week1/question4.cpp new file mode 100644 index 0000000..3e7568d --- /dev/null +++ b/week1/question4.cpp @@ -0,0 +1,61 @@ +#include +using namespace std; + +void decimalToBinary(int decimal) { + int binary[32]; + int index = 0; + + if (decimal == 0) { + cout << "Binary: 0" << endl; + return; + } + + while (decimal > 0) { + binary[index++] = decimal % 2; + decimal /= 2; + } + + cout << "Binary: "; + for (int i = index - 1; i >= 0; i--) { + cout << binary[i]; + } + cout << endl; +} + +void decimalToOctal(int decimal) { + int octal[32]; + int index = 0; + + if (decimal == 0) { + cout << "Octal: 0" << endl; + return; + } + + while (decimal > 0) { + octal[index++] = decimal % 8; + decimal /= 8; + } + + cout << "Octal: "; + for (int i = index - 1; i >= 0; i--) { + cout << octal[i]; + } + cout << endl; +} + +int main() { + int number; + + cout << "Enter a decimal number: "; + cin >> number; + + if (number < 0) { + cout << "Please enter a non-negative number." << endl; + return 1; + } + + decimalToBinary(number); + decimalToOctal(number); + + return 0; +} diff --git a/week1/question5.cpp b/week1/question5.cpp new file mode 100644 index 0000000..f15ea49 --- /dev/null +++ b/week1/question5.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int main() { + int number; + bool isPerfectSquare = false; + + cout << "Enter a number: "; + cin >> number; + + if (number < 0) { + cout << "Negative numbers cannot be perfect squares." << endl; + return 1; + } + + for (int i = 0; i * i <= number; i++) { + if (i * i == number) { + isPerfectSquare = true; + break; + } + } + + if (isPerfectSquare) { + cout << number << " is a Perfect Square." << endl; + } else { + cout << number << " is NOT a Perfect Square." << endl; + } + + return 0; +} diff --git a/week1/question6.cpp b/week1/question6.cpp new file mode 100644 index 0000000..07cb58c --- /dev/null +++ b/week1/question6.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main() { + int n; + cout << "Enter value of n: "; + cin >> n; + + for (int row = 1; row <= n; row++) { + for (int num = 1; num <= n; num++) { + for (int count = 1; count <= row; count++) { + cout << num; + } + } + cout << endl; + } + + return 0; +} diff --git a/week1/question7.cpp b/week1/question7.cpp new file mode 100644 index 0000000..df3e4f3 --- /dev/null +++ b/week1/question7.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() { + int n; + cout << "Enter the value of n: "; + cin >> n; + + int count = 1; + + for (int row = 1; row <= n; row++) { + for (int col = 1; col <= n; col++) { + cout << count; + if (col != n) { + cout << "*"; + } + count++; + } + cout << endl; + } + + return 0; +} diff --git a/week1/question8.cpp b/week1/question8.cpp new file mode 100644 index 0000000..7341c63 --- /dev/null +++ b/week1/question8.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main() { + int n; + cout << "Enter value of n: "; + cin >> n; + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= i; j++) { + cout << j; + } + for (int j = i; j >= 1; j--) { + cout << j; + } + + cout << endl; + } + + return 0; +} From e5385b1eb13f63b3a7e2dc1348cc13b47ae276d1 Mon Sep 17 00:00:00 2001 From: Nitish89847 Date: Sun, 20 Apr 2025 10:59:06 +0530 Subject: [PATCH 09/11] assignment completed --- week1/question3.cpp | 4 ++-- week2/question1.cpp | 0 week2/question2.cpp | 0 week2/question3.cpp | 0 week2/question4.cpp | 0 week2/question5.cpp | 0 week2/question6.cpp | 0 week2/question7.cpp | 0 week2/question8.cpp | 0 week2/question9.cpp | 0 week3/question1.cpp | 0 week3/question2.cpp | 0 week3/question3.cpp | 0 week3/question4.cpp | 0 week3/question5.cpp | 0 week3/question6.cpp | 0 week3/question7.cpp | 0 week3/question8.cpp | 0 18 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 week2/question1.cpp create mode 100644 week2/question2.cpp create mode 100644 week2/question3.cpp create mode 100644 week2/question4.cpp create mode 100644 week2/question5.cpp create mode 100644 week2/question6.cpp create mode 100644 week2/question7.cpp create mode 100644 week2/question8.cpp create mode 100644 week2/question9.cpp create mode 100644 week3/question1.cpp create mode 100644 week3/question2.cpp create mode 100644 week3/question3.cpp create mode 100644 week3/question4.cpp create mode 100644 week3/question5.cpp create mode 100644 week3/question6.cpp create mode 100644 week3/question7.cpp create mode 100644 week3/question8.cpp diff --git a/week1/question3.cpp b/week1/question3.cpp index 449ad6a..6fe8be1 100644 --- a/week1/question3.cpp +++ b/week1/question3.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include using namespace std; diff --git a/week2/question1.cpp b/week2/question1.cpp new file mode 100644 index 0000000..e69de29 diff --git a/week2/question2.cpp b/week2/question2.cpp new file mode 100644 index 0000000..e69de29 diff --git a/week2/question3.cpp b/week2/question3.cpp new file mode 100644 index 0000000..e69de29 diff --git a/week2/question4.cpp b/week2/question4.cpp new file mode 100644 index 0000000..e69de29 diff --git a/week2/question5.cpp b/week2/question5.cpp new file mode 100644 index 0000000..e69de29 diff --git a/week2/question6.cpp b/week2/question6.cpp new file mode 100644 index 0000000..e69de29 diff --git a/week2/question7.cpp b/week2/question7.cpp new file mode 100644 index 0000000..e69de29 diff --git a/week2/question8.cpp b/week2/question8.cpp new file mode 100644 index 0000000..e69de29 diff --git a/week2/question9.cpp b/week2/question9.cpp new file mode 100644 index 0000000..e69de29 diff --git a/week3/question1.cpp b/week3/question1.cpp new file mode 100644 index 0000000..e69de29 diff --git a/week3/question2.cpp b/week3/question2.cpp new file mode 100644 index 0000000..e69de29 diff --git a/week3/question3.cpp b/week3/question3.cpp new file mode 100644 index 0000000..e69de29 diff --git a/week3/question4.cpp b/week3/question4.cpp new file mode 100644 index 0000000..e69de29 diff --git a/week3/question5.cpp b/week3/question5.cpp new file mode 100644 index 0000000..e69de29 diff --git a/week3/question6.cpp b/week3/question6.cpp new file mode 100644 index 0000000..e69de29 diff --git a/week3/question7.cpp b/week3/question7.cpp new file mode 100644 index 0000000..e69de29 diff --git a/week3/question8.cpp b/week3/question8.cpp new file mode 100644 index 0000000..e69de29 From 21336bdc4caa50f95657a6364c065e9d2f578f41 Mon Sep 17 00:00:00 2001 From: Nitish89847 Date: Sun, 20 Apr 2025 11:20:14 +0530 Subject: [PATCH 10/11] completed --- week2/question1.cpp | 40 +++++++++++++++++++++++++++ week2/question2.cpp | 42 +++++++++++++++++++++++++++++ week2/question3.cpp | 28 +++++++++++++++++++ week2/question4.cpp | 34 +++++++++++++++++++++++ week2/question5.cpp | 41 ++++++++++++++++++++++++++++ week2/question6.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++ week2/question7.cpp | 23 ++++++++++++++++ week2/question8.cpp | 24 +++++++++++++++++ week2/question9.cpp | 30 +++++++++++++++++++++ 9 files changed, 328 insertions(+) diff --git a/week2/question1.cpp b/week2/question1.cpp index e69de29..6e8096c 100644 --- a/week2/question1.cpp +++ 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 index e69de29..59e3c9b 100644 --- a/week2/question2.cpp +++ 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 index e69de29..439e9f1 100644 --- a/week2/question3.cpp +++ 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 index e69de29..821f1d7 100644 --- a/week2/question4.cpp +++ 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 index e69de29..90b9c3c 100644 --- a/week2/question5.cpp +++ 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 index e69de29..92b31fd 100644 --- a/week2/question6.cpp +++ 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 index e69de29..c78ebc4 100644 --- a/week2/question7.cpp +++ 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 index e69de29..c4603c9 100644 --- a/week2/question8.cpp +++ 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 index e69de29..b02a94e 100644 --- a/week2/question9.cpp +++ 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; +} From 0a36d0fe696f44a7df68a1de3220a821cb7a4ebd Mon Sep 17 00:00:00 2001 From: Nitish89847 Date: Sun, 20 Apr 2025 11:22:41 +0530 Subject: [PATCH 11/11] done --- week3/question1.cpp | 33 +++++++++++++++++++++++++++++++++ week3/question2.cpp | 31 +++++++++++++++++++++++++++++++ week3/question3.cpp | 21 +++++++++++++++++++++ week3/question4.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ week3/question5.cpp | 37 +++++++++++++++++++++++++++++++++++++ week3/question6.cpp | 18 ++++++++++++++++++ week3/question7.cpp | 22 ++++++++++++++++++++++ week3/question8.cpp | 25 +++++++++++++++++++++++++ 8 files changed, 227 insertions(+) diff --git a/week3/question1.cpp b/week3/question1.cpp index e69de29..04b7aad 100644 --- a/week3/question1.cpp +++ 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 index e69de29..2669ed0 100644 --- a/week3/question2.cpp +++ 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 index e69de29..5affc4a 100644 --- a/week3/question3.cpp +++ 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 index e69de29..e81c8a3 100644 --- a/week3/question4.cpp +++ 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 index e69de29..50b48e2 100644 --- a/week3/question5.cpp +++ 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 index e69de29..c7b4b44 100644 --- a/week3/question6.cpp +++ 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 index e69de29..6a15c89 100644 --- a/week3/question7.cpp +++ 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 index e69de29..6f4fed2 100644 --- a/week3/question8.cpp +++ 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; +}