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

Write a recursive method to compute the greatest common divisor of two integers

ID: 3934789 • Letter: W

Question

Write a recursive method to compute the greatest common divisor of two integers m and n as follows The gcd(m, n) can he computed recursively as: If m % n is 0, then gcd(m, n) is n. Otherwise gcd(m, n) is gcd(n, m % n) Write a main method that prompts the user to enter two integers, computes their greatest common divisor by calling your gcd method, and displays the result. Write a recursive method that displays the digits of an integer in reverse using the following header: public static void reverseDisplay(int value) For example, reverseDisplay(12345) should display 54321. Write a main method that prompts the user to enter an integer and displays it in reverse by calling the reverseDisplay method.

Explanation / Answer

// program to find gcd of two numbers

import java.util.Scanner;
public class Test{
  
    static int gcd(int m,int n)//recursive method
    {
        if(m%n==0)
        return n;
        else
        return gcd(n,m%n);
    }
  
  
     public static void main(String []args){
         
    int m,n;
    Scanner sc=new Scanner(System.in);// Creation of object of class Scanner
     System.out.println("enter any two number");
     m=sc.nextInt();//read a value of m
     n=sc.nextInt();
     System.out.println("Gcd of given numbers"+gcd(m,n));
         }
}

output:

---------------------

sh-4.3$ javac Test.java                                                                                                       

sh-4.3$ java Test                                                                                                             

enter any two number                                                                                                          

12                                                                                                                            

4                                                                                                                             

Gcd of given numbers4  

--------------------------------------------------

//program2 code

import java.util.Scanner;
import java.lang.*;
class Program2{

static int printreverse(int number,int length)//
    {
            if (length == 1)
                 return number;
            else
                 return (((number % 10) * (int)Math.pow(10, length - 1)) + printreverse(number / 10, --length));

    }
    public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    int number,t,length=0;
    System.out.println("enter any number");
    number=sc.nextInt();
    // to find length of a number
    t=number;
        while(t!=0)
    {
        length++;
        t=t/10;
            }
    System.out.println("the given number in reverse is"+printreverse(number,length));
       }
}

output

sh-4.3$ javac Program2.java                                                                                                    

sh-4.3$ java Program2                                                                                                          

enter any number                                                                                                               

1234                                                                                                                           

the given number in reverse is4321                                                                                             

sh-4.3$                                                                                                                        

          

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