In: Computer Science
How to print these methods in a separate main class?
need the output to look like this:
Enter three integers whose GCD is to be found ->
Enter an integer n to find the nth Fibonacci number ->
Enter the base and exponent , an integer, of a power ->
Enter two positive integers 1 and j where i < j ->
gcd() =
fib() =
(a number ^ a number) = a number
There are ___ palindromic numbers between (a number) and (a number)
package recursiveauxiliarymath;
public class RecursiveAuxiliaryMath {
public static boolean recIsPalindrome(String num, int i, int
j){
if(i>=j){
return true;
}
else{
if(num.charAt(i)!=num.charAt(j)){
return false;
}
else{
recIsPalindrome(num,i+1,j-1);
}
}
return true;
}
public static long recFibonacci(int n)
{
if (n<=2)
return 1;
else
{
long fib;
fib = recFibonacci(n - 1) + recFibonacci (n - 2);
return fib;
}
}
public static int recGCD(int a, int b)
{
if(b!=0){
return recGCD(b,b%a);
}
else{
return a;
}
}
public static double recPowInt (double a, int n)
{
if(n==1){
return a;
}
else{
return recPowInt(a*a,n-1);
}
}
}
Hi, Please find my implementation.
Please let me know in case of any issue.
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 <= 2)
return 1;
else{
long fib;
fib = recFibonacci(n-1) + recFibonacci(n-2);
return fib;
}
}
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 -> 30
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(30) = 832040
-4.5^-3 = -0.010973936899862825
There are 108 palindrome numbers beween 1 and 1000
*/