This blog is under construction

Monday 8 July 2013

C program to calculate Greatest Common Divisor(GCD)

Write a C program to calculate Greatest Common Divisor (GCD).


  #include <stdio.h>
  int main()  {
        int num1, num2, gcd;

        /* get the input values from the user */
        printf("Enter your first number:");
        scanf("%d", &num1);
        printf("Enter your second number:");
        scanf("%d", &num2);

        /* take smallest value to manipulate gcd value */
        if (num1 > num2) {
                gcd = num2;
        } else {
                gcd = num1;
        }

        /* gcd manipulation */
        while (gcd > 1) {
                if (num1 % gcd == 0 && num2 % gcd == 0) {
                        break;
                }
                gcd--;
        }

        /* print the outputs */
        printf("GCD between %d and %d is %d\n", num1, num2, gcd);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your first number:54
  Enter your second number:24
  GCD between 54 and 24 is 6





See Also:

No comments:

Post a Comment