Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with a Java problem: Using the LotteryOdds class provided below as a

ID: 3808853 • Letter: I

Question

I need help with a Java problem:

Using the LotteryOdds class provided below as an example, write a class called GCD that prompts the user for two integers and then computes and prints their greatest common divisor (GCD).

Here is a Python version of a program to do that:

Note that the main part of the Python program is the last three lines and that, in Java, the main code goes into the main method, as in LotteryOdds.

LotteryOdds.java:

import stdlib.*;

public class LotteryOdds {

   public static long factorial(int n){
       if (n == 0) {
           return 1;
       }
       else {
           return n * factorial(n-1);
       }
   }

   public static long choose(int n, int k){
       return factorial(n)/(factorial(k)*factorial(n-k));
   }
/*
   public static int choose(int n, int k) {
       if (k == 0 || k == n) {
           return 1;
       }
       return choose(n-1, k) + choose(n-1, k-1);
   }
*/
   public static void main(String[] args) {
       StdOut.print("Enter the number of values in the lottery: ");
       int n = Integer.parseInt(StdIn.readLine());
       StdOut.print("Enter the number of values chosen: ");
       int k = Integer.parseInt(StdIn.readLine());
       StdOut.printf("There are %,d possible combinations.", choose(n, k));
   }

}

Explanation / Answer

JAVA CODE

import stdlib.*;

public class GCD {
   public static int gcd(int a, int b)
   {
       if(b==0)
           return a;
       return gcd(b, a%b);
   }
   public static void main(String[] args)
   {
       StdOut.print("Enter a: ");
       int a=Integer.parseInt(StdIn.readLine());
       StdOut.println("Enter b: ");
       int b=Integer.parseInt(StdIn.readLine());
       StdOut.printf("GCD is: %d",gcd(a, b));
   }

}

NOTE: As per the direction, a new class named GCD has been created with a method gcd() into it computing the gcd of two input number from main method. Used stdlib package into it.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote