# Operation on fraction

This is a discussion on Operation on fraction within the C Programming forums, part of the General Programming Boards category; Can anyone help me to write a program with the problem as shown below: Write a complete C program to ...

1. ## Operation on fraction

Can anyone help me to write a program with the problem as shown below:

Write a complete C program to perform computations with common fractions and get results that are common fractions in reduced form. Your program shoud be able to perform addition, subtraction, multiplication or division on the given two fractions of a/b where a and b are integers representing numerator and denominator respectively.
Sample input and output:
Enter the first fraction as two integers seperated by a slash> 3/4
Enter the second fraction as two integers seperated by a slash> 5/8
Enter an arithmatic operator (+,-,*,/)> +
3/4 + 5/8 = 11/8
Would you like to continue<Y/N> Y
Enter the first fraction as two integers seperated by a slash> 3/4
Enter the second fraction as two integers seperated by a slash> 5/8
Enter an arithmatic operator (+,-,*,/)> *
3/4 * 5/8 = 15/32
Would you like to continue?<Y/N> N
Bye. Bye...have a nice day!

2. I would start by programming something and then asking for help when you get stuck.

3. I wrote an algebra solver that handled this sort of stuff. If you are just doing straight fractions you could start off with something like:

Example:
Code:
```struct fraction_t
{
signed int numerator;
unsigned int denominator;
};```
From there on out, it should be fairly simple logic for you to program on your own, right? On my algebra solver, however, I stored the numerator and denominator in a custome type that was designed to be arbitrary precision. You'd probably get some bonus points from your teacher for writing something like that.

4. I would review the use to strtok(). To split your inputed string on '\'. Then do the appropriate math on your numerator and denominator.

5. sorry I think the solution you all give is in C++ not C program. Can you give me in C language? Thanks

6. Originally Posted by boonjie
sorry I think the solution you all give is in C++ not C program. Can you give me in C language? Thanks
Your thought is incorrect. All the above are C.

7. Both master5001's and slingerland3g's suggestions are entirely within the realm of C.

8. I don't understand what you need help with. Can you write a Hello World program? Do you know what a variable is? Do you know how to get input from the user? Do you understand how the math works?

9. I am always more of a C programmer than C++ programmer, for whatever that is worth, boonjie. C is to my speaking English as C++ is to my speaking Spanish (granted I am more confident and fluent in C++ than Spanish, but I digress).

I don't know of a cleaner way of doing it than using a structure to keep track of your numerators and denominators. You could also use an array of integers and consider one index to be the numerator and the other to be the denominator.

10. Originally Posted by master5001
I am always more of a C programmer than C++ programmer, for whatever that is worth, boonjie. C is to my speaking English as C++ is to my speaking Spanish (granted I am more confident and fluent in C++ than Spanish, but I digress).

I don't know of a cleaner way of doing it than using a structure to keep track of your numerators and denominators. You could also use an array of integers and consider one index to be the numerator and the other to be the denominator.
But how to give the answer in reduce form?

11. Originally Posted by boonjie
But how to give the answer in reduce form?
You need to find the greatest common divisor.

--
Mats

12. Euclidean algorithm...

13. I will reiterate what you have been already told several times, you produce some code, you will get some code in return. I will give psuedo code to people who sound like they need help and do know how to program, but just don't know where to start. And finally, I will write source code for those who are actually trying.

Try my friend. Do something.