+ 1

How to print 1 1 2 4 7 13 24 44

17th Feb 2017, 4:36 AM
Ali Zeawo
Ali Zeawo - avatar
3 Answers
+ 1
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input= new Scanner(System.in); System.out.println("Enter the number of terms in the series"); byte j= input.nextByte(); long[] s= new long[j]; s[0] = s[1]=1; s[2] = 2; System.out.print ("The first "+j+ " terms of the series are \n" +s[0]+", "+s[1]+", "+s[2]); for(int i=3;i<j;i++) { s[i]=s[i-2]+s[i-1]+s[i-3]; System .out.print(", "+s[i]); } } } For the series to stop at 44, give the input as 8 as the number of terms in your series is 8
17th Feb 2017, 3:11 PM
Kommoju Sunil Kumar
Kommoju Sunil Kumar - avatar
+ 1
Done with recursion. In fact, this is similar to Fibonacci numbers. public static int fab3(int n) { if (n < 0) return 0; if (n < 2) return 1; return fab3(n-1)+fab3(n-2)+fab3(n-3); } public static void main(String[] args) { for (int i=0; i<8; i++) System.out.print(fab3(i)+" "); }
18th Feb 2017, 4:03 AM
Jian-hua Yeh
Jian-hua Yeh - avatar
0
int a=1; int b=1; int c=2; for(int i=0; i<3; i++){ System.out.print(a + " "); System.out.print(b + " "); if(c<44){ System.out.print(c + " "); } a=c+b+a; b=a+c+b; c=b+a+c; }
17th Feb 2017, 5:49 AM
LordHill
LordHill - avatar