-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprograms.json
More file actions
1 lines (1 loc) · 43.1 KB
/
programs.json
File metadata and controls
1 lines (1 loc) · 43.1 KB
1
[{"uid":0,"title":"Average of Array Elements","category":"array","code":"// C++ program to calculate average of array elements\n#include <iostream>\nusing namespace std;\n\n// Function that return average of an array.\ndouble average(int a[], int n)\n{\n // Find sum of array element\n int sum = 0;\n for (int i=0; i<n; i++)\n sum += a[i];\n return sum/n;\n}\n\n// Driver code\nint main()\n{\n int arr[] = {10, 2, 3, 4, 5, 6, 7, 8, 9};\n int n = sizeof(arr)/sizeof(arr[0]);\n\n cout << average(arr, n) << endl;\n return 0;\n}","question":"Program to calculate average of array elements"},{"uid":1,"title":"Sum of Positive and Negative Numbers of Array","category":"array","code":"#include<iostream>\nusing namespace std;\n\nint main()\n{\n int a[100], n, i, psum=0, nsum=0;\n\n printf(\"Enter array size [1-100]: \");\n scanf(\"%d\",&n);\n printf(\"Enter %d elements: \",n);\n\n for(i=0; i<n; i++)\n {\n scanf(\"%d\",&a[i]);\n if(a[i]<0) nsum += a[i];\n else psum += a[i];\n }\n\n printf(\"All Positive numbers sum: %d\",psum);\n printf(\"\\nAll Negative numbers sum: %d\",nsum);\n printf(\"\\nTotal sum = %d\", psum+nsum);\n return 0;\n}\n","question":"Program to calculate sum of positive and negative numbers of Array"},{"uid":2,"title":" Maximum and Minimum element in array","category":"array","code":"/**\n * C program to find maximum and minimum element in array\n */\n\n#include <iostream>\n\nusing namespace std;\n\n#define MAX_SIZE 100 // Maximum array size\n\nint main()\n{\n int arr[MAX_SIZE];\n int i, max, min, size;\n\n /* Input size of the array */\n printf(\"Enter size of the array: \");\n scanf(\"%d\", &size);\n\n /* Input array elements */\n printf(\"Enter elements in the array: \");\n for(i=0; i<size; i++)\n {\n scanf(\"%d\", &arr[i]);\n }\n\n\n /* Assume first element as maximum and minimum */\n max = arr[0];\n min = arr[0];\n\n /*\n * Find maximum and minimum in all array elements.\n */\n for(i=1; i<size; i++)\n {\n /* If current element is greater than max */\n if(arr[i] > max)\n {\n max = arr[i];\n }\n\n /* If current element is smaller than min */\n if(arr[i] < min)\n {\n min = arr[i];\n }\n }\n\n /* Print maximum and minimum element */\n printf(\"Maximum element = %d\\\\n\", max);\n printf(\"Minimum element = %d\", min);\n\n return 0;\n}","question":"C program to find maximum and minimum element in array"},{"uid":3,"title":"2nd Largest Element in Array","category":"array","code":"// C program to find second largest\n// element in an array\n\n#include <bits/stdc++.h>\n#include <limits.h>\n\nusing namespace std;\n\n/* Function to print the second largest elements */\nvoid print2largest(int arr[], int arr_size)\n{\n int i, first, second;\n\n /* There should be atleast two elements */\n if (arr_size < 2)\n {\n printf(\" Invalid Input \");\n return;\n }\n\n first = second = INT_MIN;\n for (i = 0; i < arr_size ; i ++)\n {\n /* If current element is greater than first\n then update both first and second */\n if (arr[i] > first)\n {\n second = first;\n first = arr[i];\n }\n\n /* If arr[i] is in between first and\n second then update second */\n else if (arr[i] > second && arr[i] != first)\n second = arr[i];\n }\n if (second == INT_MIN)\n printf(\"There is no second largest element\\\\n\");\n else\n printf(\"The second largest element is %d\", second);\n}\n\n/* Driver program to test above function */\nint main()\n{\n int arr[] = {12, 35, 1, 10, 34, 1};\n int n = sizeof(arr)/sizeof(arr[0]);\n print2largest(arr, n);\n return 0;\n}","question":"C++ Program to find 2nd Largest Element in Array"},{"uid":4,"title":"Standard Deviation of Array","category":"array","code":"#include <iostream>\n#include <cmath>\nusing namespace std;\n\nfloat calculateSD(float data[]);\n\nint main()\n{\n int i;\n float data[10];\n\n cout << \"Enter 10 elements: \";\n for(i = 0; i < 10; ++i)\n cin >> data[i];\n\n cout << endl << \"Standard Deviation = \" << calculateSD(data);\n\n return 0;\n}\n\nfloat calculateSD(float data[])\n{\n float sum = 0.0, mean, standardDeviation = 0.0;\n\n int i;\n\n for(i = 0; i < 10; ++i)\n {\n sum += data[i];\n }\n\n mean = sum/10;\n\n for(i = 0; i < 10; ++i)\n standardDeviation += pow(data[i] - mean, 2);\n\n return sqrt(standardDeviation / 10);\n}","question":"C++ program to find Standard Deviation of an array"},{"uid":5,"title":"Transpose of a matrix","category":"array","code":"#include <iostream>\nusing namespace std;\n\nint main()\n{\n int a[10][10], trans[10][10], r, c, i, j;\n\n cout << \"Enter rows and columns of matrix: \";\n cin >> r >> c;\n\n // Storing element of matrix entered by user in array a[][].\n cout << endl << \"Enter elements of matrix: \" << endl;\n for(i = 0; i < r; ++i)\n for(j = 0; j < c; ++j)\n {\n cout << \"Enter elements a\" << i + 1 << j + 1 << \": \";\n cin >> a[i][j];\n }\n\n // Displaying the matrix a[][]\n cout << endl << \"Entered Matrix: \" << endl;\n for(i = 0; i < r; ++i)\n for(j = 0; j < c; ++j)\n {\n cout << \" \" << a[i][j];\n if(j == c - 1)\n cout << endl << endl;\n }\n\n // Finding transpose of matrix a[][] and storing it in array trans[][].\n for(i = 0; i < r; ++i)\n for(j = 0; j < c; ++j)\n {\n trans[j][i]=a[i][j];\n }\n\n // Displaying the transpose,i.e, Displaying array trans[][].\n cout << endl << \"Transpose of Matrix: \" << endl;\n for(i = 0; i < c; ++i)\n for(j = 0; j < r; ++j)\n {\n cout << \" \" << trans[i][j];\n if(j == r - 1)\n cout << endl << endl;\n }\n\n return 0;\n}","question":"C++ program to find transpose of a matrix"},{"uid":6,"title":"Multiply two matrices without using functions","category":"array","code":"#include <iostream>\nusing namespace std;\n\nint main()\n{\n int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;\n\n cout << \"Enter rows and columns for first matrix: \";\n cin >> r1 >> c1;\n cout << \"Enter rows and columns for second matrix: \";\n cin >> r2 >> c2;\n\n // If column of first matrix in not equal to row of second matrix,\n // ask the user to enter the size of matrix again.\n while (c1!=r2)\n {\n cout << \"Error! column of first matrix not equal to row of second.\";\n\n cout << \"Enter rows and columns for first matrix: \";\n cin >> r1 >> c1;\n\n cout << \"Enter rows and columns for second matrix: \";\n cin >> r2 >> c2;\n }\n\n // Storing elements of first matrix.\n cout << endl << \"Enter elements of matrix 1:\" << endl;\n for(i = 0; i < r1; ++i)\n for(j = 0; j < c1; ++j)\n {\n cout << \"Enter element a\" << i + 1 << j + 1 << \" : \";\n cin >> a[i][j];\n }\n\n // Storing elements of second matrix.\n cout << endl << \"Enter elements of matrix 2:\" << endl;\n for(i = 0; i < r2; ++i)\n for(j = 0; j < c2; ++j)\n {\n cout << \"Enter element b\" << i + 1 << j + 1 << \" : \";\n cin >> b[i][j];\n }\n\n // Initializing elements of matrix mult to 0.\n for(i = 0; i < r1; ++i)\n for(j = 0; j < c2; ++j)\n {\n mult[i][j]=0;\n }\n\n // Multiplying matrix a and b and storing in array mult.\n for(i = 0; i < r1; ++i)\n for(j = 0; j < c2; ++j)\n for(k = 0; k < c1; ++k)\n {\n mult[i][j] += a[i][k] * b[k][j];\n }\n\n // Displaying the multiplication of two matrix.\n cout << endl << \"Output Matrix: \" << endl;\n for(i = 0; i < r1; ++i)\n for(j = 0; j < c2; ++j)\n {\n cout << \" \" << mult[i][j];\n if(j == c2-1)\n cout << endl;\n }\n\n return 0;\n}","question":"Program to Multiply two Matrices without using functions"},{"uid":7,"title":"Hello World","category":"basic","code":"// Your First C++ Program\n\n#include <iostream>\nint main() {\n std::cout << \"Hello World!\";\n return 0;\n}","question":"Print Hello World"},{"uid":8,"title":"Sum of 2 numbers","category":"basic","code":"#include<iostream>\nusing namespace std;\nint main()\n{\nint a,b;\ncout<<\"Enter 2 numbers\";\ncin>>a>>b;\ncout<<\"Sum\"<<a+b;\nreturn 0;\n}","question":"Print Sum of two numbers"},{"uid":9,"title":"Swap 2 numbers without using Temporary Variable","category":"basic","code":"#include <iostream>\nusing namespace std;\n\nint main()\n{\n\n int a = 5, b = 10;\n\n cout << \"Before swapping.\" << endl;\n cout << \"a = \" << a << \", b = \" << b << endl;\n\n a = a + b;\n b = a - b;\n a = a - b;\n\n cout << \"After swapping.\" << endl;\n cout << \"a = \" << a << \", b = \" << b << endl;\n\n return 0;\n}","question":"Swap 2 numbers without using Temporary Variable"},{"uid":10,"title":"Swap 2 numbers using a Temporary Variable","category":"basic","code":"#include<iostream>\nusing namespace std;\nint main()\n{\n int a = 5, b = 10, temp;\n\n cout << \"Before swapping.\" << endl;\n cout << \"a = \" << a << \", b = \" << b << endl;\n\n temp = a;\n a = b;\n b = temp;\n\n cout << \"After swapping.\" << endl;\n cout << \"a = \" << a << \", b = \" << b << endl;\n\n return 0;\n}","question":"Swap 2 numbers using a Temporary Variable"},{"uid":11,"title":"Binary to Decimal","category":"conversion","code":"// C++ program to convert binary to decimal\n#include <iostream>\nusing namespace std;\n\n// Function to convert binary to decimal\nint binaryToDecimal(int n)\n{\n int num = n;\n int dec_value = 0;\n\n // Initializing base value to 1, i.e 2^0\n int base = 1;\n\n int temp = num;\n while (temp) {\n int last_digit = temp % 10;\n temp = temp / 10;\n\n dec_value += last_digit * base;\n\n base = base * 2;\n }\n\n return dec_value;\n}\n\n// Driver program to test above function\nint main()\n{\n int num = 10101001;\n\n cout << binaryToDecimal(num) << endl;\n}","question":"Convert Binary Number to Decimal"},{"uid":12,"title":"Deimal to Binary","category":"conversion","code":"// C++ program to convert a decimal number to binary number\n\n#include <iostream>\nusing namespace std;\n\n// function to convert decimal to binary\nvoid decToBinary(int n)\n{\n // array to store binary number\n int binaryNum[32];\n\n // counter for binary array\n int i = 0;\n while (n > 0) {\n\n // storing remainder in binary array\n binaryNum[i] = n % 2;\n n = n / 2;\n i++;\n }\n\n // printing binary array in reverse order\n for (int j = i - 1; j >= 0; j--)\n cout << binaryNum[j];\n}\n\n// Driver program to test above function\nint main()\n{\n int n = 17;\n decToBinary(n);\n return 0;\n}","question":"Convert Decimal Number to Binary"},{"uid":13,"title":"Octal to Decimal ","category":"conversion","code":"#include <iostream>\n#include <cmath>\nusing namespace std;\n\nint octalToDecimal(int octalNumber);\n\nint main()\n{\n int octalNumber;\n cout << \"Enter an octal number: \";\n cin >> octalNumber;\n cout << octalNumber << \" in octal = \" << octalToDecimal(octalNumber) << \" in decimal\";\n\n return 0;\n}\n\n// Function to convert octal number to decimal\nint octalToDecimal(int octalNumber)\n{\n int decimalNumber = 0, i = 0, rem;\n while (octalNumber != 0)\n {\n rem = octalNumber % 10;\n octalNumber /= 10;\n decimalNumber += rem * pow(8, i);\n ++i;\n }\n return decimalNumber;\n}","question":"Convert Octal number to Decimal"},{"uid":14,"title":"Decimal to Octal","category":"conversion","code":"#include <iostream>\n#include <cmath>\nusing namespace std;\n\nint decimalToOctal(int decimalNumber);\n\nint main()\n{\n int decimalNumber;\n cout << \"Enter a decimal number: \";\n cin >> decimalNumber;\n cout << decimalNumber << \" in decimal = \" << decimalToOctal(decimalNumber) << \" in octal\";\n\n return 0;\n}\n\n// Function to convert decimal number to octal\nint decimalToOctal(int decimalNumber)\n{\n int rem, i = 1, octalNumber = 0;\n while (decimalNumber != 0)\n {\n rem = decimalNumber % 8;\n decimalNumber /= 8;\n octalNumber += rem * i;\n i *= 10;\n }\n return octalNumber;\n}","question":"Convert Decimal number to Octal"},{"uid":15,"title":"Binary to Octal","category":"conversion","code":"#include <iostream>\n#include <cmath>\n\nusing namespace std;\n\nint convertBinarytoOctal(long long);\nint main()\n{\n long long binaryNumber;\n\n cout << \"Enter a binary number: \";\n cin >> binaryNumber;\n\n cout << binaryNumber << \" in binary = \" << convertBinarytoOctal(binaryNumber) << \" in octal \";\n\n return 0;\n}\n\nint convertBinarytoOctal(long long binaryNumber)\n{\n int octalNumber = 0, decimalNumber = 0, i = 0;\n\n while(binaryNumber != 0)\n {\n decimalNumber += (binaryNumber%10) * pow(2,i);\n ++i;\n binaryNumber/=10;\n }\n\n i = 1;\n\n while (decimalNumber != 0)\n {\n octalNumber += (decimalNumber % 8) * i;\n decimalNumber /= 8;\n i *= 10;\n }\n\n return octalNumber;\n}","question":"Convert Binary Number to Octal"},{"uid":16,"title":"Octal to Binary","category":"conversion","code":"#include <iostream>\n#include <cmath>\n\nusing namespace std;\n\nlong long convertOctalToBinary(int);\nint main()\n{\n int octalNumber;\n\n cout << \"Enter an octal number: \";\n cin >> octalNumber;\n\n cout << octalNumber << \" in octal = \" << convertOctalToBinary(octalNumber) << \"in binary\";\n\n return 0;\n}\n\nlong long convertOctalToBinary(int octalNumber)\n{\n int decimalNumber = 0, i = 0;\n long long binaryNumber = 0;\n\n while(octalNumber != 0)\n {\n decimalNumber += (octalNumber%10) * pow(8,i);\n ++i;\n octalNumber/=10;\n }\n\n i = 1;\n\n while (decimalNumber != 0)\n {\n binaryNumber += (decimalNumber % 2) * i;\n decimalNumber /= 2;\n i *= 10;\n }\n\n return binaryNumber;\n}","question":"Convert Octal Number to Binary"},{"uid":17,"title":"Centigrade to Fahrenheit","category":"conversion","code":"#include<iostream>\nusing namespace std;\n\nint main()\n{\n\tfloat cen, fah;\n\tcout<<\"Enter temperature in Celsius : \";\n\tcin>>cen;\n\tfah=(1.8 * cen) + 32;\n\tcout<<\"\\nTemperature in Fahrenheit = \"<<fah;\n}","question":"Convert Centigrade to Fahrenheit"},{"uid":18,"title":"Area of square , parallelogram , circle ,rectangle , triangle ","category":"math","code":"#include<iostream>\n#include<math.h>\nusing namespace std;\nint main()\n{\nint choice;\ncout<<\"Enter 1.for Triangle 2. for Square 3. for Circle 4. for Rectangle 5. for Parallelogram\";\ncin>>choice;\nswitch(choice)\n{\n case 1:\n {\n int a,b,c;\n float s,area;\n cout<<\"Enter 3 sides of triangle\";\n cin>>a>>b>>c;\n s=(float)(a+b+c)/2;\n area=(float)(sqrt(s*(s-a)*(s-b)*(s-c)));\n\n cout<<\"Area of Triangle with sides\"<<a<<b<<c<<\"is \"<<area;\n break;\n }\n case 2:\n {\n float side,area;\n cout<<\"Enter Sides of Square\";\n scanf(\"%f\",&side);\n area=(float)side*side;\n cout<<\"Area of Square is \"<<area;\n break;\n }\n case 3:\n {\n float radius,area;\n cout<<\"Enter Radius of Circle\";\n scanf(\"%f\",&radius);\n area=(float)3.14159*radius*radius;\n cout<<\"Area of Circle with radius\" << radius<<\"is\"<<area;\n break;\n }\n case 4:\n {\n float len,breadth,area;\n cout<<\"Enter Length and Breadth of Rectangle\";\n scanf(\"%f %f\",&len,&breadth);\n area=(float)len*breadth;\n cout<<\"Area of Rectangle is \"<<area;\n break;\n }\n case 5:\n {\n float base,height,area;\n cout<<(\"Enter base and height of Parallelogram\");\n cin>>base>>height;\n area=(float)base*height;\n cout<<\"Enter area of Parallelogram is \"<<area;\n break;\n }\n default:\n {\n cout<<\"Invalid Choice\"<<endl;\n break;\n }\n}\n}","question":"Calculate area of square , parallelogram , circle ,rectangle , triangle "},{"uid":19,"title":"Sine of a number mathematically","category":"math","code":"#include <iostream>\n#include <math.h>\n#include <stdlib.h>\nusing namespace std;\nint main()\n{\n int n, x1;\n float accuracy, term, denominator, x, sinx, sinval;\n\n printf(\"Enter the value of x (in degrees) \\\\n\");\n scanf(\"%f\", &x);\n x1 = x;\n /* Converting degrees to radians */\n x = x * (3.142 / 180.0);\n sinval = sin(x);\n printf(\"Enter the accuracy for the result \\\\n\");\n scanf(\"%f\", &accuracy);\n term = x;\n sinx = term;\n n = 1;\n do\n {\n denominator = 2 * n * (2 * n + 1);\n term = -term * x * x / denominator;\n sinx = sinx + term;\n n = n + 1;\n } while (accuracy <= fabs(sinval - sinx));\n printf(\"Sum of the sine series = %f \\\\n\", sinx);\n printf(\"Using Library function sin(%d) = %f\\\\n\", x1, sin(x));\n}","question":"Calculate sine of a number mathematically"},{"uid":20,"title":"Cosine of a number mathematically","category":"math","code":"#include <iostream>\n#include <math.h>\n#include <stdlib.h>\nusing namespace std;\nint main()\n{\n int n, x1;\n float accuracy, term, denominator, x, cosx, cosval;\n\n printf(\"Enter the value of x (in degrees) \\\\n\");\n scanf(\"%f\", &x);\n x1 = x;\n /* Converting degrees to radians */\n x = x * (3.142 / 180.0);\n cosval = cos(x);\n printf(\"Enter the accuracy for the result \\\\n\");\n scanf(\"%f\", &accuracy);\n term = 1;\n cosx = term;\n n = 1;\n do\n {\n denominator = 2 * n * (2 * n - 1);\n term = -term * x * x / denominator;\n cosx = cosx + term;\n n = n + 1;\n } while (accuracy <= fabs(cosval - cosx));\n printf(\"Sum of the cosine series = %f'\\\\n\", cosx);\n printf(\"Using Library function cos(%d) = %f/\\n\", x1, cos(x));\n}","question":"Calculate cosine of a number mathematically"},{"uid":21,"title":"Quadrant of a given point","category":"math","code":"// CPP program to check quadrant\n#include <bits/stdc++.h>\nusing namespace std;\n\n// Function to check quadrant\nvoid quadrant(int x, int y)\n{\n\n if (x > 0 and y > 0)\n cout << \"lies in First quadrant\";\n\n else if (x < 0 and y > 0)\n cout << \"lies in Second quadrant\";\n\n else if (x < 0 and y < 0)\n cout << \"lies in Third quadrant\";\n\n else if (x > 0 and y < 0)\n cout << \"lies in Fourth quadrant\";\n\n else if (x == 0 and y > 0)\n cout << \"lies at positive y axis\";\n\n else if (x == 0 and y < 0)\n cout << \"lies at negative y axis\";\n\n else if (y == 0 and x < 0)\n cout << \"lies at negative x axis\";\n\n else if (y == 0 and x > 0)\n cout << \"lies at positive x axis\";\n\n else\n cout << \"lies at origin\";\n}\n\n// Driver code\nint main()\n{\n int x ,y;\n cin>>x>>y;\n // Function call\n quadrant(x, y);\n return 0;\n}","question":"Program to print quadrant of a given point"},{"uid":22,"title":"Roots of a Quadratic Equation","category":"math","code":"#include <iostream>\n#include <cmath>\nusing namespace std;\n\nint main() {\n\n float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;\n cout << \"Enter coefficients a, b and c: \";\n cin >> a >> b >> c;\n discriminant = b*b - 4*a*c;\n\n if (discriminant > 0) {\n x1 = (-b + sqrt(discriminant)) / (2*a);\n x2 = (-b - sqrt(discriminant)) / (2*a);\n cout << \"Roots are real and different.\" << endl;\n cout << \"x1 = \" << x1 << endl;\n cout << \"x2 = \" << x2 << endl;\n }\n\n else if (discriminant == 0) {\n cout << \"Roots are real and same.\" << endl;\n x1 = (-b + sqrt(discriminant)) / (2*a);\n cout << \"x1 = x2 =\" << x1 << endl;\n }\n\n else {\n realPart = -b/(2*a);\n imaginaryPart =sqrt(-discriminant)/(2*a);\n cout << \"Roots are complex and different.\" << endl;\n cout << \"x1 = \" << realPart << \"+\" << imaginaryPart << \"i\" << endl;\n cout << \"x2 = \" << realPart << \"-\" << imaginaryPart << \"i\" << endl;\n }\n\n return 0;\n}","question":"Print all roots of a Quadratic Equation"},{"uid":23,"title":"Simple Calculator","category":"math","code":"# include <iostream>\nusing namespace std;\n\nint main()\n{\n char op;\n float num1, num2;\n\n cout << \"Enter operator either + or - or * or /: \";\n cin >> op;\n\n cout << \"Enter two operands: \";\n cin >> num1 >> num2;\n\n switch(op)\n {\n case '+':\n cout << num1+num2;\n break;\n\n case '-':\n cout << num1-num2;\n break;\n\n case '*':\n cout << num1*num2;\n break;\n\n case '/':\n cout << num1/num2;\n break;\n\n default:\n // If the operator is other than +, -, * or /, error message is shown\n cout << \"Error! operator is not correct\";\n break;\n }\n\n return 0;\n}","question":"Simple Calculator"},{"uid":24,"title":"Simple Interest calculator","category":"math","code":"#include<iostream>\nusing namespace std;\nint main()\n{\n float p,r,t,simpleInterest;\n int choice;\n printf(\"Enter\\\\n1 to find out Simple Interest\\\\n2 to find out Principal\\\\n3 to find out rate in \\\\n4 to find out Time\\\\n\");\n scanf(\"%d\",&choice);\n switch(choice)\n {\n case 1:{\n printf(\"Enter Pricipal\\\\n\");\n scanf(\"%f\",&p);\n printf(\"Enter Rate in percentage \\\\n\");\n scanf(\"%f\",&r);\n printf(\"Enter Time in years(decimals)\\\\n\");\n scanf(\"%f\",&t);\n simpleInterest=(float)(p*r*t)/100.0;\n printf(\"Simple Interest is %f\\\\n\",simpleInterest);\n break;\n }\n case 2:{\n printf(\"Enter Simple Interest\\\\n\");\n scanf(\"%f\",&simpleInterest);\n printf(\"Enter Rate in percentage\\\\n\");\n scanf(\"%f\",&r);\n printf(\"Enter Time in years(decimals)\\\\n\");\n scanf(\"%f\",&t);\n p=(float)(100.0*simpleInterest)/(r*t);\n printf(\"Principal is %f\\\\n\",p);\n break;\n }\n case 3:{\n printf(\"Enter Simple Interest\\\\n\");\n scanf(\"%f\",&simpleInterest);\n printf(\"Enter Principal\\\\n\");\n scanf(\"%f\",&p);\n printf(\"Enter Time in years(decimals)\\\\n\");\n scanf(\"%f\",&t);\n r=(float)(100.0*simpleInterest)/(p*t);\n printf(\"Rate is %f\\\\n\",r);\n break;\n }\n case 4:{\n printf(\"Enter Simple Interest\\\\n\");\n scanf(\"%f\",&simpleInterest);\n printf(\"Enter Principal\\\\n\");\n scanf(\"%f\",&p);\n printf(\"Enter Rate in percentage\\\\n\");\n scanf(\"%f\",&r);\n t=(float)((100.0*simpleInterest)/(p*r));\n printf(\"Time is %f years\\\\n\",t);\n break;\n }\n default:cout<<\"Wrong choice\";\n }\n}","question":"Simple Interest calculator"},{"uid":25,"title":"Sum of first n numbers","category":"numbers","code":"#include<iostream>\n#include<iostream>\nusing namespace std;\nint main()\n{\n\tint n,sum=0;\n\tcout<<\"Enter number till which you would like to add\";\n\tcin>>n;\n\twhile(n>0)\n\t{\n\t\tsum+=n;\n\t\tn--;\n\t}\n\tcout<<\"\\n sum is:\"<<sum;\n\treturn 0;\n}","question":"Print Sum of First n numbers"},{"uid":26,"title":"Sum of digits of a number","category":"numbers","code":"#include<iostream>\nusing namespace std;\n\nint main()\n{\n int val, num, sum = 0;\n\n cout << \"Enter the number : \";\n cin >> val;\n num = val;\n while (num != 0)\n {\n sum = sum + num % 10;\n num = num / 10;\n }\n cout << \"The sum of the digits of \"<< val << \" is \" << sum;\n}","question":"Print Sum of digits of a number"},{"uid":27,"title":"Reverse a number","category":"numbers","code":"#include <iostream>\nusing namespace std;\n\nint main()\n{\n int n, reversedNumber = 0, remainder;\n\n cout << \"Enter an integer: \";\n cin >> n;\n\n while(n != 0)\n {\n remainder = n%10;\n reversedNumber = reversedNumber*10 + remainder;\n n /= 10;\n }\n\n cout << \"Reversed Number = \" << reversedNumber;\n\n return 0;\n}","question":"Print reverse of a number"},{"uid":28,"title":"Check Palindrome or not","category":"numbers","code":"\n#include <iostream>\nusing namespace std;\n\nint main()\n{\n int n, num, digit, rev = 0;\n\n cout << \"Enter a positive number: \";\n cin >> num;\n if(num<0){\n cout<<\"You Entered negative number\";\n return 0;\n } \n n = num;\n\n do\n {\n digit = num % 10;\n rev = (rev * 10) + digit;\n num = num / 10;\n } while (num != 0);\n\n cout << \" The reverse of the number is: \" << rev << endl;\n\n if (n == rev)\n cout << \" The number is a palindrome.\";\n else\n cout << \" The number is not a palindrome.\";\n\n return 0;\n}","question":"Program to check if a given number is palindrome or not "},{"uid":29,"title":"Sum of all numbers divisible by 2 between 2 numbers","category":"numbers","code":"#include <iostream>\nusing namespace std;\n\nint main()\n{\n int a,b, sum = 0;\n cout << \"Enter lower bound and upper bound\";\n cin>>a>>b;\n for (i = a; i < b; i++)\n {\n if (i % 2 == 0)\n {\n sum += i;\n }\n }\n cout << \"The sum : \" << sum << endl;\n}","question":"Program to print sum of all numbers divisible by 2 between 2 numbers"},{"uid":30,"title":"Factorial of a number","category":"numbers","code":"#include <iostream>\nusing namespace std;\n\nint main()\n{\n unsigned int n;\n unsigned long long factorial = 1;\n\n cout << \"Enter a positive integer: \";\n cin >> n;\n\n for(int i = 1; i <=n; ++i)\n {\n factorial *= i;\n }\n\n cout << \"Factorial of \" << n << \" = \" << factorial;\n return 0;\n}","question":"Print factorial of a number"},{"uid":31,"title":"Fibonacci Series","category":"numbers","code":"#include <iostream>\nusing namespace std;\nint main() {\n int n1=0,n2=1,n3,i,number;\n cout<<\"Enter the number of elements: \";\n cin>>number;\n cout<<n1<<\" \"<<n2<<\" \"; //printing 0 and 1\n for(i=2;i<number;++i)\n {\n n3=n1+n2;\n cout<<n3<<\" \";\n n1=n2;\n n2=n3;\n }\n return 0;\n}","question":"Print Fibonacci Series"},{"uid":32,"title":"Sum of Even Number using Recursion","category":"recursion","code":"#include<iostream>\nusing namespace std;\n\nvoid SumOfEven(int a[],int num,int sum);\nint main()\n{\n int i,a[100],num,sum=0;\n cout<<\"Enter number of Array Elements\";\n cin>>num;\n cout<<\"Enter Array Elements\" ;\n for(i=0;i<num;i++)\n cin>>a[i];\n SumOfEven(a,num-1,sum);\n}\n\nvoid SumOfEven(int a[],int num,int sum)\n{\n\n if(num>=0)\n {\n if((a[num])%2==0)\n {\n sum+=(a[num]);\n }\n SumOfEven(a,num-1,sum);\n }\n else\n {\n cout<<\"Sum=\"<<sum;\n return;\n }\n}","question":"Print sum of Even Number using Recursion"},{"uid":33,"title":"Binary Search using recursion","category":"recursion","code":"#include<iostream>\nusing namespace std;\n\nint binarySearch(int arr[], int l, int r, int x)\n{\n if (r >= l)\n {\n int mid = l + (r - l)/2;\n\n // If the element is present at the middle itself\n if (arr[mid] == x) return mid;\n\n // If element is smaller than mid, then it can only be present\n // in left subarray\n if (arr[mid] > x) return binarySearch(arr, l, mid-1, x);\n\n // Else the element can only be present in right subarray\n return binarySearch(arr, mid+1, r, x);\n }\n\n // We reach here when element is not present in array\n return -1;\n}\n\nint main(void)\n{\n int n ;\n cout<<\"Enter no of elements\";\n cin>>n;\n int a[n];\n for(int i=0; i <n;i++){\n cin>>a[i];\n }\n int x ;\n cout<<\"Enter element to be Searched\";\n cin>>x;\n int result = binarySearch(a, 0, n-1, x);\n (result == -1)? printf(\"Element is not present in array\")\n : printf(\"Element is present at index %d\", result);\n return 0;\n}","question":"Perform Binary Search an element using recursion"},{"uid":34,"title":"Bubble Sort Array using recursion","category":"recursion","code":"\n// C/C++ program for recursive implementation\n// of Bubble sort\n#include <bits/stdc++.h>\nusing namespace std;\n\n// A function to implement bubble sort\nvoid bubbleSort(int arr[], int n)\n{\n\t// Base case\n\tif (n == 1)\n\t\treturn;\n\n\t// One pass of bubble sort. After\n\t// this pass, the largest element\n\t// is moved (or bubbled) to end.\n\tfor (int i=0; i<n-1; i++)\n\t\tif (arr[i] > arr[i+1])\n\t\t\tswap(arr[i], arr[i+1]);\n\n\t// Largest element is fixed,\n\t// recur for remaining array\n\tbubbleSort(arr, n-1);\n}\n\n\n// Driver program to test above functions\nint main()\n{\n\tint arr[] = {64, 34, 25, 12, 22, 11, 90};\n\tint n = sizeof(arr)/sizeof(arr[0]);\n\tbubbleSort(arr, n);\n\tcout<<\"Sorted array :\"<<endl;\n\tfor (int i=0; i < n; i++)\n cout<<arr[i]<<\" \";\n cout<<endl;\n\treturn 0;\n}","question":"Perform Bubble Sort on a Array using recursion"},{"uid":35,"title":"Check Repeating digits using recursion","category":"recursion","code":"#include<iostream>\n\nusing namespace std;\n\nbool check(int n, int mask)\n{\n // base case: if we have checked all the digits and didn't find any duplicates, return false\n if (n == 0)\n return false;\n\n /*\n p is the place of the last digit in n (n%10).\n A digit can range from 0 to 9.\n The place of 0 will be 1 << 0 which is 1.\n The place of 1 will be 1 << 1 which is 2.\n The place of 2 will be 1 << 2 which is 4.\n ...\n ...\n The place of 9 will be 1 << 9 which is 512.\n */\n int p = 1 << (n % 10);\n\n // if place of p has already been marked then it's a duplicate\n if (mask&p)\n return true;\n\n // otherwise scrap the last digit (n/10), mark place p and recurse on the remaining digits\n return check(n / 10, mask|p);\n}\nint main()\n{\n int num,k,temp,frequency[9],flag=0,i;\n cout<<\"Enter number to find which digits are repeated\"<<endl;\n cin>>num;\n if(check(num,0))\n cout<<\"Repeated Digits are there\";\n else\n cout<<\"No Repeated digits are there\";\n\n}","question":"Check if there are Repeating digits in a number using recursion"},{"uid":36,"title":"Smallest element of array using recursion","category":"recursion","code":"// Recursive C++ program to find minimum\n\n#include <iostream>\nusing namespace std;\n\n// function to print Minimum element using recursion\nint findMinRec(int A[], int n)\n{\n // if size = 0 means whole array has been traversed\n if (n == 1)\n return A[0];\n return min(A[n-1], findMinRec(A, n-1));\n}\n\n// driver code to test above function\nint main()\n{\n int A[] = {1, 4, 45, 6, -50, 10, 2};\n int n = sizeof(A)/sizeof(A[0]);\n cout << findMinRec(A, n);\n return 0;\n}","question":"Find Smallest element of array using recursion"},{"uid":37,"title":"Factorial of a number using recursion","category":"recursion","code":"// C++ program to find factorial of given number\n#include <iostream>\nusing namespace std;\n\n// function to find factorial of given number\nunsigned int factorial(unsigned int n)\n{\n if (n == 0)\n return 1;\n return n * factorial(n - 1);\n}\n\n// Driver code\nint main()\n{\n int num = 5;\n cout << \"Factorial of \"\n << num << \" is \" << factorial(num) << endl;\n return 0;\n}","question":"Find Factorial of a number using recursion"},{"uid":38,"title":"GCD of two numbers using recursion","category":"recursion","code":"// C++ program to find GCD of two numbers\n#include <iostream>\n\nusing namespace std;\n// Recursive function to return gcd of a and b\nint gcd(int a, int b)\n{\n // Everything divides 0\n if (a == 0)\n return b;\n if (b == 0)\n return a;\n\n // base case\n if (a == b)\n return a;\n\n // a is greater\n if (a > b)\n return gcd(a-b, b);\n return gcd(a, b-a);\n}\n\n// Driver program to test above function\nint main()\n{\n int a ,b;\n cout<<\"Enter numbers\"<<endl;\n cin>>a>>b;\n cout<<\"GCD of \"<<a<<\" and \"<<b<<\" is \"<<gcd(a, b);\n return 0;\n}","question":"Find GCD of two numbers using recursion"},{"uid":39,"title":"Sum of the series 1 - X^2/2! + X^4/4!-....","category":"sumseries","code":"#include <iostream>\n#include <math.h>\nusing namespace std;\n\nint main()\n{\n float x, sum, term, fct, y, j, m;\n int i, n;\n y = 2;\n\n cout << \"\\\\n\\\\n Find the sum of the series 1 - X^2/2! + X^4/4!-....:\\\\n\";\n cout << \"---------------------------------------------------------\\\\n\";\n cout << \" Input the value of X: \";\n cin >> x;\n cout << \" Input the value for nth term: \";\n cin >> n;\n sum = 1;\n term = 1;\n cout << \" term 1 value is: \" << term << endl;\n for (i = 1; i < n; i++)\n {\n fct = 1;\n for (j = 1; j <= y; j++)\n {\n fct = fct * j;\n }\n term = term * (-1);\n m = pow(x, y) / fct;\n m = m * term;\n cout << \" term \" << i + 1 << \" value is: \" << m << endl;\n sum = sum + m;\n y += 2;\n }\n cout << \" The sum of the above series is: \" << sum << endl;\n}","question":"Find the sum of the series 1 - X^2/2! + X^4/4!-...."},{"uid":40,"title":"Sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)","category":"sumseries","code":"#include <iostream>\nusing namespace std;\n\nint main()\n{\n int i, n, sum = 0;\n cout << \"\\\\n\\\\n Find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n):\\\\n\";\n cout << \"------------------------------------------------------------------------------------\\\\n\";\n cout << \" Input the value for nth term: \";\n cin >> n;\n\n for (i = 1; i <= n; i++)\n\t{\n sum += i * i;\n cout << i << \"*\" << i << \" = \" << i * i << endl;\n }\n cout << \" The sum of the above series is: \" << sum << endl;\n}","question":"Find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)"},{"uid":41,"title":"Sum of Series 1/1! + 2/2! + 3/3! + 4/4! + ... N/N!","category":"sumseries","code":"#include <bits/stdc++.h>\nusing namespace std;\n\n/*function to calculate sum of given series*/\ndouble sumOfSeries(double num)\n{\n double res = 0, fact = 1;\n for (int i = 1; i <= num; i++) {\n /*fact variable store factorial of the i.*/\n fact = fact * i;\n\n res = res + (i / fact);\n }\n return (res);\n}\n\n/*Driver Function*/\nint main()\n{\n double n ;\n cout<<\"Enter value of n\";\n cin>>n;\n cout << \"Sum: \" << sumOfSeries(n);\n return 0;\n}","question":"Find sum of series 1/1! + 2/2! + 3/3! + 4/4! + ... N/N!"},{"uid":42,"title":"Sum of Series 1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N","category":"sumseries","code":"#include<iostream>\nusing namespace std;\n\nint main()\n{\n\tint i,n;\n\tfloat sum=0;\n\n\tcout<<\"Enter the value of n \";\n\tcin>>n;\n\n\tfor(i=1;i<=n;i++)\n\t\tsum += 1.0/i;\n\n\tcout<<\"Sum : \"<<sum;\n\n\n\treturn 0;\n}","question":"Find Sum of Series 1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N"},{"uid":43,"title":"Sum of Series 1+2+3+……+n","category":"sumseries","code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint main()\n{\n int i,n,sum=0;\n cout<<\"1+2+3+……+n\";\n cout<<\"\\nEnter the value of n:\";\n cin>>n;\n\n for(i=1;i<=n;++i)\n sum+=i;\n cout<<\"\\nSum=\"<<sum;\n}","question":"Find sum of series 1+2+3+……+n"},{"uid":44,"title":"Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2","category":"sumseries","code":"// Program to find sum of series\n// 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2.\n#include <bits/stdc++.h>\nusing namespace std;\n\n// Function to find sum of series.\nint sumOfSeries(int n)\n{\n int sum = 0;\n for (int i = 1; i <= n; i++)\n sum = sum + (2 * i - 1) * (2 * i - 1);\n return sum;\n}\n\n// Driver code\nint main()\n{\n int n = 10;\n\n cout << sumOfSeries(n);\n\n return 0;\n}","question":"Find Sum of Series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2"},{"uid":45,"title":"Sum of Series 1 + x^2/2 + x^3/3 + ....+ x^n/n","category":"sumseries","code":"// C++ program to find sum of series\n// 1 + x^2/2 + x^3/3 + ....+ x^n/n\n#include <bits/stdc++.h>\nusing namespace std;\n\n// C++ code to print the sum\n// of the series\ndouble sum(int x, int n)\n{\n double i, total = 1.0, multi = x;\n for (i = 1; i <= n; i++)\n {\n total = total + multi / i;\n multi = multi * x;\n }\n return total;\n}\n\n// Driver code\nint main()\n{\n int x = 2;\n int n = 5;\n cout << fixed << setprecision(2) << sum(x, n);\n return 0;\n}","question":"Find Sum of Series 1 + x^2/2 + x^3/3 + ....+ x^n/n"},{"uid":46,"title":"Linear Search","category":"sands","code":"#include <bits/stdc++.h>\nusing namespace std;\n\n// Linearly search x in arr[]. If x is present then return its\n// location, otherwise return -1\nint search(int arr[], int n, int x)\n{\n int i;\n for (i = 0; i < n; i++)\n if (arr[i] == x)\n return i;\n return -1;\n}\n\n// Driver code\nint main()\n{\n int arr[] = { 3, 4, 1, 7, 5 };\n int n = sizeof(arr) / sizeof(arr[0]);\n int x = 4;\n\n int index = search(arr, n, x);\n if (index == -1)\n cout << \"Element is not present in the array\";\n else\n cout << \"Element found at position \" << index;\n\n return 0;\n}","question":"Find a element using Linear Search"},{"uid":47,"title":"Binary Search","category":"sands","code":"#include <bits/stdc++.h>\nusing namespace std;\n\n// A iterative binary search function. It returns\n// location of x in given array arr[l..r] if present,\n// otherwise -1\nint binarySearch(int arr[], int l, int r, int x)\n{\n while (l <= r) {\n int m = l + (r - l) / 2;\n\n // Check if x is present at mid\n if (arr[m] == x)\n return m;\n\n // If x greater, ignore left half\n if (arr[m] < x)\n l = m + 1;\n\n // If x is smaller, ignore right half\n else\n r = m - 1;\n }\n\n // if we reach here, then element was\n // not present\n return -1;\n}\n\nint main(void)\n{\n int arr[] = { 2, 3, 4, 10, 40 };\n int x = 10;\n int n = sizeof(arr) / sizeof(arr[0]);\n int result = binarySearch(arr, 0, n - 1, x);\n (result == -1) ? cout << \"Element is not present in array\"\n : cout << \"Element is present at index \" << result;\n return 0;\n}","question":"Perform Binary Search on a array "},{"uid":48,"title":"Jump Search","category":"sands","code":"// C++ program to implement Jump Search\n//Time Complexity : O(√n)\n//Auxiliary Space : O(1)\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nint jumpSearch(int arr[], int x, int n)\n{\n\t// Finding block size to be jumped\n\tint step = sqrt(n);\n\n\t// Finding the block where element is\n\t// present (if it is present)\n\tint prev = 0;\n\twhile (arr[min(step, n)-1] < x)\n\t{\n\t\tprev = step;\n\t\tstep += sqrt(n);\n\t\tif (prev >= n)\n\t\t\treturn -1;\n\t}\n\n\t// Doing a linear search for x in block\n\t// beginning with prev.\n\twhile (arr[prev] < x)\n\t{\n\t\tprev++;\n\n\t\t// If we reached next block or end of\n\t\t// array, element is not present.\n\t\tif (prev == min(step, n))\n\t\t\treturn -1;\n\t}\n\t// If element is found\n\tif (arr[prev] == x)\n\t\treturn prev;\n\n\treturn -1;\n}\n\n// Driver program to test function\nint main()\n{\n\tint arr[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21,\n\t\t\t\t34, 55, 89, 144, 233, 377, 610 };\n\tint x = 55;\n\tint n = sizeof(arr) / sizeof(arr[0]);\n\n\t// Find the index of 'x' using Jump Search\n\tint index = jumpSearch(arr, x, n);\n\n\t// Print the index where 'x' is located\n\tcout << endl<<\"Number \" << x << \" is at index \" << index;\n\treturn 0;\n}","question":"Implement Jump Search"},{"uid":49,"title":"Bubble Sort","category":"sands","code":"// C++ program for implementation of Bubble sort\n#include <bits/stdc++.h>\nusing namespace std;\n\nvoid swap(int *xp, int *yp)\n{\n int temp = *xp;\n *xp = *yp;\n *yp = temp;\n}\n\n// A function to implement bubble sort\nvoid bubbleSort(int arr[], int n)\n{\n int i, j;\n for (i = 0; i < n-1; i++)\n\n // Last i elements are already in place\n for (j = 0; j < n-i-1; j++)\n if (arr[j] > arr[j+1])\n swap(&arr[j], &arr[j+1]);\n}\n\n/* Function to print an array */\nvoid printArray(int arr[], int size)\n{\n int i;\n for (i = 0; i < size; i++)\n cout << arr[i] << \" \";\n cout << endl;\n}\n\n// Driver code\nint main()\n{\n int arr[] = {64, 34, 25, 12, 22, 11, 90};\n int n = sizeof(arr)/sizeof(arr[0]);\n bubbleSort(arr, n);\n cout<<\"Sorted array: \\\\n\";\n printArray(arr, n);\n return 0;\n}","question":"Sort an array using bubble sort"},{"uid":50,"title":"Selection Sort","category":"sands","code":"// C++ program for implementation of selection sort\n#include <bits/stdc++.h>\nusing namespace std;\n\nvoid swap(int *xp, int *yp)\n{\n int temp = *xp;\n *xp = *yp;\n *yp = temp;\n}\n\nvoid selectionSort(int arr[], int n)\n{\n int i, j, min_idx;\n\n // One by one move boundary of unsorted subarray\n for (i = 0; i < n-1; i++)\n {\n // Find the minimum element in unsorted array\n min_idx = i;\n for (j = i+1; j < n; j++)\n if (arr[j] < arr[min_idx])\n min_idx = j;\n\n // Swap the found minimum element with the first element\n swap(&arr[min_idx], &arr[i]);\n }\n}\n\n/* Function to print an array */\nvoid printArray(int arr[], int size)\n{\n int i;\n for (i=0; i < size; i++)\n cout << arr[i] << \" \";\n cout << endl;\n}\n\n// Driver program to test above functions\nint main()\n{\n int arr[] = {64, 25, 12, 22, 11};\n int n = sizeof(arr)/sizeof(arr[0]);\n selectionSort(arr, n);\n cout << \"Sorted array: \\\\n\";\n printArray(arr, n);\n return 0;\n}","question":"Sort an array using selection sort"},{"uid":51,"title":"Insertion Sort","category":"sands","code":"// C++ program for insertion sort\n#include <bits/stdc++.h>\nusing namespace std;\n\n/* Function to sort an array using insertion sort*/\nvoid insertionSort(int arr[], int n)\n{\n int i, key, j;\n for (i = 1; i < n; i++)\n {\n key = arr[i];\n j = i - 1;\n\n /* Move elements of arr[0..i-1], that are\n greater than key, to one position ahead\n of their current position */\n while (j >= 0 && arr[j] > key)\n {\n arr[j + 1] = arr[j];\n j = j - 1;\n }\n arr[j + 1] = key;\n }\n}\n\n// A utility function to print an array of size n\nvoid printArray(int arr[], int n)\n{\n int i;\n for (i = 0; i < n; i++)\n cout << arr[i] << \" \";\n cout << endl;\n}\n\n/* Driver code */\nint main()\n{\n int arr[] = { 12, 11, 13, 5, 6 };\n int n = sizeof(arr) / sizeof(arr[0]);\n\n insertionSort(arr, n);\n printArray(arr, n);\n\n return 0;\n}","question":"Sort an array using insertion Sort"}]