473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Help - Adding fractions

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 2250
BigDaddyLH
1,216 Expert 1GB
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
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)...
2
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 =...
10
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
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...
33
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,...
10
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. ...
1
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...
1
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...

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.