Question

In: Computer Science

Recursion practice Write a Java program with functions for each of the following problems. Each problem...

Recursion practice

Write a Java program with functions for each of the following problems. Each problem should be solved by writing a recursive function. Your final program should not have any loops in it.

All of your solutions should be in a single .java file. The main function of the file should demonstrate each of your solutions, by running several tests and producing the corresponding outputs.

Write a recursive method to

1. calculate power of a give number

2. multiply m by n using only repeated addition

  1. Implement recursive Fibonacci: Return the nth number in the Fibonacci sequence (5 points) a. Demonstrate by printing the first 10 numbers
  2. Implement Euclid’s algorithm recursively.

a. Euclid’s algorithm to calculate the greatest common divisor of two positive integer         

numbers a and b (gcd(a,b)) is recursively defined as:

gcd(a,b) := a if a = b

gcd(a,b) := gcd(a - b, b) if a > b

gcd(a,b) := gcd(a, b - a) if b > a

  1. Return the length of a linked list
    1. You will need to implement a simple link list
    2. Include a length function that uses recursion to count the number of elements

Solutions

Expert Solution

import java.util.*;
public class Main
{
public static int calculatePower(int x,int y) //to calculate power(x,y)
{
if(y==0)
return 1;
else
return x*calculatePower(x,y-1);
}
public static int multiply(int m,int n)
{
       if(n>0)
           return m+multiply(m,n-1);//recursivly calling multiply()
       else
           return 0; //n<=0
}
public static int gcd(int a,int b) //to calculate gcd
{
if(a==b)
return a;
else if(a>b)
return gcd(a-b,b);
else
return gcd(a,b-a);
}
public static int fib(int n) //to get fibnocci series
{
if(n>1)
return fib(n-1)+fib(n-2);
else
return n; //n<=1
}
   public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       int n=sc.nextInt();
       int b=sc.nextInt();
       System.out.println(n+" power of "+b);
       System.out.println(calculatePower(n,b)); //calculatint Power
       System.out.println("multiplication of "+n+"x"+b);
       System.out.println(multiply(n,b)); //multiplication
       System.out.println("Greatest common divisor of "+n+" and "+b);
       System.out.println(gcd(n,b)); //to get gcd value
       System.out.println(n+"th Number in the fibnocci sequence is "+fib(n)); //get nth fibnocci number
       System.out.println("first 10 numbers in the fibnocci series are: ");
       System.out.println(fib(1)+"\n"+fib(2)+"\n"+fib(3)+"\n"+fib(4)+"\n"+fib(5)+"\n"+fib(6)+" \n"+fib(7)+" \n"+fib(8)+" \n"+fib(9)+"\n"+fib(10)); //printing first 10 numbers in fibnocci series without using loop (or) we can write another function for this.
       System.out.println("Single Linked List:");
       LinkedList L = new LinkedList();
L =L.insert(L, 10); //inserting elements to the list
L =L.insert(L, 20);
L =L.insert(L, 30);
L =L.insert(L, 40);
L =L.insert(L, 50);
L.printList(L); //print the list
       System.out.println();
System.out.println("Length of linked list is:");
System.out.println(L.count(L.head)); //length of list
      
   }
}
class LinkedList {
  
Node head; //starting node of list
static class Node { //static class to easy access
int data;
Node next;
Node(int d) // Constructor
{
data = d; // data to be stored in the node
next = null; //to create link betwwen nodes
}
}
public static LinkedList insert(LinkedList l, int data) // insert a new node
{
Node new_node = new Node(data);
new_node.next = null;
if (l.head == null) {
l.head = new_node;
}
else {
Node last = l.head;
while (last.next != null) {
last = last.next;
}
last.next = new_node;
}
return l;
}
public static int count(Node temp) //to count the number of elements in the list
{
if(temp==null)
return 0;
else
return 1+count(temp.next);
}
public static void printList(LinkedList list) //to print the list elements
{
Node n = list.head;
System.out.print("LinkedList: ");
while (n != null) {
System.out.print(n.data + " ");
n = n.next;
}
}
}

