1.DS - Lab -C Program to find GCD using recursive function(Part A)

1. C Program to find GCD using recursive function(Part A)


ALGORITHM : 

Step 1: Start
Step 2: Declare a recursive function gcd_recursive(a, b).
Step 3: If b is equal to 0, return a as the GCD.
Step 4: Otherwise, return the result of gcd_recursive(b, a % b).
Step 5: Input the two numbers, num1 and num2.
Step 6: Call the gcd_recursive function with num1 and num2 as arguments, and store the result in a variable (result).
Step 7: Display the result as the GCD.
Step 8: End


SOURCE CODE :

#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 positive integers: ");
    scanf("%d %d", &num1, &num2);

    int result = gcd(num1, num2);
    printf("GCD of %d and %d is: %d\n", num1, num2, result);

    return 0;
}


OUTPUT : We are Using Turbo C for execution ..

Enter two positive integers: 24 18
GCD of 24 and 18 is: 6

OUTPUT 1.


OUTPUT 2 .


OUTPUT 3.






No comments:

Post a Comment