473,796 Members | 2,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help - Adding fractions

2 New Member
I can't get the common denominator to work. Please help.

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.*;
  3. import java.lang.*;
  4. public class Fraction {
  5.  
  6.     /**
  7.      * @param args
  8.      */
  9.     public static void main(String[] args) {
  10.  
  11.  
  12.         // User input
  13.  
  14.         Scanner scan = new Scanner(System.in);
  15.         System.out.println ("Input numerator from first fraction: ");
  16.         int numerator1 = scan.nextInt();
  17.         System.out.println ("Input denominator from first fraction");
  18.         int denominator1 = scan.nextInt();
  19.         System.out.println ("Input numerator from second fraction");
  20.         int numerator2 = scan.nextInt();
  21.         System.out.println ("Input denominator from second fraction");
  22.         int denominator2 = scan.nextInt();
  23.  
  24.         //Check for invalid integers
  25.         if (denominator1 <= 0 || denominator2 <=0){
  26.             System.out.println ("Can not have a zero in denominator");
  27.         }
  28.  
  29.         //Compare two denominators
  30.         int smallNum = 0, bigNum = 0, bigDenom = 0, smallDenom = 0;
  31.  
  32.          if(denominator1>denominator2)
  33.             {
  34.              bigNum = numerator1;
  35.              smallNum = numerator2;
  36.              bigDenom = denominator2;
  37.              smallDenom = denominator1;
  38.             }
  39.          if (denominator2>denominator1);
  40.             {
  41.                  bigNum = numerator2;
  42.                  smallNum = numerator1;
  43.                  bigDenom = denominator1;
  44.                  smallDenom = denominator2;
  45.             }
  46.  
  47.         //Find common denominator
  48.  
  49.             int newDenom = bigDenom;
  50.             if (smallDenom == 0){System.out.println ("You suck");}
  51.             else {
  52.             int newNum = ((newDenom / smallDenom)* smallNum);
  53.             System.out.println(newNum);
  54.             System.out.println(bigDenom);
  55.             System.out.println(smallDenom);
  56.             System.out.println(smallNum);
  57.             System.out.println(bigNum);
  58.  
  59.         // Add the fractions
  60.  
  61.             int sumNum, sumDenom;
  62.             sumNum = (newNum + bigNum);
  63.             sumDenom = bigDenom;
  64.  
  65.         // Simplify fraction
  66.  
  67.             int biggerNumber; int smallerNumber;
  68.  
  69.             if(sumNum>sumDenom)
  70.                {
  71.                  biggerNumber=sumNum;
  72.                  smallerNumber=sumDenom;}
  73.                else
  74.                {
  75.                  biggerNumber=sumNum;
  76.                  smallerNumber=sumDenom;
  77.                }
  78.  
  79.                for(int divisor=1;
  80.                divisor<=biggerNumber;divisor++)
  81.  
  82.                {
  83.  
  84.                    if
  85.  
  86.                    (((biggerNumber*divisor)%smallerNumber)==0)
  87.  
  88.                {
  89.  
  90.                    int LCM=biggerNumber*divisor;
  91.  
  92.                    break;
  93.            }
  94.  
  95.            }
  96.                    int count=1;
  97.  
  98.                        while(count!=0){count=biggerNumber%smallerNumber;
  99.  
  100.                    if(count==0)
  101.  
  102.            {
  103.  
  104.            System.out.println ((sumNum / smallerNumber) + "/" + (sumDenom / smallerNumber));}
  105.  
  106.                   else{
  107.  
  108.                       biggerNumber=smallerNumber;
  109.                       smallerNumber=count;}}}}
  110.  
  111.         }
  112.  
  113.  
