Here are the homework instructions:(adsbygoogle = window.adsbygoogle || []).push({});

Write a program that will accept two integers representing the numerator and denominator of a fraction, and display:

1. The fraction in "lowest form" with a sign only in the numerator.

2. The fraction is signed decimal form.

Write your program with three functions in addition to main(): one to print the fraction in fraction form, one to print it in decimal form, and one to find the greatest common denominator (gcd) to allow you to reduce the fraction to lowest form. (You will want to search the Internet for Euclid's algorithm for this.)

Here is what I have coded so far:

double decimal(int, int); // function prototype of fraction's decimal form

int gcd(int, int); // function prototype for greatest common denominator

# include <stdio.h>

int main()

{

int n; // represents numerator

int d; // represents denominator

int fraction; // represents numerator & denominator as a fraction

printf("Hello, this program is designed to accept two integers \n"

"representing the numerator and denominator of a fraction. \n"

"It is also designed to display the decimal form, \n"

"GCD, and the fractional form of the fraction! \n\n");

printf("What is the integer value of your numerator? ");

scanf("%d", &n);

printf("What is the integer value of your denominator? ");

scanf("%d", &d);

printf("\nThe GCD of your fraction is: %d ", gcd(n, d));

printf("\nThe decimal form of your fraction is: %.6lf \n", decimal(n, d));

printf("\nProgram Author 1: Vladimir S. Lolinco\n");

printf("Program Author 2: Glenda Jacobs\n\n");

system("pause");

return 0;

}

double decimal(int n, int d)

{

return (double)n/d;

}

int gcd(int n, int d)

{

if (d == 0)

return n;

else

return gcd(d, n % d);

}

My problems/questions:

1) How do I go about enforcing a positive or negative sign for the decimal form of my fraction?

2) How do I go about writing a function that prints the numerator and denominator as an actual fraction without doing any math and also attaching either a plus or minus sign next to the numerator?

3) I need to make this loop and I have tried using SENTINEL values or EOF (aka Ctrl+Z) and it still does not work - how would I make it loop so that when the user enters Ctrl+Z for either the numerator or denominator that the program exits out and the loop terminates?

Thanks.

**Physics Forums | Science Articles, Homework Help, Discussion**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Homework Help: C+ Programming Homework Help (Not C++)

**Physics Forums | Science Articles, Homework Help, Discussion**