Write a recursive method to compute the greatest common divi

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.

Solution

// 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$                                                                                                                        

          

 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,
 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,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site