Advertisement
informaticage

Euclidean algorithm for gcd

May 27th, 2021
1,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.33 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int mcd(int n, int m);
  4. int main(void) {
  5.   int n, m;
  6.   printf("Inserire n, m: ");
  7.   scanf("%d%d", &n, &m);
  8.  
  9.   printf("MCD: %d", mcd(n, m));
  10.   return 0;
  11. }
  12.  
  13. int mcd(int n, int m) {
  14.   int balance, a = n, b = m;
  15.  
  16.   while (b != 0) {
  17.     balance = a % b;
  18.     a = b;
  19.     b = balance;
  20.   }
  21.  
  22.   return a;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement