fork download
  1. //
  2. // main.cpp
  3. // GCD
  4. //
  5. // Created by Himanshu on 19/09/21.
  6. //
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. int gcdUsingSubtarction (int a, int b) {
  12. if (a == 0) {
  13. return b;
  14. }
  15.  
  16. while (b != 0) {
  17. if (a>b)
  18. a = a - b;
  19. else
  20. b = b - a;
  21. }
  22. return a;
  23. }
  24.  
  25. int gcdUsingDivision (int a, int b) {
  26. if (b == 0) {
  27. return a;
  28. } else {
  29. return gcdUsingDivision (b, a%b);
  30. }
  31. }
  32.  
  33. int main() {
  34. int a = 7, b = 43;
  35.  
  36. printf("GCD of %d and %d using subtraction is %d \n", a, b, gcdUsingSubtarction(a, b));
  37. printf("GCD of %d and %d using division is %d \n", a, b, gcdUsingDivision(a, b));
  38.  
  39. a = 21;
  40. b = 343;
  41.  
  42.  
  43. printf("GCD of %d and %d using subtraction is %d \n", a, b, gcdUsingSubtarction(a, b));
  44. printf("GCD of %d and %d using division is %d \n", a, b, gcdUsingDivision(a, b));
  45.  
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0.01s 5648KB
stdin
Standard input is empty
stdout
GCD of 7 and 43 using subtraction is 1 
GCD of 7 and 43 using division is 1 
GCD of 21 and 343 using subtraction is 7 
GCD of 21 and 343 using division is 7