Sunday, May 4, 2014

Fibonacci series using Recursion

This Java Program prints the fibonacci of a given number using recursion.
In fibonacci series, each number is the sum of the two preceding numbers.
For Example : 0, 1, 1, 2, 3, 5, 8, …

The following program returns the nth number entered by user residing in the fibonacci series.
Here is the source code of the Java program to print the nth number of a fibonacci number.

 Code :

public class Fibonacci {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the nth number in the fibonacci series");
        int n = scanner.nextInt();
       
        System.out.println("The "+n+"th no of fibonacci series is "+ fibo(9));

    }

    private static int fibo(int n) {
       
        if (n <= 1) {
            return n;
        }

        return (fibo(n - 1) + fibo(n - 2));

    }
}


 Ouput :


Enter the nth number in the fibonacci series
9
The 9th no of fibonacci series is 34

Thursday, November 25, 2010

This is a simple applet program

//importing the packages
import java.applet.*;
import java.awt.*;

//this will include the class file for running in the browser
/*<applet code="FirstApplet.java" width=100 height=100 ><
/applet >*/

//main function begins here
public class FirstApplet extends Applet{

public void paint(Graphics g){
//it will prints the string in the applet viewer
g.drawString("Welcome in Java Applet.",40,20);
}
}


Save the above program as FirstApplet.java

compile the program by using javac FirstApplet.java

execute the program by

appletviewer FirstApplet.java or appletviewer FirstApplet.html

the out put of the above program will be




Wednesday, October 27, 2010

Method in java

A java method is a series of statements that perform some repeated task. Instead of writing ten lines of code we can put those ten lines in a method and just call it in one line. It is like a shortcut.

Java methods are similar to functions or procedures in other programming languages.

on the other hand, a Java method is a set of Java statements which can be included inside a Java class.

The syntax of the java method is as follows

access-specifier return-type(data-type) method-name(arguments)
{
statement1;
statement2;//body of the function or method
statement3;
}

access-specifier will be public, private or protected

return-type is the one the data type. such as int, float,etc...

method-name is any user defined name.

arguments are the value we need to pass to the function

The example program
/**
*
* @author prabhu
*/
public class addition {
//variable declaration
int a,b,c;

public void add()
{
a=10;
b=20;
c=a+b;
System.out.println("a+b="+c);
}

public static void main(String ar[])
{
//creating object
addition add=new addition();

//calling the method in the class using the object
add.add();
}


}

simple java program

This is a simple java program

/*

This is a simple java program
Save this file as simple.java

*/


public class simple
{
//the program begins here
public static void main(String ar[])
{
System.out.println("welcome to java program");
}
}

the output of the above program is

welcome to java program