Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Generate prime numbers within fiboncacci series?

    Hey,

    i am new to java programming,and i am working a simple program that generate prime numbers whithin the generated fibonacci series,i tryed a lot to track the error out,but its just not displaying me the prime number(though fibonnaci numbers are generated),hope this forum can help to track my error which is bugging me from the past 5-6 hours.(platform used is netbeans 6.9)


    /* program to generate prime numbers within the fibonnaci numbers */




    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package javaapplication5;
    import java.lang.*;
    import java.util.*;

    public class Main
    {
    public static void main(String[] args)
    {
    int fib1,fib2,fib3,n=0,flag, int i;
    int a[]=new int[20];
    Scanner sc=new Scanner(System.in);
    n=sc.nextInt();
    fib1=0;
    fib2=1;
    System.out.println(fib1+ " " +fib2);
    fib3=fib1+fib2;
    while(fib3<n)

    {
    System.out.println(fib3);
    fib3=fib1+fib2;
    fib1=fib2;
    fib2=fib3;


    flag=1;

    for(i=2;i<=fib3/2;i++)
    {
    if(fib3%i==0)
    {
    flag=0;

    }
    if(flag==1)
    {
    System.out.println("the prime numbers are");
    System.out.println(i);
    }

    }


    }

    }
    }

  • #2
    New Coder
    Join Date
    Aug 2009
    Posts
    94
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Code:
    import java.util.*;
    
    public class Main{
    	public static void main(String[] args){
    		int fib1,fib2,fib3 = 0,n=0;
    		Scanner sc=new Scanner(System.in);
    		n=sc.nextInt();
    
    		fib1=-1;
    		fib2=1;
    		
    		while(fib3<n){
    			System.out.print(fib3);
    			
    			if(isPrime(fib3)){
    				System.out.print(" - Is Prime");
    			}
    			System.out.println("");
    
    			fib1=fib2;
    			fib2=fib3;
    			fib3=fib1+fib2;
    
    			
    			
    		}
    	}
    
    	public static boolean isPrime(int number){
    		for(int i = 2; i <= number / 2; ++i){
    			if (number % i == 0)
    				return false;
            }
           return true;
    
        }
    
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •