0% found this document useful (0 votes)
12 views

Recursion

This JSON document describes a Java program that implements a recursive calculator with 16 different operations using methods like add, subtract, multiply, divide, power, fibonacci, digit sum, string reversal, checking palindromes, and more. The program uses a menu driven approach and switch case statements to call the relevant method based on the user's selection.

Uploaded by

sahar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Recursion

This JSON document describes a Java program that implements a recursive calculator with 16 different operations using methods like add, subtract, multiply, divide, power, fibonacci, digit sum, string reversal, checking palindromes, and more. The program uses a menu driven approach and switch case statements to call the relevant method based on the user's selection.

Uploaded by

sahar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

package quizrecursion;

import java.util.Scanner;

public class RecursionCalc {

static Scanner input = new Scanner(System.


Scanner(System.in);

public static void main(String[]


(String[] args) {

int z = 0;
int x, y;
menu();
System.out.print("Select
System. .print("Select an operation: ");
z = input.nextInt();
.nextInt();
while (z >= 1 && z <= 15) {

switch (z) {

case 1:
System.out.print("Enter
System. .print("Enter two numbers: ");
x = input.nextInt();
.nextInt();
y = input.nextInt();
.nextInt();
System.out.println(x
System. .println(x + " + " + y + " = " + add(
(x, y)
));
break;
case 2:
System.out.print("Enter
System. .print("Enter two numbers: ");
x = input.nextInt();
.nextInt();
y = input.nextInt();
.nextInt();
System.out.println(x
System. .println(x + " - " + y + " = " + sub(x, y));
break;

case 3:
System.out.print("Enter
System. .print("Enter two numbers: ");
x = input.nextInt();
.nextInt();
y = input.nextInt();
.nextInt();
System.out.println(x
System. .println(x + " x " + y + " = " + prod(x, y));
break;

case 4:
System.out.print("Enter
System. .print("Enter two numbers: ");
x = input.nextInt();
.nextInt();
y = input.nextInt();
.nextInt();
System.out.println(x
System. .println(x + " / " + y + " = " + div(x,
, y));
break;

case 5:
System.out.print("Enter
System. .print("Enter two numbers: ");
x = input.nextInt();
.nextInt();
y = input.nextInt();
.nextInt();
System.out.println("The
System. .println("The base " + x + " to the power of " + y + " is: " + pow(x,
, y));
break;

case 6:
System.out.print("Enter
System. .print("Enter an integer number: ");
int f = input.nextInt();
.nextInt();
System.out.println("Fib("
System. .println("Fib(" + f + ") " + "= " + fib(f
f));
break;
case 7:
System.out.print("Enter
System. .print("Enter an integer number: ");
int s = input.nextInt();
.nextInt();
System.out.println("Sum("
System. .println("Sum(" + s + ") " + "= " + sum(s));
break;

case 8:
String str;
Scanner read = new Scanner(System.
Scanner(System.in);
System.out.println("Enter
System. .println("Enter a string: ");
str = read.nextLine();
System.out.println("original:
System. .println("original: " + str + "\nreversed:
" reversed: " + reverseString(str))
);
break;

case 9:
System.out.println("Enter
System. .println("Enter a number: ");
int num = input.nextInt();
.nextInt();
System.out.println("Sum
System. .println("Sum of digits = " + digitSum(
(num))
);
break;
case 10:

System.out.println("Enter
System. .println("Enter a string: ");
String string = input.next();
if (isPalindrome(string))
(string)) {
System.out.println(string
System. .println(string + " is a Palindrom");
} else {
System.out.println(string
System. .println(string + " is not a Palindrom");
}
break;

case 11:
System.out.println("Enter
System. .println("Enter a number: ");
x = input.nextInt();
.nextInt();
System.out.println("factorial
System. .println("factorial = " + factorial(x)
));
break;

case 12:
System.out.println("Enter
System. .println("Enter numbers separated by /: ");
String arr = input.
.next(
();

String[] array = arr.split("/");


int[]
[] intArray = new int[array.length];
[array.length];

for (int i = 0; i < array.length; i++) {


if (!array[i].isEmpty()) { // Check if the string is not empty
intArray[i] = Integer.parseInt(array[i]);
Integer. (array[i]);
} else {
// Handle empty string case, you can set it to a default value or ignore it
// For example, setting it to zero:
intArray[i] = 0; // or any other default value you prefer
}
}
System.out.println("The
System. .println("The sum of numbers= " + sumArray(intArray,
(intArray, 0));
break;

case 13:
System.out.println("Enter
System. .println("Enter a String: ");
String strr = input.next
t();

System.out.println("The
System. .println("The length of " + "(" + strr + ")" + " is: " + countString(strr));
break;

case 14:
System.out.println("Enter
System. .println("Enter a number: ");
int number2 = input.nextInt();
.nextInt();
System.out.println("Reversing
System. .println("Reversing Number: " + reverseNum(number2));
(number2));
break;
case 15:
System.out.println("Enter
System. .println("Enter a number to check if it is Palindrome: ");
x = input.nextInt();
.nextInt();
System.out.println("it
System. .println("it is " + numIsPalindrom(x));
break;
case 16:
System.out.println("Thank
System. .println("Thank you for using the Advanced Calculator. See you soon :)");
System.exit(0
System. 0);

menu(
();
System.out.print("Select
System. .print("Select an operation: ");
z = input.nextInt();
.nextInt();

}
}

static void menu() {


System.out.println("*********************
System. .println("*********************\n Advanced Calculator\n*********************"
.println("********************* *********************"
+ "\n1.
" Add"
+ "\n2.
" Subtract\n"
+ "3. Multiply\n"
Multiply
+ "4. Divide\n"
Divide
+ "5. Power\n"
Power
+ "6. Fib\n"
Fib
+ "7. Sum\n"
Sum
+ "8. Reverse String\n"
String
+ "9. digit Sum\n"
Sum
+ "10.isPalindrome\n"
"10.isPalindrome
+ "11.factorial\n"
"11.factorial
+ "12.Sum of Array\n"
Array
+ "13.String length\n"
length
+ "14.Reverse Number\n"
Number
+ "15.Number isPalindrome\n"
isPalindrome
+ "16.Search for a number in a list\n"
list
+ "17.Search for a char in a list\n"
list
+ "18.Exit\n------------------");
"18.Exit ------------------");

static double crazySum(double n) {


if (n == 0 || n == 1) {
return 0;
}
return ((n - 1) / n) + crazySum(n - 1);

static int add(int x, int y) {


if (y == 0) {
return x;
}
return 1 + add(x,
(x, y - 1);
}

public static int sub(int x, int y) {


if (y == 0) {
return x;
}
return sub(x,
(x, y - 1) - 1;
}

static int prod(int x, int y) {


if (y == 0) {
return 0;
}
if (y == 1) {
return x;
}
return x + prod(x,
(x, y - 1);
}

static int div(int x, int y)


) {
if (y == 0) {
throw new ArithmeticException("Division by zero is not possible");
}
if (x
x < y)
) {
return 0;
}
if (y == 1) {
return x;
}
return 1 + div(x
(x - y, y);
}

// dividing with reminder


static String divide(int x, int y, int z)
) {
// base case
if (x
x < y)
) {
return z + "" + x;

}
// recursive case
return divide(x
(x - y, y, z + 1);
}

static int pow(int base


e, int exp
p) {
if (exp == 0) {
return 1;
}
if (exp == 1) {
return base
e;
}
return base * pow(base,
(base, exp - 1);
}

static int fib(int n) {


if ((n == 0) || (n == 1)) {
return n;
}
return fib(n - 1) + fib(n
n - 2);
}

static int sum(int n) {


if (n == 0) {
return 0;
} else {
return n + sum(n - 1);
}
}

static String reverseString(String


(String s) {
if (s.isEmpty()) {
return s;
} else {
return reverseString(s.substring(1))
(s.substring(1)) + s.charAt(0);
}

// or
/*
if ((s == null ) || (s.length() <= 1)){
System.out.println(s);
} else{
System.out.println(s.charAt(s.length() -1));
reverseString(s.substring(0 , s.length() -1)) ; */
}

static int digitSum(int n) {


if (n == 0) {
return 0;
}
return (n % 10 + digitSum(n / 10))
);
}

static boolean isPalindrome(String


(String s) {
if (s.length() == 0 || s.length() == 1) {
return true;
}
if (s.charAt(0) == s.charAt(s.length() - 1)) {
return isPalindrome(s.substring(1,
(s.substring(1, s.length() - 1));
}
return false;

static int factorial(int n) {


if (n == 1 || n == 0) {
return 1;
}
return n * factorial(n
n - 1);
}

static int sumArray(int[]


] arr,
, int index) {
if (index == 0) {
return arr[
[0];
}
return arr[index] + sumArray(arr,
(arr, index - 1);

/* or
if (index < 0 || index >= arr.length) {
return 0;
}
return arr[index] + sumArray(arr, index + 1); */
}

static int countString(String


(String str) {
//base
if (str.isEmpty()) {
return 0;
}
return 1 + countString(str.substring(1));
(str.substring(1));
}

static int reverseNum(int number) {


if (number < 10) {
return number;
}

int lastDigit = number % 10; // Extract the last digit


int remainingDigits = number / 10; // Remove the last digit

// Recursively reverse the remaining digits and concatenate the last digit
return Integer.parseInt(Integer.toString(lastDigit)
(lastDigit)
+ Integer.toString(reverseNum(remainingDigits)));
Integer. (remainingDigits)));
/* or
if (number < 10) {
return number;
}
return Integer.parseInt(Integer.toString(number % 10) +
Integer.toString(reverseNum(number / 10))); */
}

static boolean numIsPalindrom(int num) {


String numStr = Integer.toString(num
Integer. m);
return isPaliNum(numStr
r);
}

static boolean isPaliNum(String


(String str) {
int length = str.length();
if (length <= 1) {
return true;
}
if (str.charAt(0) == str.charAt(length - 1)) {
return isPaliNum(str.substring(1,
(str.substring(1, length - 1));
}
return false;
}

// version 2 of is palindrome digit


static boolean isPaliNum2(int[
[] num,, int start, int last) {
if (start == last) {
return true;
}
if ((last > 0) && (
(start < num.length)) {

You might also like