Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create RecursiveFibo.java #4667

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions CPP/binarytree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Binary Tree in C++

#include <stdlib.h>

#include <iostream>

using namespace std;

struct node {
int data;
struct node *left;
struct node *right;
};

// New node creation
struct node *newNode(int data) {
struct node *node = (struct node *)malloc(sizeof(struct node));

node->data = data;

node->left = NULL;
node->right = NULL;
return (node);
}

// Traverse Preorder
void traversePreOrder(struct node *temp) {
if (temp != NULL) {
cout << " " << temp->data;
traversePreOrder(temp->left);
traversePreOrder(temp->right);
}
}

// Traverse Inorder
void traverseInOrder(struct node *temp) {
if (temp != NULL) {
traverseInOrder(temp->left);
cout << " " << temp->data;
traverseInOrder(temp->right);
}
}

// Traverse Postorder
void traversePostOrder(struct node *temp) {
if (temp != NULL) {
traversePostOrder(temp->left);
traversePostOrder(temp->right);
cout << " " << temp->data;
}
}

int main() {
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);

cout << "preorder traversal: ";
traversePreOrder(root);
cout << "\nInorder traversal: ";
traverseInOrder(root);
cout << "\nPostorder traversal: ";
traversePostOrder(root);
}
10 changes: 10 additions & 0 deletions Java/Factorial.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class FactorialExample{
public static void main(String args[]){
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
28 changes: 28 additions & 0 deletions Java/RecursiveFibo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.*;
public class fibonacci{
public static int fib(int n){
if(n==1){
return array[0];
}
// base cases
if(n==2){
return array[1];
}
else{
array[n-1] = fib(n-1) + fib(n-2);
return (array [n-1]);
}
}
public static void main(String args[]){
int n;
Scanner snr= new Scanner(System.in);
n=snr.nextInt();
snr.close();
array[0]=0;
array[1]=1;
System.out.println(fib(n));
// printing number in fibonacci series
}
static int array[]=new int[1000];
// Declaring global array large enough
}