0

I am supposed to create a program that takes a numerator and denominator value from the user then outputs the decimal value of that fraction. However, when I enter the values my output is 0.000000. Here's my code:

import java.util.Scanner;
import java.util.Random;
public class fraction
{
   public static void main ( String [] args )
    {
       int num;
       int den;
       double result;


       num = getNum();
       den = getDen();
       result = getResult(num, den);

    printResult(num, den, result);

    }

    public static int getNum()
     {
    Scanner input = new Scanner (System.in);

    int num;

    System.out.print("Please enter the numerator: ");
    num = input.nextInt();

    return num;

     }

    public static int getDen()
        {
       Scanner input = new Scanner (System.in);

       int den;

       System.out.print("Please enter the denominator: ");
       den = input.nextInt();

       return den;

        }


public static double getResult(int num, int den)
{
    double result;
    result = num / den;
    return result;
}

public static void printResult(int num, int den, double result)
{
System.out.printf("The fraction %d / %d in decimal form is %f\n", num, den, result);
}}
user1858350
  • 115
  • 1
  • 4
  • 10
  • possible duplicate of [wrong division in java](http://stackoverflow.com/questions/3553047/wrong-division-in-java) (and many others - I'll see if I can find a better one) – Jon Skeet Mar 28 '13 at 06:47
  • I guess you need to typecast it to double when you are dividing. – Sudhanshu Umalkar Mar 28 '13 at 06:47
  • 1
    Show us some sample input-output – Apurv Mar 28 '13 at 06:47
  • 1
    Additionally, as a matter of style, it's better to declare a local variable at the point where you have its value - rather than declaring everything near the start of the method, and then assigning a value later on. – Jon Skeet Mar 28 '13 at 06:48
  • It should be The fraction 3 / 4 in decimal form is 0.75000 My output is The fraction 3 / 4 in decimal form is 0.000000 – user1858350 Mar 28 '13 at 06:49
  • Here's a better one: http://stackoverflow.com/questions/15649057/java-arithmetic-division and this one: http://stackoverflow.com/questions/5543661/whats-wrong-with-this-division – Jon Skeet Mar 28 '13 at 06:49
  • Please use the conventions: classes start with a capital letter, avoid abreviations (`num`, `den`). It is not clear at the first signe what `getDen` does. – SteeveDroz Mar 28 '13 at 06:52
  • possible duplicate of [Java Integer division: How do you produce a double?](http://stackoverflow.com/questions/3144610/java-integer-division-how-do-you-produce-a-double) – fglez Apr 04 '13 at 15:17

2 Answers2

7

That's because 1 / 2 is equal to 0, not 0.5 when you use ints. Try casting your ints into doubles either with (double) num / den or with 1.0 * num / den.

SteeveDroz
  • 6,006
  • 6
  • 33
  • 65
2

This should do -

result = (double) num / den;

Else, the result of int / int would be int without fractional part.

Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33