/********************************************** * Section : 10 * Roll No. : 20CS10000 / 20CS30000 * Name : Aritra Hazra * Assignment No : 3 * Description : Continued Fraction Explorations * Date : 20-Apr-2021 **********************************************/ #include #include #define ERROR 0.000001 #define MAX_FRACTION 1000000 int main() { int choice; unsigned int x, y, num, i, temp, gcd, rem; double root, next, b, c, real; do{ // menu of options printf("\n====================================================\ \n++ Determine the Following using Continued Fraction:\ \n --> 1. Representation of Rational Fractions\ \n --> 2. Representation of Finite Reals\ \n --> 3. Square Root of Positive Integer\ \n --> 4. Roots of Quadratic Equation\ \n --> 0. Exit\ \n===================================================="); printf("\n++ Enter Your Choice: "); scanf("%d", &choice); switch(choice) { // choice selection case 0: // exit from program loop printf("\n** Thank You! Bye ...\n"); break; case 1: // representing rationals using continued fractions printf("\n++ Enter Two Positive Integers (space-separated): "); scanf("%u%u", &x, &y); printf("\n** The Continued Fraction Representation of %u/%u = ", x, y); // computing GCD of x and y gcd = x; temp = y; while(temp > 0){ rem = gcd % temp; gcd = temp; temp = rem; } // normalized fraction x = x/gcd; y = y/gcd; printf("%u/%u : [ ", x, y); do { // iterating over quotients and reminders in continued fraction printf("%u ", x/y); temp = y; y = x - y*(x/y); x = temp; }while(y > 0); printf("]\n"); break; case 2: // representing finite reals using continued fractions printf("\n++ Enter a Positive Real Number: "); scanf("%lf", &real); printf("\n** The Continued Fraction Representation of %lf: [ %u ", real, (unsigned int)real); // extracting the fractional part (6 digits after decimal point) x = MAX_FRACTION; y = real*MAX_FRACTION - (unsigned int)real*MAX_FRACTION; do { // iterating over quotients and reminders in continued fraction printf("%u ", x/y); temp = y; y = x - y*(x/y); x = temp; }while(y > 0); printf("]\n"); break; case 3: // square-root determination printf("\n++ Enter a Positive Integer: "); scanf("%u", &num); printf("\n** Iteration-wise Square-Root Values:"); next = 1.0; i = 0; do{ // iterating over root values through continued fraction root = next; next = 1.0 + (num-1)/(1.0+root); printf("\nIteration ## %6u : SQRT(%u) = %lf", i+1, num, root); i++; }while(fabs(next-root) > ERROR); printf("\n\n** The Final Square-Root = %lf", next); printf("\n** The MATH FORMULA Square-Root = %lf", sqrt(num)); // formula wise square root determination printf("\n"); break; case 4: // quadratic equation root determination printf("\n++ For Quadratic Equation x^2 + Bx + C = 0,\ \n Enter B and C (space-separated real-values ensuring B^2 >= 4C) : " ); scanf("%lf%lf", &b, &c); printf("\n** The Quadratic Equation: x^2 + (%lf)x + (%lf) = 0", b, c); printf("\n** Iteration-wise Root Values:"); next = b; i = 0; do{ // iterating over root values through continued fraction root = next; next = -b - c/root; printf("\nIteration ## %6u : Root = %lf", i+1, root); i++; }while(fabs(next-root) > ERROR); printf("\n\n** The Final Root = (%lf)\ \n The Other Root = (%lf)", next, c/next); // finding other root printf("\n** The MATH FORMULA Roots = (%lf) and (%lf)", (-b+sqrt(b*b-4*c))/2, (-b-sqrt(b*b-4*c))/2); // formula wise roots determination printf("\n"); break; default: // handling wrong choices printf("\n** Wrong Choice Entered! Please Try Again ...\n"); break; } }while(choice != 0); return 0; }