Java program to print Fibonacci series.
package programs;
public class Fibonacci {
public static void main(String[] args) {
int Count = 10;
int[] feb = new int[Count];
feb[0] = 0;
feb[1] = 1;
for(int i=2; i < Count; i++){
feb[i] = feb[i-1] + feb[i-2];
}
for(int i=0; i< Count; i++){
System.out.print(feb[i] + " ");
}
}
}
package programs;
public class Fibonacci {
public static void main(String[] args) {
int Count = 10;
int[] feb = new int[Count];
feb[0] = 0;
feb[1] = 1;
for(int i=2; i < Count; i++){
feb[i] = feb[i-1] + feb[i-2];
}
for(int i=0; i< Count; i++){
System.out.print(feb[i] + " ");
}
}
}
better to develop code with out using arrays.....
ReplyDeleteint prev=0,current=1,next,size=10;
for(int j=1;j<=10;j++){
if(j==1 || j==2)
syso(j+" ");
else{
next=prev+current;
syso()next
prev=current;
current=next
}
}
BY:Suman Verma
Correction:
Deleteint prev=0,current=1,next,size=10;
for(int j=0;j<=size;j++){
if(j==0||j==1 ){
System.out.println(j+" ");
}else{
next=prev+current;
System.out.println(next);
prev=current;
current=next;
}
}
int prev=1,next=1,sum,n=2;
Deletefor(int i = 1; i<= n;i++){
//System.out.println(prev+"step1\n");
sum =prev+next;
prev=next;
next=sum;
System.out.println("fibenanci of "+prev+"="+next);
}
Can any send the code for printing first 10 prime number...
ReplyDeleteint prev=1,next=1,sum,n=2;
Deletefor(int i = 1; i<= n;i++){
//System.out.println(prev+"step1\n");
sum =prev+next;
prev=next;
next=sum;
System.out.println("fibenanci of "+prev+"="+next);
}