Professional Documents
Culture Documents
Lab session - 5
Programs
1) In mathematics Fibonacci numbers are
the following sequence of numbers:
0 1 1 2 3 5 8 13….
Write a Java program to print the Fibonacci
series. Please accept the upper limit from
the user i.e from the console.
2) Read 3 numbers from console and print
the digit as series of words.
Example run: 1 one 2 two 3 three
Skeleton for Fibonacci
Public class Fibonacci
{
public static void main(String args[])
{
int ….//declare the variables
for( init ; condn ; incr/decr)
{
// Next term is the sum of previous two
// print the number
// First previous becomes 2nd previous
// And current number becomes previous
}
}
}
Java program
Public class Fibonacci
{
public static void main(String args[])
{
int n0 = 1, n1 = 1, n2, limit;
S.O.P(n0 + “” + n1);
for(int I = 0; I < limit ; i++)
{
n2 = n1 + n0;
S.O.P(n2);
n0 = n1;
n1 = n2;
}
}
}
One approach to problem-2
import java.util.Scanner;
while(count < 3)
{
if(count == 0)
var = s.nextInt();
if(count == 1)
{var = s.nextInt();}
if(count == 2)
{
var = s.nextInt();
}
switch(var)
{
case 0: System.out.println("zero");break;
case 1: System.out.println("one");break;
case 2: System.out.println("two");break;
default : System.out.println("nothing");
}
count++;
}
}