Computer Science, asked by BrainlyProgrammer, 5 months ago

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

Answered by anindyaadhikari13
10

Solution:

The given co‎de 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:

\dag\:\:\boxed{\begin{array}{c|c|c}\tt\underline{{N}ame}:&amp;\tt\underline{Type}:&amp;\tt\underline{Function}:\\ \tt n&amp;\tt int&amp;\tt Stores\:\:limit.\\ \tt i,j&amp;\tt int&amp;\tt {L}oop\:\:Variable.\\ \tt a,b,c&amp;\tt int&amp;\tt Calculate\:\:Fibo\:\:Number.\\ \tt prime&amp;\tt int&amp;\tt Stores\:\:Prime\:\:Number.\\ \tt counter&amp;\tt int&amp;\tt Counts\ factors\ of\ prime.\end{array}}

See the attachment for output.

•••♪

Attachments:
Similar questions