Oct 13 '08 #1
1 2266
BigDaddyLH
1,216 Recognized Expert Top Contributor
I can't get the common denominator to work. Please help.

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.*;
  3. import java.lang.*;
  4. public class Fraction {
  5.  
  6.     /**
  7.      * @param args
  8.      */
  9.     public static void main(String[] args) {
  10.  
  11.  
  12.         // User input
  13.  
  14.         Scanner scan = new Scanner(System.in);
  15.         System.out.println ("Input numerator from first fraction: ");
  16.         int numerator1 = scan.nextInt();
  17.         System.out.println ("Input denominator from first fraction");
  18.         int denominator1 = scan.nextInt();
  19.         System.out.println ("Input numerator from second fraction");
  20.         int numerator2 = scan.nextInt();
  21.         System.out.println ("Input denominator from second fraction");
  22.         int denominator2 = scan.nextInt();
  23.  
  24.         //Check for invalid integers
  25.         if (denominator1 <= 0 || denominator2 <=0){
  26.             System.out.println ("Can not have a zero in denominator");
  27.         }
  28.  
  29.         //Compare two denominators
  30.         int smallNum = 0, bigNum = 0, bigDenom = 0, smallDenom = 0;
  31.  
  32.          if(denominator1>denominator2)
  33.             {
  34.              bigNum = numerator1;
  35.              smallNum = numerator2;
  36.              bigDenom = denominator2;
  37.              smallDenom = denominator1;
  38.             }
  39.          if (denominator2>denominator1);
  40.             {
  41.                  bigNum = numerator2;
  42.                  smallNum = numerator1;
  43.                  bigDenom = denominator1;
  44.                  smallDenom = denominator2;
  45.             }
  46.  
  47.         //Find common denominator
  48.  
  49.             int newDenom = bigDenom;
  50.             if (smallDenom == 0){System.out.println ("You suck");}
  51.             else {
  52.             int newNum = ((newDenom / smallDenom)* smallNum);
  53.             System.out.println(newNum);
  54.             System.out.println(bigDenom);
  55.             System.out.println(smallDenom);
  56.             System.out.println(smallNum);
  57.             System.out.println(bigNum);
  58.  
  59.         // Add the fractions
  60.  
  61.             int sumNum, sumDenom;
  62.             sumNum = (newNum + bigNum);
  63.             sumDenom = bigDenom;
  64.  
  65.         // Simplify fraction
  66.  
  67.             int biggerNumber; int smallerNumber;
  68.  
  69.             if(sumNum>sumDenom)
  70.                {
  71.                  biggerNumber=sumNum;
  72.                  smallerNumber=sumDenom;}
  73.                else
  74.                {
  75.                  biggerNumber=sumNum;
  76.                  smallerNumber=sumDenom;
  77.                }
  78.  
  79.                for(int divisor=1;
  80.                divisor<=biggerNumber;divisor++)
  81.  
  82.                {
  83.  
  84.                    if
  85.  
  86.                    (((biggerNumber*divisor)%smallerNumber)==0)
  87.  
  88.                {
  89.  
  90.                    int LCM=biggerNumber*divisor;
  91.  
  92.                    break;
  93.            }
  94.  
  95.            }
  96.                    int count=1;
  97.  
  98.                        while(count!=0){count=biggerNumber%smallerNumber;
  99.  
  100.                    if(count==0)
  101.  
  102.            {
  103.  
  104.            System.out.println ((sumNum / smallerNumber) + "/" + (sumDenom / smallerNumber));}
  105.  
  106.                   else{
  107.  
  108.                       biggerNumber=smallerNumber;
  109.                       smallerNumber=count;}}}}
  110.  
  111.         }
  112.  
  113.  
Can you be more specific about your error? By the way, my compiler noticed a semicolon here, which should not be there:
Expand|Select|Wrap|Line Numbers
  1. if (denominator2>denominator1);
Oct 14 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
2513
by: Ted | last post by:
I've written a little function to remove everything after the 2nd decimal place for prices which is as follows: - ReturnConvertedCurrency = (fix(iSterling * session("ExchangeRate") * 100) / 100) However, it sometimes returns incorrect values. i.e. Why does the following: - response.write(FormatNumber((fix(2.30 * 1 * 100) / 100) , 2))
2
9249
by: karp | last post by:
Lets say I have the following class in order to add two fractions, class Fraction{ public: Fractions::Fraction(int numer = 0, int denom = 1) { valeurNumerateur = numer; valeurDenominateur = denom; }
10
2693
by: sp0 | last post by:
Is there a reason why to make mix numbers improper when adding? It seems when subtracting and adding, adding a subtracting the whole numbers and fraction parts should be sufficient? what'ch think
5
5923
by: surrealtrauma | last post by:
the requirement is : Create a class called Rational (rational.h) for performing arithmetic with fractions. Write a program to test your class. Use Integer variables to represent the private data of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided and should...
33
6638
by: selowitch | last post by:
I've been searching in vain for a way to present typographically correct fractions (not resorting to <sup> and <sub> tags) but have been frustrated by the fact that the glyphs for one-half, one-eighth, three-quarters, etc. do not display in the correct typeface (or even consistently the same typeface) and seem totally resistant to attempt to fix this through CSS: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
10
1459
by: David Ricker | last post by:
I am having problems adding two numbers. I am trying to add 1.005 and 1.007 to come up with 2.012. Should be easy enough right? Problem is that I keep getting 2.0119999999999996 as my result. Why is this happening? Is there a way that I can get it to return the right result? Any help is appreciated. Thanks, David J. Ricker II
1
8569
by: JWest46088 | last post by:
I am having trouble figuring out how to add, subtract, multiply, and divide fractions in my Java program. First off I will tell you what I already did. At first, the assignment was to: Define a class called Rational. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator or denominator as a double. Include additional member function that outputs the value of the...
1
1639
by: Semajthewise | last post by:
Here it is cleaned up a little more. Here's what this code does. It will take 2 fractions and add, subtract, multiply, or divide them. The user enters the fractions to be calculated into two textboxes. These can be entered as a whole number and a proper fraction, whole number and an improper fraction, just a proper/inproper fraction, or just a whole number. Form build Requirements: using Microsoft Visual Studio.NET 2003 single form...
0
1247
by: Paddy | last post by:
(From: http://paddy3118.blogspot.com/2008/09/python-fractions-issue.html) There seems to be a problem/difference in calculating with the new fractions module when comparing Python 26rc2 and 30rc1 I was reading the paper "Interval Arithmetic: Python Implementation and Applications" and thought to try the first example function f(x,y), which needs more precision than Python floating point provides (on my Windows PC), to calculate a...
0
10467
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10244
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4130
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.