How to print the Fibonacci series and prime number list

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.