## Recursion program in Java

A Recursion is function which call itself. This enables the function to repeat itself several times, outputting the result and the end of each iteration.
A class Recursion has been defined to find the Fibonacci series upto a limit. Some of the members of the class are given below:
Class Name : Recursion
Data Members/instance variables : f1, f2, fib, limit
Member functions/methods :
Recursion() : constructor to assign f1,f2,fib with appropriate values.

void input() : to accept the limit of the series.
int fibo(int n) : to return the nth(limit0 Fibonacci term using recursive technique.
void genearate_fibseries() : to generate the Fibonacci series upto the given limit.
Specify the class Recursion giving details of the constructorint fibo() , void generate_fibseries(). You may assume other functions are written for you and you need not write the main function.

Solution:
Recursive method execute a little slower than the iterative method. Many recursive calls to a method could cause a stack overrun. Because storage for parameters and local variables, it is possible that the stack could be exhausted. If this occurs, the java run-time system will cause an exception. With  the help of recursive method  most of algorithm become simpler and clearer than  iterative method. For example, the QuickSort sorting algorithm is quite difficult to implement in an iterative way.  Fibonacci is simple in Recursive method.

import java.io.*;
class RecursionFibonacciSeries
{
int f1,f2,fib,limit;
RecursionFibonacciSeries() //Constructor method
{
f1=0;
f2=1;
fib=0;
limit=0;
}
void input()throws IOException        // input limit of the series
{
System.out.print("Enter the limit : ");
}
int fibo(int n) //Recursive function generating the 'nth' term of Fibonacci Series
{
if(n<=1)
return f1;
else if(n==2)
return f2;
else
return (fibo(n-1)+fibo(n-2));
}
void generate_fibseries() //Function generating all the Fibonacci Series numbers upto 'n' terms
{
System.out.println("The Fibonacci Series is:");
for(int i=1;i<=limit;i++)
{
fib=fibo(i);
System.out.print(fib+"  ");
}
}
public static void main(String args[])throws IOException
{
// Creating object of the class
RecursionFibonacciSeries ob=new RecursionFibonacciSeries();
ob.input();
ob.generate_fibseries();
}
} // main method end
Output:
Enter the limit : 10
The Fibonacci Series is:
0 1 1 2 3 5 8 13 21 34

You may also interestd in

## Java Program: Important interview QA

Java Program: Important interview QA: Q. What is the difference between an argument and a parameter? Ans: While defining method, variables passed in the method are called pa...

## Java Swing Login Diloge Box

Java Swing program for creating buttons.

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public static void main(String[] args)  // main method started
{
JFrame frame = new JFrame("Demo application");
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
placeComponents(panel);

frame.setVisible(true);
}   // main method end

private static void placeComponents(JPanel panel) {

panel.setLayout(null);

JLabel userLabel = new JLabel("User");
userLabel.setBounds(10, 10, 80, 25);

JTextField userText = new JTextField(20);
userText.setBounds(100, 10, 160, 25);

JButton registerButton = new JButton("register");
registerButton.setBounds(180, 80, 80, 25);
}

}

Output:

Java swing output

## Java program for Stack

Stack is a memory container where data inserted and removed according to LIFO(Last in First Out) method. There are two operation in Stack, one is push which add the data and another is pop which remove the data. Suppose in a Bucket when we keep plate one by one. We can only pick that plat which inserted last.
Courtesy : en.wikipedia.org

/**
* Stack example
* Inspireskills
*/

public class StackExample {
private static final int capacity = 3;
int arr[] = new int[capacity];
int top = -1;

public void push(int pushedElement) {
if (top < capacity - 1) {
top++;
arr[top] = pushedElement;
System.out.println("Element " + pushedElement
+ " is pushed to Stack !");
printElements();
} else {
System.out.println("Stack Overflow !");
}
}

public void pop() {
if (top >= 0) {
top--;
System.out.println("Pop operation done !");
} else {
System.out.println("Stack Underflow !");
}
}

public void printElements() {
if (top >= 0) {
System.out.println("Elements in stack :");
for (int i = 0; i <= top; i++) {
System.out.println(arr[i]);
}
}
}

public static void main(String[] args) {
StackExample stackDemo = new StackExample();

stackDemo.push(9);
stackDemo.push(2);
stackDemo.push(7);
stackDemo.push(6);
stackDemo.pop();
stackDemo.pop();
stackDemo.pop();
stackDemo.pop();
}

}

Output
Stack Underflow !
Element 9 is pushed to Stack !
Elements in stack :
9
Element 2 is pushed to Stack !
Elements in stack :
9
2
Element 7 is pushed to Stack !
Elements in stack :
9
2
7
Stack Overflow !
Pop operation done !
Pop operation done !
Pop operation done !
Stack Underflow !