Вы находитесь на странице: 1из 6

Recursion

Q : Write a program to obtain a number from console and calculate and display the factorial of the given number using recursive technique. import java.util.*; class FactorialRecursive { public static void main() { Scanner in = new Scanner(System.in); System.out.print("Enter a number : "); int num = in.nextInt(); FactorialRecursive obj = new FactorialRecursive(); System.out.println("Factorial of "+num+" is : "+obj.fact(num)); } int fact(int n) { if(n==1) return 1; else return n*fact(n-1); } } Output : Enter a number : 5 Factorial of 5 is : 120 Enter a number : 7 Factorial of 7 is : 5040

Q : Write a program to find the sum of n natural numbers using a recursive function sum import java.util.*; class SumRecursive { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.print("Enter the value of n : "); int n = in.nextInt(); int sum = sum(n); System.out.println("Sum of first "+n+" natural number(s) is : "+sum); } private static int sum(int n) { if(n==1) return 1; else return(n+(sum(n-1))); } } Output : Enter the value of n : 5 Sum of first 5 natural number(s) is : 15 Enter the value of n : 10 Sum of first 10 natural number(s) is : 55

Q : Write a program to find the binary equivalent of a decimal number using recursion. import java.util.*; class Dec2BinRecursive { int b=0,i=0; public static void main () { System.out.print("Enter a number to find its binary equivalent : "); Scanner in = new Scanner(System.in); int num = in.nextInt(); Dec2BinRecursive obj = new Dec2BinRecursive(); obj.bin(num); } void bin(int n) { if(n>0) { int rem = n%2; b=b+(int)(Math.pow(10,i)*rem); i++; bin(n/2); } else { System.out.print("Binary Equivalent of the given number is : " +b); } } } Output : Enter a number to find its binary equivalent : 10 Binary Equivalent of the given number is : 1010

Q : Write a program to find the value of x to the power y using recursion. import java.util.*; import java.util.*; class PowerRecursive { public static void main() { Scanner in = new Scanner(System.in); System.out.print("Enter as x^y : "); StringTokenizer s = new StringTokenizer(in.nextLine(),"^"); int x = Integer.parseInt(s.nextToken()); int y = Integer.parseInt(s.nextToken()); PowerRecursive obj = new PowerRecursive(); System.out.println(x+" to the power "+y+" is = "+obj.pow(x,y)); } int pow(int n,int p) { if(p==1) return n; else return n * pow(n,p-1); } } Output : Enter as x^y : 6^3 6 to the power 3 is = 216 Enter as x^y : 5^5 5 to the power 5 is = 3125

Q : Write a program to find the sum of Fibonacci series up to n terms. import java.util.*; class FibSRecursive { public static void main() { Scanner in = new Scanner(System.in); System.out.print("Enter the number of terms : "); int num = in.nextInt(); FibSRecursive obj = new FibSRecursive(); System.out.print("The sum of Fibonacci series up to "+num+" terms is : "+obj.fib(num)); } int fib(int n) { if(n<=1) return n; else return fib(n-1)+fib(n-2) ; } } Output : Enter the number of terms : 5 The sum of Fibonacci series up to 5 terms is : 5 Enter the number of terms : 6 The sum of Fibonacci series up to 6 terms is : 8 Enter the number of terms : 10 The sum of Fibonacci series up to 10 terms is : 55

Вам также может понравиться