Question

In: Computer Science

Using the code below as a template, write a program that asks the user to enter...

Using the code below as a template, write a program that asks the user to enter two integers, and finds and prints their greatest common denominator (GCD) in Java!

package cp213;

import java.util.Scanner;

public class Lab01 {

    /**
     * @param a
     * @param b
     * @return
     */
    public static int gcd(int a, int b) {

        // your code here

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int a = 0;
        int b = 0;
        int c = 0;

        // Read an integer from the keyboard.
        System.out.print("Enter a (0 to quit): ");
        a = keyboard.nextInt();

        // your code here

        // gcd function call
        c = Lab01.gcd( a, b );

        // your code here

    }

}

Solutions

Expert Solution

package cp213;

import java.util.Scanner;

public class Lab01 {

    /**
     * @param a
     * @param b
     * @return
     */
    public static int gcd(int a, int b) {
        if (a == 0 || b == 0)
            return 0;
        else if (a == b)
            return a;
        else if (a > b)
            return gcd(a - b, b);
        else
            return gcd(a, b - a);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int a = 0;
        int b = 0;
        int c = 0;

        while (true) {
            // Read an integer from the keyboard.
            System.out.print("Enter a number(0 to quit): ");
            a = keyboard.nextInt();
            if (a == 0) break;
            System.out.print("Enter another number: ");
            b = keyboard.nextInt();

            // gcd function call
            c = Lab01.gcd(a, b);

            System.out.println("GCD of " + a + " and " + b + " is " + c + "\n");
        }
    }
}


Related Solutions

Please write in Python code please: Write a program that asks the user to enter 5...
Please write in Python code please: Write a program that asks the user to enter 5 test scores between 0 and 100. The program should display a letter grade for each score and the average test score. You will need to write the following functions, including main: calc_average – The function should accept a list of 5 test scores as an input argument, and return the average of the scores determine_grade – The function should accept a test score as...
In the space provided below write a C++ program that asks the user to enter their...
In the space provided below write a C++ program that asks the user to enter their quarterly earnings for the past two years stores the data in a 2-dimensional array. The program then computes both the annual earnings as well as the total earning and prints the results along with the 2-dimensional array on screen as well as onto a file.
In the space provided below write a ******C program********* that asks the user to enter their...
In the space provided below write a ******C program********* that asks the user to enter their quarterly earnings for the past two years stores the data in a 2-dimensional array. The program then computes both the annual earnings as well as the total earning and prints the results along with the 2-dimensional array on screen. Using the embed icon shown above, also include screenshots demoing the execution of your program. Please write carefully. Thank you
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
---- Python CIMP 8A Code Lab 4 Write a program that asks the user to enter...
---- Python CIMP 8A Code Lab 4 Write a program that asks the user to enter 5 test scores. The program will display a letter grade for each test score and an average grade for the test scores entered. The program will write the student name and average test score to a text file (“studentgrades.txt”). Three functions are needed for this program. def letter_grade( test_score) Test Score Letter Grade 90-100 A 80-89 B 70-79 C 60-69 D Below 60 F...
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in...
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in a number greater than or equal to zero and less than or equal to 100. If they do not you should alert them and end the program. Next, determine the letter grade associated with the number. For example, A is any grade between 90 and 100. Report the letter grade to the user.
Write a program using switch statement that asks user to enter the month number and then...
Write a program using switch statement that asks user to enter the month number and then it prints out the number of days for that month. For simplicity reasons have your program print 28 days for February no matter if it is a leap year or not. Your program should also handle any invalid month numbers that user could enter (hint use default for the switch). Use a while loop to allow user to test for different month entries till...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Code a program which asks user to enter two strings using Scanner class and returns the...
Code a program which asks user to enter two strings using Scanner class and returns the total number of non-space characters in the Strings. For ex: “Hi there” and “hello” should return 12.
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT