Create a project named RecursiveAuxiliaryMath that illustrates the use of recurs
ID: 3790916 • Letter: C
Question
Create a project named RecursiveAuxiliaryMath that illustrates the use of recursion. This project will have the following: A class named RecursiveAuxiliaryMath that has the following static methods public static boolean recIsPalindrome (String num, int i, int j) public static long recFibonacci (int n) public static int recGCD(int a, int b) public static double recPowInt(double a, int n) The main class, named RecursiveAuxiliaryMathDemo, with one method: output: enter three integers whose GCD is to be found rightarrow 120 90 -75 Enter an integer n to find the nth Fibonacci number rightarrow 30 Enter the base and exponent, an integer, of a power rightarrow -4.5 -3 Enter two positive integers 1 and j where iExplanation / Answer
Hi, Please find my implementation.
import java.util.Scanner;
public class RecursiveAuxiliaryMath {
public static boolean recIsPalindrome(String num, int i, int j){
if( i > j)
return false;
if( i == j) // one digit
return true;
if(num.charAt(i) == num.charAt(j)){
if(i+1 == j) // only two digits
return true;
return recIsPalindrome(num, i+1, j-1);
}else{
return false;
}
}
public static long recFibonacci(int n){
if(n == 0)
return 1;
if( n == 1)
return 1;
return ((long)n)*recFibonacci(n-1);
}
public static int recGCD(int a, int b){
if (a == 0)
return b;
return recGCD(b%a, a);
}
public static double recPowInt(double a, int n){
double temp;
if( n == 0)
return 1;
temp = recPowInt(a, n/2);
if (n%2 == 0)
return temp*temp;
else
{
if(n > 0)
return a*temp*temp;
else
return (temp*temp)/a;
}
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter three digits whose GCD is to be found -> ");
int a1 = sc.nextInt();
int a2 = sc.nextInt();
int a3 = sc.nextInt();
int gcd = Math.abs(recGCD(a1, a2));
gcd = Math.abs(recGCD(gcd, a3));
System.out.print("Enter an integer n to ind the nth Fibonacci number -> ");
int a4 = sc.nextInt();
long fib = recFibonacci(a4);
System.out.print("Enter the base and exponenet, an integer, f a power -> ");
double a5 = sc.nextDouble();
int a6 = sc.nextInt();
double pow = recPowInt(a5, a6);
System.out.print("Enter two positive numbers i and jhere i < j -> ");
int a7 = sc.nextInt();
int a8 = sc.nextInt();
int count = 0;
for(int k = a7; k<=a8; k++){
String num = Integer.toString(k);
if(recIsPalindrome(num, 0, num.length()-1)){
count++;
}
}
System.out.println();
System.out.println("gcd("+a1+", "+a2+", "+a3+") = "+gcd);
System.out.println("fib("+a4+") = "+fib);
System.out.println(a5+"^"+a6+" = "+pow);
System.out.println("There are "+count+" palindrome numbers beween "+a7+" and "+a8);
}
}
/*
Sample run:
Enter three digits whose GCD is to be found -> 120 90 -75
Enter an integer n to ind the nth Fibonacci number -> 10
Enter the base and exponenet, an integer, f a power -> -4.5 -3
Enter two positive numbers i and jhere i < j -> 1 1000
gcd(120, 90, -75) = 15
fib(10) = 3628800
-4.5^-3 = -0.010973936899862825
There are 108 palindrome numbers beween 1 and 1000
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.