Hey guys, rookie programmer here, need some help for a program due. Excuse me for the triviality of this program, remember we all have to start somewhere.
The topic: Write a program that asks the user to enter a fraction of two nature numbers, and then
reduces the fraction to lowest terms:
Enter a fraction of natural numbers: 18/6
The lowest term is: 3/1
Note: Youre required to check the validity of the input, i.e. the input should be non-negative
and the denominator should ≠ 0. If not, print a hint and let the user re-input.
Hints: 1. Use a loop to check the numbers until they are valid.
2. To reduce a fraction, first compute the greatest common divisor (GCD). A classic
algorithm for computing GCD is the Euclidean algorithm. It goes as follows: Let and be the
two numbers, . Compute the remainder when is divided by . Copy into and
copy the remainder into . If is 0, then stop and contains the GCD. Otherwise, repeat this
process. For detail, please refer to http://en.wikipedia.org/wiki/Euclidean_algorithm
I've never used a loop before, but right now here's what i got. :
Kind of get lost here. I know what I want to do, just not entirely sure on how to get there. Right now I'm just trying to calculate the GCD, not really working on anything else till i get that. I have c=a%b, this is the remainder, then c=a%c, so something like 10/6, c=4, then c=2, then if i did a/c, would give me 5, meaning c is zero, then breaking the loop? excuse me once more, some help is needed, just a general push in the right direction would be appreciated!
EDIT: now i'm thinking it could go something like:
This did not work, but am I headed in the right track?
The topic: Write a program that asks the user to enter a fraction of two nature numbers, and then
reduces the fraction to lowest terms:
Enter a fraction of natural numbers: 18/6
The lowest term is: 3/1
Note: Youre required to check the validity of the input, i.e. the input should be non-negative
and the denominator should ≠ 0. If not, print a hint and let the user re-input.
Hints: 1. Use a loop to check the numbers until they are valid.
2. To reduce a fraction, first compute the greatest common divisor (GCD). A classic
algorithm for computing GCD is the Euclidean algorithm. It goes as follows: Let and be the
two numbers, . Compute the remainder when is divided by . Copy into and
copy the remainder into . If is 0, then stop and contains the GCD. Otherwise, repeat this
process. For detail, please refer to http://en.wikipedia.org/wiki/Euclidean_algorithm
I've never used a loop before, but right now here's what i got. :
Code:
#include <stdio.h>
main(void)
{
int a,b,c,d,e,f,g;
printf("Enter your first number");
scanf_s("%d/n",&a);
printf("Enter your second number");
scanf_s("%d/n",&b);
while(c!=0)
{
c=a%b;
c=a%c;
Kind of get lost here. I know what I want to do, just not entirely sure on how to get there. Right now I'm just trying to calculate the GCD, not really working on anything else till i get that. I have c=a%b, this is the remainder, then c=a%c, so something like 10/6, c=4, then c=2, then if i did a/c, would give me 5, meaning c is zero, then breaking the loop? excuse me once more, some help is needed, just a general push in the right direction would be appreciated!
EDIT: now i'm thinking it could go something like:
Code:
do{
c=a%b;
a=b;
b=c;}
while(c>0);
This did not work, but am I headed in the right track?
Last edited by a moderator: