0% found this document useful (0 votes)
10 views6 pages

Basic Code Sheet

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Basic Code Sheet

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
12721124, 11:03AM code 47 // Reverse an Array #include using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Reversed array: for (int i=n- 1; i 2 0; i-) { cout << arr[i] <<" "; + return 0; } // Find the Second Largest Element in an Array # us i } include sing namespace std; nt main() { int arr[] = {10, 20, 4, 45, 99}; int n = sizeof(arr) / sizeof(arr[0]); int largest = INT_MIN, secondLargest = INT_MIN; for (int i = 0; i largest) { secondLargest = largest; largest = arrlil; } else if (arr[i] > secondiargest 8 arr[i] < largest) { secondLargest = arr[i]; } } cout << "Second largest element: " << secondLargest << endl; return 0; // Find the Intersection of Two Arrays tt us il include sing namespace std; nt main() { int arri[] = {1, 2, 3, 4, 5}; aint arr2[] = {3, 4, 5, 6, 7}; int nl = sizeof(arr1) / sizeof(arri[0]); int n2 = sizeof(arr2) / sizeof(arr2[0]); Intps:tarkjaber github olCode-to-POF/ 18 12721124, 1103AM code 48 cout << “Intersection: " 49 for (int i = 0; i < ni; i+) { 50 for (int j = 0; j < m2; j++) { 51 if (arrili] = arr2[j]) { 52 cout <« arrifi] «< " "; 53 } 54 } 55 } 56 return 0; 57 } 58 59 // Move All Zeroes to the End 60 #include 61 using namespace std; 62 63 int main() { 64 int arr[] = {0, 1, 9, 0, 8, 0, 2}; 65 int n = sizeof(arr) / sizeof(arr[@]); 66 int j = 0; 67 68 for (int i= 0; i< nj it) { 69 if (arr[i] # 0) { 70 swap(arrli], arr[j]); 1 it; 72 t 73 + 74 75 cout << "Array after moving zeroes to the end 76 for (int i= 0; i 84 #include 85 using namespace std; 86 87 int main() { 88 string str1 = "Listen"; 89 string str2 = "silent"; 90 91 sort([Link](), [Link]()); 92 sort([Link](), [Link]()); 93 94 if (str1 = str2) { 95 cout << "The strings are anagrams." << endl; ‘ntps:tarkjaber github o!Code-to-POF/ 218 12721124, 1103AM code 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 } else { cout << "The strings are not anagrams." << endl; } return 0; } // Find the Most Frequent Character in a String include #include using namespace std; int main() { string str = "character" unordered_map freq; for (char ch : str) { freqich]++; } char mostFrequent = str[0]; int maxCount = 0; for (auto pair : freq) { if ([Link] > maxCount) { maxCount = [Link]; mostFrequent = pair. first; } cout << "Most frequent character: maxCount << " times)" << endl; return 0; << mostFrequent << * (* << } // Count the Number of Vowels in a String #include using namespace std; int main() { string str - "Capgemini"; int count = 0; for (char ch : str) { if (ch = ‘at || ch = ch || ch = ='u') count++; ‘ntps:tarkjaber github o!Code-to-POF/ ais 12721124, 1103AM code 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 a7 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 t cout << "Number of vowels: " << count << endl; return 0; } // Reverse Each Word in a String include w#include using namespace std; int main() { string str = "Hello World"; stringstream ss(str); string word; cout << "Reversed words: "; while (ss >> word) { reverse([Link](), [Link]()); cout << word « " } return 0; } // Find Factorial of a Number #include using namespace std; int main() { int n= 5, factorial = 1 for (int i = 1; i < nj is) { factorial *= i; + cout << "Factorial of "<< n << "is" « return 0; } // Check if a Number is Prime #include using namespace std; int main() { int n= 175 bool isPrime = true; ‘ntps:tarkjaber github o!Code-to-POF/ factorial << endl; 46 12721124, 1103AM code 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 if (n < 2) isPrime = false; for (int i= 2; i* i using namespace std; int main() { int n= 10,a=0,b=1 cout << "Fibonacci series: "; for (int i= 1; i < nj i+) { cout using namespace std; int ged(int a, int b) { if (b = 0) return a; return gcd(b, a % b); t int main() { int num1 = 56, num2 = 98; ‘ntps:tarkjaber github o!Code-to-POF/ 56 12721124, 1103AM 237 cout << "GCD of " << numl << " and num2) << endl; 238 return 0; 239 4} 240 ‘ntps:tarkjaber github o!Code-to-POF/ code << num << is << ged(num1, 5s

You might also like