Java Help

0 Zodiac delby · November 2, 2014
Please can any one help on below :

1.      Write a program that reads from the user four integers representing the numerators and denominators of two fractions, calculates the results of the two fractions and displays the values of the fractions sum, subtraction, multiplication and division.
 
Sample run:
Enter the numerator and denominator of the first fraction: 6 4
Enter the numerator and denominator of the second fraction: 8 5
The sum is: 3.1
The subtraction is: -0.1
The multiplication is: 2.4
The division is: 0.9375


Answer :

Post a Reply

Replies

Oldest  Newest  Rating
0 Yasser Taha · November 3, 2014

import java.util.Scanner;

public class JavaApplication {
   
  public static void main(String[] args) {

    System.out.println("Enter the numerator and denominator of the first fraction respectively:");
    Scanner input = new Scanner(System.in);
    double n1 = input.nextDouble(); //defining 1st num
    double d1 = input.nextDouble(); //definding 1st denominator
    System.out.println("Enter the numerator and denominator of the second fraction respectively:");
    double n2 = input.nextDouble(); // 2nd num
    double d2 = input.nextDouble(); // 2nd denominator
    double s = n1/d1 + n2/d2; //sum
    double sub = n1/d1 - n2/d2; // subtraction
    double m = (n1/d1)*(n2/d2); //multiplication
    double div = (n1/d1)/(n2/d2); //division
    System.out.println("The sum is: " + s); //easy stuff
    System.out.println("The subtraction is: " + sub);
    System.out.println("The multiplication is: " + m);
    System.out.println("The division is: " + div);
  • 1

Java / Android Development

107,255 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator