#Help with Lucas series

1 messages · Page 1 of 1 (latest)

gray tusk
#

So I have this code which prints the lucas series.

Lucas series is a series which starts from 2 and 1 and the following numbers are the sum of the previous 2 number like

        2
        1

2 +1 = 3
3 +1 = 4
4 +3 = 7
and so on.

import java.util.*;
public class Main
{
    public static void main(String[] args) {
        int a=2,b=1,c,i;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the length of lucas series:");
        int len=sc.nextInt();
        for(i=1;i<=len;i++){
            c=a+b;
            System.out.print(c+" ");
            a=b;
            b=c;
        }
    }
}

So the problem is if I for example say input 10 for the scanner input. Then it starts from 3, 4, 7, etc. So how do I get the first 2 and 1? I need it to print all 10 starting from 2 and 1.
So that is 2,1,3,4,7....

small pathBOT
#

This post has been reserved for your question.

Hey @gray tusk! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

gray tusk
#

is it possible to print the first 2 numbers with the loop and not just doing prntln(a) and println(b) ?

sterile kestrel
#

Just do

if(i == 1) {
 a = 2;
 c = a;
} else if(i == 2) {
  b = a;
  a = 1;
  c = a;
}
gray tusk
#

thank you

small pathBOT
# gray tusk thank you

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.