Skip to content

Instantly share code, notes, and snippets.

@incognitokshitij
Created March 8, 2021 14:26
Show Gist options
  • Select an option

  • Save incognitokshitij/102eac42c13479969ff2d38c7bbf34f3 to your computer and use it in GitHub Desktop.

Select an option

Save incognitokshitij/102eac42c13479969ff2d38c7bbf34f3 to your computer and use it in GitHub Desktop.
/* Created by IntelliJ IDEA.
* Author: Kshitij Varshney (kshitijvarshne1)
* Date: 06-Mar-21
* Time: 1:32 AM
* File: Factorial.java
*/
package March.mar06_21_NK;
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
System.out.println(factorial(number));
}
private static int factorial(int n) {
if (n == 1 || n == 0) {
return 1;
}
return n * factorial(n - 1);
}
}
/* Created by IntelliJ IDEA.
* Author: Kshitij Varshney (kshitijvarshne1)
* Date: 06-Mar-21
* Time: 1:36 AM
* File: Fibonacci.java
*/
package March.mar06_21_NK;
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(fibonacci(n));
for (int i = 0; i < n + 1; i++) {
System.out.print(fibonacci(i) + " ");
}
}
private static int fibonacci(int n) {
if (n == 0) {
return 0;
}
if (n == 1 || n == 2) {
return 1;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
/* Created by IntelliJ IDEA.
* Author: Kshitij Varshney (kshitijvarshne1)
* Date: 06-Mar-21
* Time: 1:51 AM
* File: FibonacciUsingIteration.java
*/
package March.mar06_21_NK;
import java.util.Scanner;
public class FibonacciUsingIteration {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = 0;
int b = 1;
System.out.print(a + " " + b + " ");
for (int i = 2; i <= n; i++) {
System.out.print(a + b + " ");
int c = a + b;
a = b;
b = c;
}
}
}
/* Created by IntelliJ IDEA.
* Author: Kshitij Varshney (kshitijvarshne1)
* Date: 06-Mar-21
* Time: 3:17 PM
* File: SecondLargestElement.java
*/
package March.mar06_21_NK;
import java.util.Stack;
public class SecondLargestElement {
public static void main(String[] args) {
int[] a = {6, 9, 3, 4, 5};
System.out.println(find(a));
}
private static int find(int[] a) {
Stack<Integer> stack = new Stack<>();
for (int i : a) {
if (stack.size() < 2) {
if (stack.size() == 0) {
stack.push(i);
} else {
if (stack.peek() > i) {
stack.push(i);
} else {
int x = stack.pop();
stack.push(i);
stack.push(x);
}
}
} else if (stack.peek() < i) {
int temp = stack.pop();
if (stack.peek() < i) {
int x = stack.pop();
stack.push(i);
stack.push(x);
} else {
stack.push(i);
}
}
}
return stack.pop();
}
}
/* Created by IntelliJ IDEA.
* Author: Kshitij Varshney (kshitijvarshne1)
* Date: 06-Mar-21
* Time: 1:22 AM
* File: TowerOfHanoi.java
*/
package March.mar06_21_NK;
import java.util.Scanner;
public class TowerOfHanoi {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numberOfDisc = sc.nextInt();
towerOfHanoi(numberOfDisc, 'A', 'H', 'D');
}
public static void towerOfHanoi(int n, char s, char h, char d) {
if (n >= 1) {
towerOfHanoi(n - 1, s, d, h);
System.out.println(n + " " + s + " -> " + d);
towerOfHanoi(n - 1, h, s, d);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment