This blog is under construction

Sunday, 23 June 2013

C program to find the GCD of two numbers

Write a C program to find the greatest common factor of two numbers.

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

        /* get the inputs from the user */
        printf("Enter input1:");
        scanf("%d", &num1);
        printf("Enter input2:");
        scanf("%d", &num2);

        /* find the gcd of two numbers */
        while (num2 > 0) {
                temp = num1 % num2;
                num1 = num2;
                num2 = temp;

        /* print the result */
        printf("Greatest common factor: %d\n", num1);
        return 0;

  jp@jp-VirtualBox:~/cpgms/lab_pgms/02_control_flow$ ./a.out
  Enter input1:100
  Enter input2:125
  Greatest common factor: 25

No comments:

Post a Comment