Showing posts with label example java. Show all posts
Showing posts with label example java. Show all posts

Wednesday, December 28, 2011

Fibonacci Series Program in Java Programming

It is a java program to generate Fibonacci series. In this program user input a number and prints the Fibonacci series for accepted number.

Java Fibonacci Series Program

Source Code
import java.lang.*;
import java.util.*;
class fibonacci
{
 public static void main(String arg[])
 {
        int a=0,b=1,c=0,i,num;
 Scanner scan = new Scanner(System.in);
 System.out.print("enter any number for fibonacci series:  ");
 num = scan.nextInt();
 System.out.println("Fibonacci Series is:"); 
 for(i=0;i<num;i++)
 {
  System.out.print(c+"\t");       
  a=b;
  b=c;
  c=a+b;
  }
 }
}

Output

In this java program create a class Fibonacci and use concept of Scanner class to accept a number from user. This program generate Fibonacci series for input.

Friday, December 23, 2011

Basic Java Program To Print Hello World

It is a basic JAVA program to say to the world "Hello World". It is example for beginners to understand basic java code.
The name Java was derived at a local coffee shop frequented by some of the members and it's related to coffee material.
So In this basic program print Hello WORLD inside the coffee mug.



First install JDK(JAVA Development Kit) with latest version because to run java program need JVM(java virtual machine) when you install JDK then JVM installed in your system.
Java Virtual Machine(JVM) makes JAVA as platform independent.


Hello World JAVA Program

Program Code
class hello   //create class hello
{
  public static void main(String[] args)  //main method 
  {
    System.out.println("\nExample to print HELLO WORLD in Coffee Mug\n");
    System.out.println(" _______\n|       |\n|       |__\n| HELLO"
+" |  |\n| WORLD |  |\n|       |__|    \n|       |\n|_______|");
  }
}

Save Program
Type the program code in notepad and save Java program with classname.java. In this program save as hello.java.
File name and class name must be same.

Compile Program
javac hello.java
When compile program then it creates bytecode named with hello.class. class file created in same location where program stored.
Bytecode is a code which as same as machine language code.

Exicute the Program
java hello
Now we execute the bytecode in the Java interpreter with using this command.

Output