Question

In: Computer Science

Create a project called rise. Add a class called GCD. Complete a program that reads in...

Create a project called rise. Add a class called GCD. Complete a program that reads in a numerator (as an int) and a denominator (again, as an int), computes and outputs the GCD, and then outputs the fraction in lowest terms. Write your program so that your output looks as close to the following sample output as possible. In this sample, the user entered 42 and 56, shown in green.

Enter the numerator: 42

Enter the denominator:

56 The GCD is: 14

The fraction in lowest terms is: ¾

Use a while loop, in Java

Solutions

Expert Solution

import java.util.*;

public class GCD
{
        public static void main(String[] args) 
        {
                Scanner sc=new Scanner(System.in);
                int n,d,n1,n2;
        //taking user inputs
                System.out.print("Enter the numerator : ");
                n=sc.nextInt();
                System.out.print("Enter the denominator : ");
                d=sc.nextInt();
        assigning the values in separate variables
                n1=n;
                n2=d;
        //while loop will execute until both values become equal
        //the least value for this evaluation will be the GCD
                while(n1!=n2)   
        {  
            if(n1>n2) 
            {
                n1=n1-n2; 
            }
            else  
            {
                n2=n2-n1; 
            }
        } 
        System.out.println("The GCD is : "+n2);
        System.out.println("The fraction in lowest terms is : "+(n/n2)+"/"+(d/n2));
        }  
}

Create a project in Eclipse or in NetBeans with preferable name and then create java class file named as GCD.java ,save the above code in that and execute..


Related Solutions

Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables:...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables: StudentName (string), SchoolYear (int), YearsUntilGraduation(int) * Method YTK() = 12 - SchoolYear; 2. Main *Enter name *Enter age *You will attend school:____ years before graduating.
In this class add Comparable interface. In the driver program create a few objects and In...
In this class add Comparable interface. In the driver program create a few objects and In the driver program create a few objects and compare them . then create a list of those objects and sort them .A Quadratic is bigger than another Quadratic if it opens faster package pack2; /** * This is a program for defining a quadratic equation * @author sonik */ public class Quadratic { public int coeffX2 = 0; public int coeffX = 0; public...
In BlueJ, create a project called Lab6 Create a class called LineSegment –Note: test changes as...
In BlueJ, create a project called Lab6 Create a class called LineSegment –Note: test changes as you go Copy the code for LineSegment given below into the class. Take a few minutes to understand what the class does. Besides the mutators and accessors, it has a method called print, that prints the end points of the line segment in the form: (x, y) to (x, y) You will have to write two methods in the LineSegment class. There is information...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
C++ Program -------------------- Project 5-1: Monthly Sales Create a program that reads the sales for 12...
C++ Program -------------------- Project 5-1: Monthly Sales Create a program that reads the sales for 12 months from a file and calculates the total yearly sales as well as the average monthly sales. Console Monthly Sales COMMAND MENU m - View monthly sales y - View yearly summary x - Exit program Command: m MONTHLY SALES Jan        14317.41 Feb         3903.32 Mar         1073.01 Apr         3463.28 May         2429.52 Jun         4324.70 Jul         9762.31 Aug        25578.39 Sep         2437.95 Oct         6735.63 Nov          288.11 Dec         2497.49...
JAVA single method Loop (for beginner) Name your class LoopsFiles Create a program that reads a...
JAVA single method Loop (for beginner) Name your class LoopsFiles Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed When writing your CSV file, the first row (header row)...
Directions: Create a Bluej project called Exam1 that implements the Headphone class and the HeadphoneInventory class...
Directions: Create a Bluej project called Exam1 that implements the Headphone class and the HeadphoneInventory class described below. Each class is worth 50 points. When you are finished, zip the project folder and submit it. The Headphone class represents a headphone. It stores the headphone’s manufacturer: manufacturer, name: name, quantity: quantity, number of times restocked: timesRestocked. Write the Headphone class according to the following requirements. Add the four fields to the Headphone class. Create a constructor that takes two parameters....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT