This blog is under construction

Monday 8 July 2013

C program to calculate Highest Common Factor(HCF)

Write a C program to calculate Highest Common Factor(HCF).


  #include <stdio.h>
  int main()  {
        int val1, val2, hcf;

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

        /* take smallest value to manipulate hcf value */
        if (val1 > val2) {
                hcf = val2;
        } else {
                hcf = val1;
        }

        /* hcf manipulation */
        while (hcf > 1) {
                if (val1 % hcf == 0 && val2 % hcf == 0) {
                        break;
                }
                hcf--;
        }

        /* print the outputs */
        printf("HCF between %d and %d is %d\n", val1, val2, hcf);
        return 0;
  }



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





See Also:

No comments:

Post a Comment