TechnologY

Display given Improper_Function

Question:

Ask the user to input two integer, mainly numerator and denominator. You are supposed to print the improper fraction in the improper fraction.

so if you say a/b = denominator * quotient + remainder

||                       Remainder
||==> Quotient———————-
||                       Denominator

Answer:

Lets begin considering the test cases.

Divide by 0 Whole Number Division
99/7 7/99
-99/7 -7/99
99/-7 7/-99

I am working in Ubuntu on Code Block IDE so the headers might be different when used in windows OS.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}

int main()
{
    int n,d,temp;
    scanf("%d",&n);
    scanf("%d",&d);

    if(d==0){
        printf("Scientists are still working on it\n");
        exit(0);
    }
    printf("GCD %d \n",temp=gcd(n,d));
    n=n/temp;
    d=d/temp;

    printf("\nThe numerator magnitude is %d\n", n);
    printf("\nThe denominator magnitude is %d\n", d);


    if(n%d==0)
        printf("%d",n/d);
    else if((abs(n) < abs(d)) && ((n<0 && d>0) ||(n>0 && d<0)) ) {  // -7, 99   7,-99
        printf("K %d = %d - C\n\n", n,d);
        printf("  %d\n", (n%d));
        printf(" ----\n");
        printf("  %d\n", d);
    }
    else if((abs(n) > abs(d) && n<0 && d>0)) {  // -99, 7
        printf("M %d = %d - C\n\n", n,d);
        printf("    %d\n", (n%d)*-1);
        printf("%d----\n",n/d);
        printf("    %d\n", d);
    }
    else if((n<d && n>0 && d>0) || (n>d && n<0 && d<0)) { //  7,99   -7,-99
        printf("N %d = %d - C\n\n", n,d);
        printf(" %d\n", (n%d>0)?(n%d):((n%d)*-1));
        printf("----\n");
        printf(" %d\n", (d>0)?d:(d*-1));
    }
    else{                                           //99,7      99,-7     -99,-7
        printf("%d = ",n);
        printf("B %d x %d  + %d\n\n\n",d, d, n%d);
        printf("   %d\n", (n%d>0)?(n%d):((n%d)*-1));
        printf("%d----\n",n/d);
        printf("   %d\n", (d>0)?d:(d*-1));
    }
}

The program does not have any error or warning. The code has been tested and optimised. Comment if you can suggest some modifications.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s