Tuesday, 27 October 2015

Fibonacci Series Using JAVA

Fibonacci Series Using JAVA


More Details About Fibonacci Series: Click Here

Program in JAVA

public class fibo{
public static void main(String args[]){
int no=10;
int next,prev,sum;
next=1;
prev=0;
for(int i=0;i<no;i++){
System.out.print(prev+"  ");
sum=next+prev;
prev=next;
next=sum;
}
}
}

Download Program File: Click Here

Output:


Pascal's Triangle Using JAVA

Pascal's Triangle Using JAVA


In mathematics, Pascal's triangle is a triangular array of the binomial coefficients. In much of the Western world it is named after French mathematician Blaise Pascal, although other mathematicians studied it centuries before him in India, Iran, China, Germany, and Italy.
The rows of Pascal's triangle (sequence A007318 in OEIS) are conventionally enumerated starting with row n = 0 at the top (the 0th row). The entries in each row are numbered from the left beginning with k = 0 and are usually staggered relative to the numbers in the adjacent rows. Having the indices of both rows and columns start at zero makes it possible to state that the binomial coefficient \tbinom{n}{k} appears in the nth row and kth column of Pascal's triangle. A simple construction of the triangle proceeds in the following manner: In row 0, the topmost row, the entry is \tbinom{0}{0} = 1 (the entry is in the zeroth row and zeroth column). Then, to construct the elements of the following rows, add the number above and to the left with the number above and to the right of a given position to find the new value to place in that position. If either the number to the right or left is not present, substitute a zero in its place. For example, the initial number in the first (or any other) row is 1 (the sum of 0 and 1), whereas the numbers 1 and 3 in the third row are added to produce the number 4 in the fourth row.
This construction is related to the binomial coefficients by Pascal's rule, which says that if
(x+y)^n=\sum_{k=0}^n{n \choose k}x^{n-k}y^{k}
then
 {n \choose k} = {n-1 \choose k-1} + {n-1 \choose k}
for any non-negative integer n and any integer k between 0 and n.
Pascal's triangle has higher dimensional generalizations. The three-dimensional version is called Pascal's pyramid or Pascal's tetrahedron, while the general versions are called Pascal's simplices.

Program Using JAVA:

public class pascal{
public static void main(String args[]){
int row=10;

for(int i=0;i<row;i++){
int no=1;
System.out.format("%"+(row-i)*2+"s","");
for(int j=0;j<=i;j++){
System.out.format("%4d",no);
no=no*(i-j)/(j+1);
}
System.out.println();
}
}
}

Download Program File: Click Here

Output:


Tower Of Hanoi with Recursion Using JAVA

Tower Of Hanoi with Recursion Using JAVA


The Tower of Hanoi (also called the Tower of Brahma or Lucas' Tower, and sometimes pluralized) is a mathematical game or puzzle.It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
  1. Only one disk can be moved at a time.
  2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
  3. No disk may be placed on top of a smaller disk.
With three disks, the puzzle can be solved in seven moves. The minimum number of moves required to solve a Tower of Hanoi puzzle is 2n - 1, where n is the number of disks.

More Details About Tower Of Hanoi: Click Here

Program Using JAVA:

import java.util.*;

public class HanoiTower{

public static void doTower(int disk,char from,char mid,char end){
if(disk==1){
System.out.println("Disk 1 from "+from+" To "+end);
}else{
doTower(disk-1,from,end,mid);
System.out.println("Disk "+disk+" From "+from+" To "+end);
doTower(disk-1,mid,from,end);
}
}
public static void main(String args[]){
Scanner input=new Scanner(System.in);
int disk;
System.out.print("Enter No Of Disk:");
disk=Integer.parseInt(input.nextLine());
if(disk<=0){
System.out.println("Invalid Disk SIze");
}else{
doTower(disk,'A','B','C');
}
}
}

Download Program File: Click Here

output:




Tower of Hanoi without recursion using java

Tower of Hanoi without recursion using java



Program Using JAVA:

import java.util.*;

public class TowerOfHanoi{
public static int makeTower(Stack A,Stack B){
int a,b;
try{
a=Integer.parseInt(A.peek().toString());
}catch(Exception e){
a=0;
}
try{
b=Integer.parseInt(B.peek().toString());
}catch(Exception e){
b=0;
}
if(a==b) return 0;
if(a==0){
A.push(B.pop());
return 2;
}
else if(b==0){
B.push(A.pop());
return 1;
}
if(a>b){
A.push(B.pop());
return 2;
}
else if(a<b){
B.push(A.pop());
return 1;
}
return -1;
}
public static void main(String args[]){
Scanner input = new Scanner(System.in);
try{
Stack source=new Stack();
Stack Auxilary=new Stack();
Stack Destination=new Stack();
int stepno=0;
int status=0;

System.out.print("Enter no Of Disk:");
int disk=Integer.parseInt(input.nextLine());

if(disk<=0){
System.out.println("Invalid Disk Size");
System.exit(1);
}
for(int i=disk;i>0;i--)
source.push(i);
int m=disk%2;
do{
if(m==1){
status=makeTower(source,Destination);
if(status==1){
System.out.println((++stepno)+"Source --> Destination");
}
else if(status==2){
System.out.println((++stepno)+"Destination --> Source");
}
status=makeTower(source,Auxilary);
if(status==1){
System.out.println((++stepno)+"Source -->Auxilary");
}
else if(status==2){
System.out.println((++stepno)+"Auxilary --> Source");
}else
break;
}else{
status=makeTower(source,Auxilary);
if(status==1){
System.out.println((++stepno)+"Source -->Auxilary");
}
else if(status==2){
System.out.println((++stepno)+"Auxilary --> Source");
}
status=makeTower(source,Destination);
if(status==1){
System.out.println((++stepno)+"Source --> Destination");
}
else if(status==2){
System.out.println((++stepno)+"Destination --> Source");
}
}
status=makeTower(Auxilary,Destination);
if(status==1){
System.out.println((++stepno)+"Auxilary --> Destination");
}
else if(status==2){
System.out.println((++stepno)+"Destination --> Auxilary");
}

}while(Destination.size()!=disk);
System.out.println("\nfinish");
}catch(Exception ex){}
}
}

download program file: click here

output: