#includeusing namespace std; void bubbleSort(int arr[], int n) { int i, j; for (i = 0; i < n-1; i++) for (j = 0; j < n-i-1; j++) if (arr[j] > arr[j+1]) swap(arr[j], arr[j+1]); } int main() { int arr[] = {64, 34, 25, 12, 22, 11, 90}; int n = sizeof(arr)/sizeof(arr[0]); bubbleSort(arr, n); cout << "Sorted array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; return 0; }
#includeThis example demonstrates how to use recursion to find the factorial of a number. The code defines the `factorial()` function, which checks if `n` is equal to 0 and returns 1 if it is. Otherwise, it multiplies `n` by the result of calling `factorial()` with `n-1`. The `main()` function then calls `factorial()` with a value of 5, prints the result to the screen, and exits. This example does not make use of any external package libraries.using namespace std; int factorial(int n) { if (n == 0) return 1; return n * factorial(n-1); } int main() { int num = 5; int fact = factorial(num); cout << "Factorial of " << num << " is " << fact << endl; return 0; }