cs1713-day0-prog1.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (C) 2020, 2019, 2018, 2017 Girish M
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. * MA 02110-1301, USA.
  17. *
  18. */
  19. /*-----------------------------------
  20. Name:Girish M
  21. Roll number:cs1713
  22. Date:20 July 2017
  23. Program description: Given two positive integers, find their greatest common divisor (gcd).
  24. Acknowledgements: Euclid's algorithm to compute gcd of two numbers
  25. -----------------------------------*/
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <ctype.h>
  29. int gcd(int num1, int num2)
  30. {
  31. if(num2 == 0)
  32. return num1;
  33. else
  34. return gcd(num2, num1%num2);
  35. }
  36. int main(int argc, char* argv[])
  37. {
  38. if(argc == 3)
  39. {
  40. int num1 = atoi(argv[1]);
  41. int num2 = atoi(argv[2]);
  42. if((num1 >= 0) && (num2 >= 0))
  43. {
  44. if(num1 >= num2)
  45. printf("\nGCD of %d and %d is: %d\n", num1, num2, gcd(num1,num2));
  46. else
  47. printf("\nGCD of %d and %d is: %d\n", num1, num2, gcd(num2,num1));
  48. }
  49. }
  50. else
  51. {
  52. printf("\nUsage: ./cs1713-day0-prog1.o num1 num2\n");
  53. }
  54. return 0;
  55. }