output:


Related Solutions

Java Recursion (Timelimit: 3 seconds) Problem Description Write a Java program to solve the following problem....
Java Recursion (Timelimit: 3 seconds) Problem Description Write a Java program to solve the following problem. Recursion may appear in various contexts and in different forms. For fast implementation, we should always aim at transforming recursions into a simpler form of computation. In this assignment, the task is to evaluate X(·), which is defined as follows:               |0,if m = 0 or n = 0               | X(m,n−1),if n is odd and m is even X(m,n) = | X(m−1,n),if m...
Write functions for each of the following problems. Each problem should be solved by writing a...
Write functions for each of the following problems. Each problem should be solved by writing a recursive function. Your final program should not have any loops in it. (a) Write a function that uses recursion to raise a number to a power. The function should take two arguments, the number to be raised to the power (floating point) and the power (a non-negative int). (10 Points) (b) Write a boolean function named isMember that takes two arguments: an array of...
java 2D array / recursion explain every method You are requested to write a Java program...
java 2D array / recursion explain every method You are requested to write a Java program of a simple Memory Management Unit. The program should allow the following: 1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit. 2. The user can delete a process by specifying a process ID. The program should do that and free...
Java Palindrome (Timelimit: 10seconds) Problem Description Write a Java program to solve the following problem. A...
Java Palindrome (Timelimit: 10seconds) Problem Description Write a Java program to solve the following problem. A palindromic number is an integer that is the same when the digits are reversed. For example, 121 and 625526 are palindromic, but 625 is not a palindromic number. Input: The input is in ‘palindrome.txt’. The first line of the input contains the line count m (1 ≤ m ≤ 1,000), which is the number of lines that follows the first line. Each of the...
For each problem below, write a java program to solve it, name your programs as PA2_1.java...
For each problem below, write a java program to solve it, name your programs as PA2_1.java and PA2_2.java. 1. We have a steam heating boiler whose bursting pressure is known, but we of course want to use it only at pressures well below this limit. Our engineers recommend a safety factor of 3; that is, we should never exceed a pressure that is one-third of the rated bursting pressure. Write a java program that reads in the rated bursting pressure...
To get some practice with recursion. You can do all this in one driver program. Write...
To get some practice with recursion. You can do all this in one driver program. Write a recursive method to compute the result of the Fibonacci sequence: Fibonacci(N) = Fibonacci(N -1) + Fibonacci(N-2) N == 0 is 0 N == 1 is 1 Testing: Display the result for Fibonacci(N) and the number of function calls for N = 2, 5 and 10.
please use java swing and recursion and the suggested method hints listed Objective: Write a program...
please use java swing and recursion and the suggested method hints listed Objective: Write a program in which draws (yes it actually makes a picture) a triangular fractal using recursion. This is best if done using a java applet. Suggested Methodology The idea for it is this First draw a filled equilateral triangle Next draw another filled equilateral triangle of a different color that’s upside down in the middle of that triangle Using the other triangles formed repeat step 2...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program that will print the number of words, characters, and letters read as input. It is guaranteed that each input will consist of at least one line, and the program should stop reading input when either the end of the file is reached or a blank line of input is provided. Words are assumed to be any non-empty blocks of text separated by spaces, and...
Solving Problems Using Recursion (Python): To solve the problem, you have to use recursion and cannot...
Solving Problems Using Recursion (Python): To solve the problem, you have to use recursion and cannot use for or while loops to solve the problems as well as not using global variables. 1. Create a function that takes a positive integer and returns it with neighboring digits removed. Do not convert the integer to a list. Ex. Input = [5555537777721] Output = [53721]
Write a program in python that implements quicksort, first using recursion and then without recursion.
Write a program in python that implements quicksort, first using recursion and then without recursion.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT