These are actually two very basic and simple questions. However, somehow often reminds me of these two problems, sometimes cake does not think of the solution and will be sweating ...................
Let's get down to the truth and paste the code for these two questions.
(1) print the Fibonacci series
// Java program for Fibonacci number using Loop. public static int fibonacciLoop(int number){ if(number == 1 || number == 2){ return 1; } int fibo1=1, fibo2=1, fibonacci=1; for(int i= 3; i<= number; i++){ fibonacci = fibo1 + fibo2; //Fibonacci number is sum of previous two Fibonacci number fibo1 = fibo2; fibo2 = fibonacci; } return fibonacci; //Fibonacci number }
(2) print prime numbers
public static void prime( int number) { for (int i=2; i<number; i++) for (int j=2; j*j<=i; j++) { if (i % j == 0) break; else if (j+1 > sqrt(i)) { System.out.println(i); } } return 0;}
How to print the Fibonacci series and prime number list