Skip to main content

Egyptian fraction solution in c

C program for Egyptian fraction solution. Here more information.

// Include header file
#include <stdio.h>
/*
  C program for
  Egyptian fraction solution
*/
void egyptianFraction(int a, int b) {
	if ((a == 0 || b == 0)) {
		return;
	}
	if ((b >= a)) {
		// Calculate remainder
		int remainder = b % a;
		// Calculate divisor
		int divisor = b / a;
		if ((remainder != 0)) {
			divisor++;
			printf("1/%d + ",divisor);
			a = a * divisor - b;
			b = b * divisor;
			egyptianFraction(a, b);
		} else {
			// When remainder is zero
			printf("1/%d", divisor);
		}
	} else {
		// When b < a
		printf("%d + ",(a / b));
		egyptianFraction(a % b, b);
	}
}
int main() {
	// Two numbers
	int a = 7;
	int b = 53;
	egyptianFraction(a, b);
}

Output

1/8 + 1/142 + 1/30104




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment