mynotes: day4: recursion hell

This commit is contained in:
Evan Ferrao 2024-08-09 23:42:01 +05:30
parent 968ace4753
commit b88bf1e6b6
No known key found for this signature in database
GPG Key ID: F01DEB4D7CFC9B52
5 changed files with 127 additions and 0 deletions

25
Recursion/Fibonacci.java Normal file
View File

@ -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();
}
}

27
Recursion/PrintMtoN.java Normal file
View File

@ -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, lets 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();
}
}

View File

@ -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, lets 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();
}
}

27
Recursion/PrintNtoM.java Normal file
View File

@ -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, lets 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();
}
}

View File

@ -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, lets take the value of n to be 4.
int n = 8;
func(n,4);
}
}