mirror of https://github.com/evanferrao/dsa
mynotes: day4: recursion hell
This commit is contained in:
parent
968ace4753
commit
b88bf1e6b6
|
|
@ -0,0 +1,25 @@
|
||||||
|
// write code to find the nth fibonacci number using recursion
|
||||||
|
import java.util.Scanner;
|
||||||
|
public class Fibonacci{
|
||||||
|
static int first=0,second=1,next=0;
|
||||||
|
static void printFibonacci(int count){
|
||||||
|
if(count>0){
|
||||||
|
next = first + second;
|
||||||
|
first = second;
|
||||||
|
second = next;
|
||||||
|
System.out.print(" "+next);
|
||||||
|
printFibonacci(count-1);
|
||||||
|
|
||||||
|
// can you assign proper variable names to n1, n2 and n3?
|
||||||
|
// n1 should be named as first, n2 as second and n3 as next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void main(String args[]){
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("Enter the number of terms: ");
|
||||||
|
int count = sc.nextInt();
|
||||||
|
System.out.print(first+" "+second);//printing 0 and 1
|
||||||
|
printFibonacci(count-2);//n-2 because 2 numbers are already printed
|
||||||
|
sc.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
class PrintMtoN {
|
||||||
|
|
||||||
|
static void func(int i, int n){
|
||||||
|
|
||||||
|
// Base Condition.
|
||||||
|
if(i<n) return;
|
||||||
|
System.out.println(i);
|
||||||
|
|
||||||
|
// Function call to print i till i decrements to 1.
|
||||||
|
func(i-1,n);
|
||||||
|
|
||||||
|
}
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// Here, let’s take the value of n to be 4.
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("Enter the value of m (Ending Variable): ");
|
||||||
|
int m = sc.nextInt();
|
||||||
|
System.out.println("Enter the value of n (Starting Variable): ");
|
||||||
|
int n = sc.nextInt();
|
||||||
|
//int n = 8;
|
||||||
|
func(m,n);
|
||||||
|
sc.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
class PrintMtoNUsingBacktracking {
|
||||||
|
|
||||||
|
static void func(int i, int n){
|
||||||
|
|
||||||
|
// Base Condition.
|
||||||
|
if(i>n) return;
|
||||||
|
|
||||||
|
// Function call to print(i+1) integers.
|
||||||
|
func(i+1,n);
|
||||||
|
System.out.println(i);
|
||||||
|
|
||||||
|
}
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// Here, let’s take the value of n to be 4.
|
||||||
|
//int n = 8;
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("Enter the value of m (Ending Variable): ");
|
||||||
|
int m = sc.nextInt();
|
||||||
|
System.out.println("Enter the value of n (Starting Variable): ");
|
||||||
|
int n = sc.nextInt();
|
||||||
|
func(n,m);
|
||||||
|
sc.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
// write a code to print N to one using recursion and function in java using backtracking
|
||||||
|
import java.util.Scanner;
|
||||||
|
class PrintNtoMUsingBacktracking {
|
||||||
|
|
||||||
|
static void func(int i, int n){
|
||||||
|
|
||||||
|
// Base Condition.
|
||||||
|
if(i<n) return;
|
||||||
|
|
||||||
|
// Function call to print(n-1) integers.
|
||||||
|
func(i-1,n);
|
||||||
|
System.out.println(i);
|
||||||
|
|
||||||
|
}
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// Here, let’s take the value of n to be 4.
|
||||||
|
System.out.println("Enter the value of n (Starting Variable): ");
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int n = sc.nextInt();
|
||||||
|
System.out.println("Enter the value of m (Ending Variable): ");
|
||||||
|
int m = sc.nextInt();
|
||||||
|
//int n = 8;
|
||||||
|
func(m,n);
|
||||||
|
sc.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
// write a code to print N to one using recursion and function in java using backtracking
|
||||||
|
|
||||||
|
class PrintNtoMUsingBacktracking {
|
||||||
|
|
||||||
|
static void func(int i, int n){
|
||||||
|
|
||||||
|
// Base Condition.
|
||||||
|
if(i<n) return;
|
||||||
|
|
||||||
|
// Function call to print(n-1) integers.
|
||||||
|
func(i-1,n);
|
||||||
|
System.out.println(i);
|
||||||
|
|
||||||
|
}
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// Here, let’s take the value of n to be 4.
|
||||||
|
int n = 8;
|
||||||
|
func(n,4);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue