Write a program to find GCD of two number.
using function
Using recursion
using function
- #include<stdio.h>
- int find_gcd(int n1, int n2);
- int main() {
- int n1, n2;
- printf("Enter two numbers to find GCD : ");
- scanf("%d %d", & n1, & n2);
- printf("\n%d and %d of GCD is: %d\n\n", n1, n2, find_gcd(n1, n2));
- return 0;
- }
- int find_gcd(int n1, int n2) {
- if (n2 == 0)
- return n1;
- else
- return find_gcd(n2, n1 % n2);
- }
Using recursion
- #include<stdio.h>
- int find_gcd(int , int );
- int main()
- {
- int n1, n2, gcd;
- printf("Enter two numbers to find GCD : ");
- scanf("%d%d", &n1, &n2);
- gcd = find_gcd(n1, n2);
- printf("\n%d and %d of GCD is: %d\n\n", n1, n2, gcd);
- return 0;
- }
- int find_gcd(int x, int y)
- {
- if(x > y)
- find_gcd(x-y, y);
- else if(y > x)
- find_gcd(x, y-x);
- else
- return x;
- }
একটি মন্তব্য পোস্ট করুন