diff --git a/December 01/Vanishing Number.c b/December 01/Vanishing Number.c new file mode 100644 index 0000000..ca8bf55 --- /dev/null +++ b/December 01/Vanishing Number.c @@ -0,0 +1,64 @@ +#include +#include + +int arraySize() +{ + int n; + printf("Enter the value of n: "); + scanf("%d",&n); + return n; +} + +int* createArray(int n) +{ + int ctrl1; + int* array = (int*)malloc(n * sizeof(int)); + + for( ctrl1 = 0; ctrl1 < n; ctrl1++) + { + printf("Enter element-%d: ",ctrl1+1); + scanf("%d",&array[ctrl1]); + } + return array; +} + +int vanishingNumber(int array[], int n) +{ + int ctrl2 = 0, ctrl3 = 1, flag = 0; + + while (ctrl2 < n && ctrl3 <= n) + { + if (array[ctrl2] != ctrl3) + { + return ctrl3; + flag = 1; + break; + } + else + { + flag = 0; + ctrl2++; + ctrl3++; + } + } + if(flag == 0) + { + return 0; + } +} + +int main() +{ + int size = arraySize(); + int* result = createArray(size); + int missingNumber = vanishingNumber(result, size); + if (missingNumber != 0) + { + printf("The missing number is: %d", missingNumber); + } + else + { + printf("The numbers are in order."); + } +} + diff --git a/December 02/WaveSort.c b/December 02/WaveSort.c new file mode 100644 index 0000000..b29ed7f --- /dev/null +++ b/December 02/WaveSort.c @@ -0,0 +1,78 @@ +#include +#include + +int arraySize() +{ + int n; + printf("Enter the number of elements for array: "); + scanf("%d", &n); + return n; +} + +int* createArray(int size) +{ + int ctrl; + int* array = (int*)malloc(size * sizeof(int)); + for(ctrl = 0; ctrl < size; ctrl++) + { + printf("Enter element-%d: ",ctrl+1); + scanf("%d", &array[ctrl]); + } + return array; +} + +int* arraySort(int array[], int size) +{ + int ctrl1, ctrl2; + int end = size - 1; + for(ctrl1 = 0; ctrl1 < end; ctrl1++) + { + for(ctrl2 = ctrl1 + 1; ctrl2 < size; ctrl2++) + { + if(array[ctrl1] > array[ctrl2]) + { + int temp = array[ctrl1]; + array[ctrl1] = array[ctrl2]; + array[ctrl2] = temp; + } + } + } + return array; +} + +void display(int array[], int size) +{ + int ctrl; + for(ctrl = 0; ctrl < size; ctrl++) + { + printf("%d\t",array[ctrl]); + } +} + +int* waveSort(int array[], int result[], int size) +{ + int end = size - 1; + int ctrl = 0, ctrl1 = 0; + int ctrl2 = end; + while(ctrl1 < ctrl2) + { + result[ctrl] = array[ctrl2]; + ctrl++; + result[ctrl] = array[ctrl1]; + ctrl++; + ctrl1++; + ctrl2--; + } + return result; +} + +int main() +{ + int size = arraySize(); + int result[size]; + int* input = createArray(size); + int* sortedInput = arraySort(input, size); + int* output = waveSort(sortedInput, result, size); + display(output, size); + return 0; +} \ No newline at end of file diff --git a/December 03/Alternate Square.c b/December 03/Alternate Square.c new file mode 100644 index 0000000..eef0433 --- /dev/null +++ b/December 03/Alternate Square.c @@ -0,0 +1,113 @@ +#include + +int number(char colour[]) +{ + int n; + printf("Enter the number of %s squares: ", colour); + scanf("%d",&n); + return n; +} + +void display(char array[], int size) +{ + int ctrl; + for(ctrl = 0; ctrl < size; ctrl++) + { + printf("%c\t",array[ctrl]); + } +} + +void redSequence(int numberOfRed, int numberOfBlue) +{ + int ctrl, idx = 0; + int size = numberOfRed + numberOfBlue; + char array[size]; + for(ctrl = 1; ctrl <= numberOfRed; ctrl++) + { + array[idx] = 'R'; + idx+=2; + } + idx = 1; + for(ctrl = 1; ctrl <= numberOfBlue; ctrl++) + { + array[idx] = 'B'; + idx+=2; + } + display(array, size); +} +void blueSequence(int numberOfRed, int numberOfBlue) +{ + int ctrl, idx = 0; + int size = numberOfRed + numberOfBlue; + char array[size]; + for(ctrl = 1; ctrl <= numberOfRed; ctrl++) + { + array[idx] = 'B'; + idx+=2; + } + idx = 1; + for(ctrl = 1; ctrl <= numberOfBlue; ctrl++) + { + array[idx] = 'R'; + idx+=2; + } + display(array, size); +} + +void possibleArrangement(int numberOfRed, int numberOfBlue, int boolean) +{ + int twoRemaining = numberOfBlue - numberOfRed; + int oneRemaining = numberOfRed - numberOfBlue; + + if(boolean == 1 && oneRemaining == 1) + { + printf("It is possible to form a sequence in which no two adjacent squares are of the same color\n"); + redSequence(numberOfRed, numberOfBlue); + } + + else if(boolean == 2 && twoRemaining == 1) + { + printf("It is possible to form a sequence in which no two adjacent squares are of the same color\n"); + blueSequence(numberOfRed, numberOfBlue); + } + else if(boolean == 0 && oneRemaining == 0) + { + printf("It is possible to form a sequence in which no two adjacent squares are of the same color\n"); + printf("Sequence-1: \n"); + redSequence(numberOfRed, numberOfBlue); + + printf("\nSequence-2: \n"); + blueSequence(numberOfRed, numberOfBlue); + } + + else + { + printf("Not possible"); + } +} + +void sizeCheck(int numberOfRed, int numberOfBlue) +{ + if(numberOfRed > numberOfBlue) + { + possibleArrangement(numberOfRed, numberOfBlue, 1); + } + + else if(numberOfRed < numberOfBlue) + { + possibleArrangement(numberOfRed, numberOfBlue, 2); + } + + else + { + possibleArrangement(numberOfRed, numberOfBlue, 0); + } +} + +int main() +{ + int red = number("red"); + int blue = number("blue"); + sizeCheck(red, blue); + return 0; +} \ No newline at end of file diff --git a/December 04/Plant Growth Tracker.c b/December 04/Plant Growth Tracker.c new file mode 100644 index 0000000..8a1aa3d --- /dev/null +++ b/December 04/Plant Growth Tracker.c @@ -0,0 +1,60 @@ +#include + +int input() +{ + int numberOfMonths; + printf("\nEnter the number of months: "); + scanf("%d",&numberOfMonths); + return numberOfMonths; +} + +int predict(int numberOfMonths) +{ + int start = 0, next = 1, ctrl; + int sum = start + next; + for(ctrl = 2; ctrl < numberOfMonths; ctrl++) + { + start = next; + next = sum; + sum = start + next; + } + return sum; +} + +int errorHandling(int numberOfMonths) +{ + if(numberOfMonths == 1) + { + return 1; + } + else if(numberOfMonths > 1) + { + return 0; + } + else + { + return -1; + } +} + +int main() +{ + int numberOfMonths = input(); + int error = errorHandling(numberOfMonths); + if (error == 1) + { + printf("The plants take two months to grow before increasing in numbers."); + } + + else if(numberOfMonths > 1) + { + int prediction = predict(numberOfMonths); + printf("\nAfter %d months, there will be %d plants", numberOfMonths, prediction); + } + + else + { + printf("Please enter the number of months as postive integers"); + } + return 0; +} \ No newline at end of file diff --git a/December 05/JosephusProblem.java b/December 05/JosephusProblem.java new file mode 100644 index 0000000..09bdc2f --- /dev/null +++ b/December 05/JosephusProblem.java @@ -0,0 +1,133 @@ +import java.util.*; +import java.lang.*; + +public class JosephusProblem +{ + static int numberOfPeople() + { + Scanner sc = new Scanner(System.in); + System.out.println("Enter the number of people: "); + int number = sc.nextInt(); + return number; + } + + static String[] inputArray(int size) + { + Scanner sc = new Scanner(System.in); + + String array[] = new String[size]; + int ctrl; + + for(ctrl = 0; ctrl < size; ctrl++) + { + int i = ctrl +1; + System.out.println("Enter the element-" + i +": "); + array[ctrl] = sc.next(); + } + return array; + } + + static void display(String array[], int size) + { + int ctrl; + + for(ctrl = 0; ctrl < size; ctrl++) + { + System.out.print(array[ctrl] + "\t"); + } + } + + static int safePosition(String array[], int size) + { + int ctrl, idx = 0; + for(ctrl = 0; ctrl < size; ctrl++) + { + if(array[ctrl].equalsIgnoreCase("Done") == false) + { + idx = ctrl + 1; + } + } + return idx; + } + + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + + int size = numberOfPeople(); + String input[] = inputArray(size); + String temp[] = new String[size - 1]; + int ctrl = 0, ctrl1 = 0, counter = 1, i = 1; + System.out.println("Enter the value of k: "); + int k = sc.nextInt(); + + while(temp[size-2] == null) + { + // System.out.println("\n"); + // System.out.println("Iteration: " + i); + + while(ctrl < size) + { + // System.out.println("\nctrl: " + ctrl); + // System.out.println("\nCounter: " + counter); + + if(counter != k && input[ctrl].equalsIgnoreCase("Done") == false) + { + // System.out.println("1st if"); + ctrl++; + counter++; + } + + else if(counter !=k && input[ctrl].equalsIgnoreCase("Done") == true) + { + ctrl++; + // System.out.println("1st else if"); + } + + else if(counter == k && input[ctrl].equalsIgnoreCase("Done") == false) + { + // System.out.println("2nd else if"); + counter = 1; + temp[ctrl1] = input[ctrl]; + // display(temp, size-1); + // System.out.println("\n"); + ctrl1++; + input[ctrl] = "Done"; + // display(input, size); + ctrl++; + } + + else if(counter == k && input[ctrl].equalsIgnoreCase("Done") == true) + { + // System.out.println("3rd else if"); + ctrl++; + } + } + i++; + + if(ctrl == size && counter < k) + { + if(counter == 1) + { + // System.out.println("1st outer if inner if"); + ctrl = 0; + } + else + { + // System.out.println("1st outer if inner else "); + ctrl = 0; + counter++; + } + } + + else if(ctrl == size && counter == k) + { + // System.out.println("2nd outer else if"); + ctrl = 0; + } + + } + int result = safePosition(input, size); + System.out.println("The player should stand at " + result + " to survive the game."); + } +} \ No newline at end of file diff --git a/December 06/TargetPairFinder.c b/December 06/TargetPairFinder.c new file mode 100644 index 0000000..8789caa --- /dev/null +++ b/December 06/TargetPairFinder.c @@ -0,0 +1,134 @@ +#include +#include + +int arraySize() +{ + int size; + printf("\nEnter the number of elements in the array: "); + scanf("%d", &size); + return size; +} + +int* createArray(int size) +{ + int* array = (int*)malloc(size * sizeof(int)); + int i; + for(i = 0; i < size; i++) + { + printf("\nEnter element-%d: ", i+1); + scanf("%d", &array[i]); + } + + return array; +} + +int inputTarget() +{ + int target; + printf("\nEnter target element: "); + scanf("%d", &target); + return target; +} + +int binarySearch(int searchElement, int* array, int size) +{ + int low = 0; + int high = size - 1; + int found = -1; + + while(low <= high) + { + // printf("\nLow Value: %d", low); + // printf("\nHigh Value: %d", high); + int middle = (low + high)/2; + // printf("\nMiddle Index: %d", middle); + int value = array[middle]; + // printf("\nMiddle value: %d", value); + + if(value < searchElement) + + low = middle + 1; + + else if(value > searchElement) + + high = middle - 1; + + else if(value == searchElement) + { + found = middle; + break; + } + } + return found; +} + +void arraySort(int* array, int size) +{ + int ctrl1, ctrl2; + int end = size - 1; + for(ctrl1 = 0; ctrl1 < end; ctrl1++) + { + for(ctrl2 = ctrl1 + 1; ctrl2 < size; ctrl2++) + { + if(array[ctrl1] > array[ctrl2]) + { + int temp = array[ctrl1]; + array[ctrl1] = array[ctrl2]; + array[ctrl2] = temp; + } + } + } +} + +void targetPairFinder(int target, int* array, int size) +{ + // printf("\n%d", size); + arraySort(array, size); + for(int i = 0; i < size; i++) + { + // printf("\nIteration-%d", i); + // printf("\n"); + // for(int i = 0; i < size; i++) + // { + // printf("%d\t", array[i]); + // } + // printf("\n"); + if(array[i] != 0 && array[i] < target) + { + int remaining = target - array[i]; + // printf("\nRemaining: %d", remaining); + int idx = binarySearch(remaining, array, size); + // printf("\nIndex: %d", idx); + if(idx != -1 && array[i] != array[idx]) + { + printf("(%d, %d)", array[i], array[idx]); + array[i] = 0; + array[idx] = 0; + arraySort(array, size); + } + + } + // printf("\n\n"); + + } +} + +void main() +{ + int n = arraySize(); + int* input = createArray(n); + int target = inputTarget(); + targetPairFinder(target, input, n); + + // int array[6] = {2, 4, 3, 7, 1, 5}; + // arraySort(array, 6); + + // for(int i = 0; i < 6; i++) + // { + // printf("%d\t", array[i]); + // } + + // int search = binarySearch(5, array, 6); + + // printf("\n%d", search); +} \ No newline at end of file diff --git a/December 07/MagicalTower.c b/December 07/MagicalTower.c new file mode 100644 index 0000000..27a81cc --- /dev/null +++ b/December 07/MagicalTower.c @@ -0,0 +1,88 @@ +#include +#include + +int numberOfFloors() +{ + int n; + printf("\nEnter the number of floors in the Magical Tower: "); + scanf("%d", &n); + return n; +} + +int powerOf(int base, int power) +{ + int powerValue = 1, i; + for(i = 0; i < power; i++) + { + powerValue = powerValue * base; + } + return powerValue; +} + +int numberOfDigits(int number) +{ + int count = 0, r; + while(number != 0) + { + count++; + r = number % 10; + number = number / 10; + } + return count; +} + +int** constructTower(int numberOfFloors) +{ + int** array = (int**)malloc(numberOfFloors * sizeof(int*)), i; + + for(i = 0; i < numberOfFloors; i++) + { + int powerValue = powerOf(11, i); + int digitCount = numberOfDigits(powerValue); + + array[i] = (int*)malloc(digitCount * sizeof(int)); + int idx = digitCount - 1; + + while(powerValue != 0) + { + array[i][idx] = powerValue % 10; + idx--; + powerValue = powerValue / 10; + } + } + + return array; +} + +void display(int** array, int numberOfFloors) +{ + int i, j; + printf("["); + for(i = 0; i < numberOfFloors; i++) + { + int powerValue = powerOf(11,i); + int digitCount = numberOfDigits(powerValue); + printf("["); + for(j = 0; j < digitCount; j++) + { + if(j == digitCount - 1) + + printf("%d", array[i][j]); + + else + + printf("%d, ", array[i][j]); + + } + printf("]"); + printf("\t"); + } + printf("]"); +} + +void main() +{ + int n = numberOfFloors(); + int** tower = constructTower(n); + display(tower, n); +} \ No newline at end of file diff --git a/December 08/DigitManipulation.c b/December 08/DigitManipulation.c new file mode 100644 index 0000000..a219858 --- /dev/null +++ b/December 08/DigitManipulation.c @@ -0,0 +1,31 @@ +#include + +int inputRange() +{ + int n; + printf("\nEnter the range: "); + scanf("%d", &n); + return n; +} + +void digitManipulation(int n) +{ + int r, squareSum = 0; + for(int i = 1; i <= n; i++) + { + int j = i; + while(j != 0) + { + r = j % 10; + squareSum = squareSum + r * r; + j = j / 10; + } + } + printf("\nSum of Square of all digits from 1 to %d is: %d", n, squareSum); +} + +void main() +{ + int input = inputRange(); + digitManipulation(input); +} \ No newline at end of file diff --git a/December 09/CustomerReturnFrequency.c b/December 09/CustomerReturnFrequency.c new file mode 100644 index 0000000..5e05a7b --- /dev/null +++ b/December 09/CustomerReturnFrequency.c @@ -0,0 +1,45 @@ +#include +#include + +int inputReturns() +{ + int n; + printf("\nEnter the number of customers who have returned their product: "); + scanf("%d", &n); + return n; +} + +int* arrayReturns(int numberOfReturns) +{ + int* array = (int*)malloc(numberOfReturns * sizeof(int)); + printf("\nEnter the number of products returned by each customer: "); + for(int i = 0; i < numberOfReturns; i++) + { + printf("\nEnter the number of products returned by customer-%d: ", i + 1); + scanf("%d", &array[i]); + } + + return array; +} + +int returnOnce(int* array, int numberOfReturns) +{ + int count = 0; + + for(int i = 0; i < numberOfReturns; i++) + { + if(array[i] == 1) + + count++; + } + + return count; +} + +void main() +{ + int n = inputReturns(); + int* returns = arrayReturns(n); + int countOfOne = returnOnce(returns, n); + printf("Therefore, %d customers had returned their products exactly once", countOfOne); +} \ No newline at end of file diff --git a/December 10/ConcurrentTask.java b/December 10/ConcurrentTask.java new file mode 100644 index 0000000..3b4d6b5 --- /dev/null +++ b/December 10/ConcurrentTask.java @@ -0,0 +1,110 @@ +import java.util.*; + +public class ConcurrentTask +{ + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + ArrayList taskList = new ArrayList<>(); + ArrayList numDependency = new ArrayList<>(); + ArrayList> task = new ArrayList<>(); + + int i, j, k; + System.out.print("\nEnter the number of tasks: "); + int numberOfTasks = sc.nextInt(); + sc.nextLine(); + + for(i = 0; i < numberOfTasks; i++) + { + System.out.printf("\nEnter the name of task-%d: ", i + 1); + String taskName = sc.nextLine(); + taskList.add(taskName); + + System.out.print("\n\tEnter the number of dependencies: "); + int noOfDependencies = sc.nextInt(); + sc.nextLine(); + numDependency.add(noOfDependencies); + ArrayList dependencies = new ArrayList<>(); + + for(j = 0; j < noOfDependencies; j++) + { + System.out.printf("\n\t\tEnter dependency-%d: ", j + 1); + String element = sc.nextLine(); + dependencies.add(element); + } + + task.add(dependencies); + } + + int flag = 0; + for(i = 0; i < numberOfTasks - 1; i++) + { + int size = numDependency.get(i); + ArrayList taskDependencies = task.get(i); + for(j = 0; j < size; j++) + { + String e1 = taskDependencies.get(j); + int flag1 = 0; + for(k = i + 1; k < numberOfTasks; k++) + { + String e2 = taskList.get(k); + if(e1.equals(e2) == true) + flag1 = 1; + break; + } + + if(flag1 == 1) + { + flag = 1; + break; + } + } + if(flag == 1) + System.out.println("Error: Cyclic Dependency detected"); + break; + } + + if (flag == 0) + { + ArrayList> taskExecution = new ArrayList<>(); + ArrayList startState = new ArrayList<>(); + startState.add(taskList.get(0)); + taskExecution.add(startState); + + ArrayList> tempTask = new ArrayList<>(); + ArrayList tempTaskList = new ArrayList<>(); + tempTask = task; + tempTaskList = taskList; + + for(i = 1; i < tempTask.size(); i++) + { + ArrayList list1 = new ArrayList<>(); + list1 = tempTask.get(i); + ArrayList state = new ArrayList<>(); + state.add(tempTaskList.get(i)); + int n = i + 1; + + while(n < tempTask.size()) + { + ArrayList list2 = new ArrayList<>(); + list2 = tempTask.get(n); + if(list1.equals(list2) == true) + { + state.add(tempTaskList.get(n)); + tempTask.remove(n); + tempTaskList.remove(n); + } + + else + + n++; + } + taskExecution.add(state); + } + + System.out.println(taskExecution); + + + } + } +} \ No newline at end of file diff --git a/December 11/RobotReturns.java b/December 11/RobotReturns.java new file mode 100644 index 0000000..89f61f4 --- /dev/null +++ b/December 11/RobotReturns.java @@ -0,0 +1,95 @@ +import java.util.*; + +public class RobotReturns +{ + static String movesSequence(int numberOfMoves) + { + Scanner sc = new Scanner(System.in); + + String moves = new String(); + System.out.print("\nEnter moves: "); + moves = sc.nextLine(); + + return moves; + } + + static int numberOfMoves() + { + Scanner sc = new Scanner(System.in); + + System.out.print("\nEnter number of moves: "); + int n = sc.nextInt(); + + return n; + } + + static boolean moveRobot(String moves, int numberOfMoves) + { + int x = 0, y = 0; + for(int i = 0; i < numberOfMoves; i++) + { + switch(moves.charAt(i)) + { + case 'R': + { + x += 1; + break; + } + + case 'L': + { + x -= 1; + break; + } + + case 'U': + { + y += 1; + break; + } + + case 'D': + { + y -= 1; + } + } + } + + if(finalPosition(x, y)) + + return true; + + else + + return false; + } + + static boolean finalPosition(int x, int y) + { + if(x == 0 && y == 0) + + return true; + + else + + return false; + } + + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + int n = numberOfMoves(); + String moves = movesSequence(n); + + if(moveRobot(moves, n)) + { + System.out.print("\ntrue"); + } + + else + + { + System.out.print("\nfalse"); + } + } +} \ No newline at end of file