Question

I wanted to check if a fraction 2 4 (for example) can be simplified to 1 2!!
However logical condition fails.

#include <stdio.h>
int main()
{
  int a,b,live=1;
  printf("\n\nInput integers for fraction:");
  scanf(" %d%d",&a,&b);
  while(live){  
        if(!(a%2 && b%2)){
            a/=2;
            b/=2;
        }else if(!(a%3 && b%3)){
            a/=3;
            b/=3;
        }else if(!(a%5 && b%5)){
            a/=5;
            b/=5;
        }else if(!(a%7 && b%7)){
            a/=7;
            b/=7;
        }else live--;

   }
   printf("Simplified Fraction is %d/%d",a,b);
} 
Was it helpful?

Solution

The condition a%2 is equivalent to a%2 != 0, i.e. it tests if a is not divisible by 2. From De Morgan's Laws, the condition if(!(a%2 && b%2)) is equivalent to if(!(a%2) || !(b%2)) or if((a%2 == 0) || (b%2 == 0)), which is not what you want.

You really want to test if((a%2 == 0) && (b%2 == 0)) -- that is, if both are divisible by 2, not if either is divisible by 2. Writing it this way is also much less confusing.

And it should also be obvious that in order to simplify any fraction, you need to test for all possible prime factors, which is impossible to do with a finite number of if statements. The recommended way of doing this is to use the Euclidean algorithm to determined the greatest common divisor of the numerator and denominator, and then you divide both by the GCD to get the fraction in reduced form.

OTHER TIPS

(!(a%2 && b%2)) will yield true even if only one of a%2 or b%2 holds.

Have a look at the following example:

3/4 -> a%2 == 0, b%2 == 1 -> (a%2 && b%2) == 0 -> (!(a%2 && b%2)) == 1

You are looking for (a%2 == 0 && b%2 == 0) instead of your condition, and similarly for other conditions.

An "after an accepted answer" answer.

This does not detail the issues with OP's code nicely like @Adam Rosenfield, but does address the larger OP desire of "I wanted to check if a fraction 2 4 (for example) can be simplified to 1 2!!" in a general way.

Use the Euclidean Algorithm to find the greatest-common-denominator, then divide a,b by it. No need to generate prime number list. Very fast.

// Euclidean Algorithm
unsigned gcd(unsigned a, unsigned b) {
  while (b != 0) {
    int t = b;
    b = a % b;
    a = t;
  }
  return a;
}

#include <stdio.h>
int main() {
  int a, b;
  for (;;) {
    printf("\nInput positive fraction like 12/30: ");
    if (scanf("%u/%u", &a, &b) != 2)
      break;
    unsigned g = gcd(a, b);
    a /= g;
    b /= g;
    printf("Simplified Fraction is %u/%u", a, b);
  }
  return 0;
}

In addition to the logical or issue identified by others, you also have an infinite loop with your while condition. You don't need (or want) to loop with your current code. Try this

#include <stdio.h>
int main ()
{
  int a, b;
  printf ("\n\nInput integers for fraction:");
  scanf (" %d%d", &a, &b);
  while (a % 2 == 0 && b % 2 == 0)
    {
      a /= 2;
      b /= 2;
    }
  while (a % 3 == 0 && b % 3 == 0)
    {
      a /= 3;
      b /= 3;
    }
  while (a % 5 == 0 && b % 5 == 0)
    {
      a /= 5;
      b /= 5;
    }
  while (a % 7 == 0 && b % 7 == 0)
    {
      a /= 7;
      b /= 7;
    }

  printf ("Simplified Fraction is %d/%d", a, b);
}

Output(s) with your given input(s)

Input integers for fraction:2 4
Simplified Fraction is 1/2

Input integers for fraction:8 24
Simplified Fraction is 1/3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top