Advertisement
Shailrshah

Euclidean algorithm for GCD

Jul 9th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.43 KB | None | 0 0
  1. #include <stdio.h>
  2. void swap(int *a, int *b){
  3.     int temp = *a;
  4.     *a = *b;
  5.     *b = temp;
  6. }
  7. int gcd(int a, int b){
  8.     if(a < b) swap(&a, &b);
  9.     int r = a % b;
  10.     printf("\n%d\t=\t%d\t*\t%d\t+\t%d", a, a/b, b, r);
  11.     if (r == 0) return b;
  12.     gcd (b, r);
  13. }
  14. int main(){
  15.     printf("Enter two numbers to find out their GCD:-\n");
  16.     int a, b;
  17.     scanf("%d", &a);
  18.     scanf("%d", &b);
  19.     printf("\n\nSo, %d is the GCD of %d and %d\n", gcd(a, b), a, b);
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement