This blog is under construction

Sunday 23 June 2013

C program to simplify the given fraction using GCF

Write a C program to simplify the given fraction using Greatest Common Factor.


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

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

        /* find the GCF */
        while (num2 > 0) {
                temp = num1 % num2;
                num1 = num2;
                num2 = temp;
        }
        printf("Greatest common factor: %d\n", num1);

        /* simplify the fraction and display outputs */
        printf("Simplified fraction:(%d/%d)\n", input1/num1, input2/num1);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter input1:100
  Enter input2:125
  Greatest common factor: 25
  Simplified fraction:(4/5)



No comments:

Post a Comment