✅ Day 4 solution - DSA with C

 Find the GCD of Two Numbers Using Recursion

GCD (Greatest Common Divisor) using Euclidean Algorithm:


#include <stdio.h> int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int num1, num2; printf("Enter two numbers: "); scanf("%d %d", &num1, &num2); printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2)); return 0; }

✅ 2. Recursive Function to Calculate Power (xⁿ)


#include <stdio.h> int power(int x, int n) { if (n == 0) return 1; return x * power(x, n - 1); } int main() { int base, exponent; printf("Enter base and exponent: "); scanf("%d %d", &base, &exponent); printf("%d^%d = %d\n", base, exponent, power(base, exponent)); return 0; }

✅ 3. Reverse an Array Using Recursion


#include <stdio.h> void reverse(int arr[], int start, int end) { if (start >= end) return; // Swap int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; reverse(arr, start + 1, end - 1); } int main() { int arr[100], n; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements:\n", n); for (int i = 0; i < n; i++) scanf("%d", &arr[i]); reverse(arr, 0, n - 1); printf("Reversed array:\n"); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }

Comments

Popular posts from this blog

Raster scan Vs Vector Scan

Inheritance

unit -1 Introduction of Image processing