Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Implement a C# Console Application that will prompt the user to enter integer values for two fractions. Take note that each fraction consists of an integer numerator and an integer denominator part.

Your program needs to display the two fractions in the following format:
Original Fractions:
Fraction 1: 1 / 2
Fraction 2: 2 / 5

Your program also needs to calculate and display the sum and the product of the two fractions
in a non-simplified form as follows:
New Fractions with Common Denominator:
Fraction 1: 5 / 10
Fraction 2: 4 / 10
Sum: 5 / 10 + 4 / 10 = 9 / 10
Product: 1 / 2 x 2 / 5 = 2 / 10

What I have tried:

C#
int numerator1 = 1, denominator1 = 2;
            int numerator2 = 2, denominator2 = 5;

            int fraction1;
            int fraction2;

            Console.WriteLine("Original Fractions:");
            Console.WriteLine("Fraction 1:    {0}/{1}",numerator1,denominator1);
            Console.WriteLine("Fraction 2:    {0}/{1}", numerator2,denominator2);


            Console.Write("Enter a fraction1 ");
            fraction1 = int.Parse(Console.ReadLine());
            Console.Write("Enter a fraction 2 ");
            fraction2 = int.Parse(Console.ReadLine());
Posted
Updated 9-Mar-17 1:10am
v3
Comments
Tomas Takac 9-Mar-17 3:55am    
Do you realize you didn't state your problem? What's wrong? Is there an error? Please note that we don't do homeworks around here. But if you have a valid programming question you should rephrase your question as such.

The way I would do it is to create a Fraction class which contains a numerator and a denominator. I'd also override ToString to have it return a "human readable" form:
numerator / denominator

I'd also create Add and Product methods which combined two fractions to return the sum and product respectively as a new Fraction instance.

Then in my Main method, I'd get the two values from the user - like you are asked - construct an instance of the Fraction class using the values. Create a second, and use the Add and Product methods together with ToString to print the result.

Sounds complicated? It isn't, not really - it's actually simpler than what you are trying to do once you get your head round it.

But ... this is your homework, so I'll not give you the code.
Give it a try, and see how far you get!
 
Share this answer
 
Comments
CPallini 9-Mar-17 5:19am    
I would give my 5 to your homework.
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900