Asked by
AL MaMun (4 Golds)
Friday, 31 Jul 2020, 04:29 PM
at (Education
Tutorials)
|
|
|
|
GCD and LCM using recursion #include<stdio.h> int gcd(int a, int b) { if(a%b==0) return b; return gcd(b, a%b); } int main() { int a, b, result; printf("Enter a and b\n"); scanf("%d%d", &a, &b); result=gcd(a,b); printf("GCD = %d\n", result); printf("LCM = %d", (a*b)/result); return 0; } |
#include <stdio.h> int main() { int a, b, x, gcd; scanf("%d %d", &a, &b); if (a < b) { x = a; } else { x = b; } for(; x >= 1; x--) { if (a % x == 0 && b % x == 0) { gcd = x; break; } } printf("GCD is %d\n", gcd); return 0; } |
A C program to find GCD #include <stdio.h> int main() { int a, b, t, x, gcd; scanf("%d %d", &a, &b); if (a == 0) gcd = a; else if (b == 0) gcd = b; else { while (b != 0) { t = b; b = a % b; a = t; } gcd = a; } printf("GCD is %d\n", gcd); return 0; } |