Tuesday, December 23, 2014

Fibonacci Series Java and Declare multiple variables in for loop and Generate Pyramid



Declare multiple variables in for loop

public class DeclaringMultipleVariablesInForLoopExample
{
public static void main(String[] args)
{
for(int i=0, j=1, k=2 ; i<5 i="" span="">
System.out.println("I : " + i + ",j : "+ j + ", k : " + k);
for(int i=0, float j; i < 5; i++);
  }
}

Fibonacci Series Java

public class JavaFibonacciSeriesExample
{
public static void main(String[] args)
{             
int limit = 20;     
long[] series = new long[limit];
series[0] = 0;
series[1] = 1;
for(int i=2; i < limit; i++)
{
series[i] = series[i-1] + series[i-2];
}
System.out.println("Fibonacci Series upto " + limit);
for(int i=0; i< limit; i++){
System.out.print(series[i] + " ");
     }
   }
}

Generate Pyramid For a Given Number

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class GeneratePyramidExample
 {
public static void main (String[] args) throws Exception
{              
BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in));             
System.out.println("Enter Number:");
int as= Integer.parseInt (keyboard.readLine());
System.out.println("Enter X:");
                     int x=   Integer.parseInt (keyboard.readLine());
int y = 0;           
for(int i=0; i<= as ;i++)
{         
for(int j=1; j <= i ; j++)
{
System.out.print(y + "\t");
y = y + x;
}                       
System.out.println("");
    }
  }
}

No comments:

Post a Comment