New Challenge!!
Create your own Java Códe to get this series as the output.
1 2 1 3 2 5 3 7 5 11 8 13 13....Nth term
Hint:-
• Odd term- Fibbonacci Series
• Even term- Prime Numbers
_
Thank you...!
Answers
Solution:
The given code is written in Java.
import java.util.*;
public class FiboPrime{
public static void main(String args[]){
int n,i,j;
int a=1,b=0,c;
int prime=2,counter;
System.out.print("Enter Limit - ");
n=(new Scanner(System.in)).nextInt();
for(i=1;i<=n;i++){
if(i%2==1){
c=a+b;
a=b;
b=c;
System.out.print(c+" ");
}
else{
System.out.print(prime++ +" ");
while(true){
counter=0;
for(j=1;j<=prime;j++){
if(prime%j==0)
counter++;
}
if(counter==2)
break;
prime++;
} // end of while.
} // else.
} // end of for.
} // end of main.
} // end of class.
Explanation:
The given series is a combination of two series -
- Fibonacci Series - 1 1 2 3 5 8 13... and,
- Series of Prime Numbers - 2 3 5 7 11 13...
The odd terms consists of fibonacci numbers and even terms consists of prime number.
Logic is very easy. If the value of the control variable is odd, then display the numbers of the fibonacci series or else, display the prime numbers.
Variable Description:
See the attachment for output.
•••♪
