  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Series program in java

Last Updated on August 18, 2023 by Mayank Dham Number series are sequences of numbers that follow a specific pattern or rule. These series can be encountered in various mathematical and computational contexts, and understanding their patterns is crucial for solving problems and creating algorithms. In the realm of programming, Java offers a powerful platform to work with number series. This article explores the concept of number series and demonstrates how to write Java programs to generate and manipulate different types of number series. Whether you’re a beginner or an experienced programmer, diving into number series program in Java can enhance your problem-solving skills and computational thinking. Let’s first discuss what is fibonacci series program in Java.

## What is the Fibonacci series?

In the Fibonacci series, the next term is an addition of the previous two terms. For example, if the previous two terms are 3 and 5 then the next term will be 8 (3+5). The first two terms of the Fibonacci series are 0 and 1 respectively. Now, if we look at the third term it will be the summation of the first term(0) and the second term(1) is 1 (0+1). If we look for the fourth term it will be the addition of the second term(1) and the third term (1) is 2(1+1). Using the same approach we can find the remaining terms easily. Let’s see various approaches to writing the Fibonacci series program in java.

### Approach 1: Iterative method for the Fibonacci series program in java

In this iterative method, we will use the first two terms of the Fibonacci series 0 and 1, and then we will find further terms using these two terms. Let’s see how to write the Fibonacci series program in java using this method.

```class PrepBytes {
public static void main(String[] args) {
int first_term=0;
int second_term=1;
int second_last_term=0;
int last_term=1;
System.out.println("The first 10 terms of the Fibonacci series are:");
for(int i=0;i<10;i++){
if(i==0){
System.out.print(first_term+" ");
}
else if(i==1){
System.out.print(second_term+" ");
}
else{
int current_term=second_last_term+last_term;
System.out.print(current_term+" ");
second_last_term=last_term;
last_term=current_term;
}
}
}
}```

Output

``````The first 10 terms of the Fibonacci series are:
0 1 1 2 3 5 8 13 21 34 ``````

In the above program, first, we have defined the initial two terms 0 and 1. After that, we have also created two more variables last_term and second_last_term to keep track of the last and second last term so far. Now, we will run a for loop 10 times to print the first 10 terms of the Fibonacci series. In the loop, if i is less than 2 then we will directly print the first two terms, and if i is greater than or equal to 2 then first we will compute the current term which equals the sum of the last and second last term. Now, we will change the second last term as the last term and the last term as the current term to calculate further terms. In the output, we can see that the first ten terms of the Fibonacci series are printed.

Time Complexity: O(n) will be the time complexity for finding a fibonacci number series program in Java.
Auxiliary Space Complexity: O(1) will be the space complexity for finding a fibonacci number series program in Java.

### Approach 2: Recursive method for the Fibonacci series program in java

In this approach, we will use base case as the first two terms and our recursive formula for the nth term will be (n-1)th term +(n-2)th term. Let’s see how to write the Fibonacci series program in java using the recursive method.

```class PrepBytes {
static int fibonacci(int n)
{
if (n < 2)
return n;

return fibonacci(n - 1)
+ fibonacci(n - 2);
}
public static void main(String args[])
{
System.out.println("The first 10 terms of the Fibbonaci series are:");
for (int i = 0; i < 10; i++) {

System.out.print(fibonacci(i) + " ");
}
}
}
```

Output

``````The first 10 terms of the Fibonacci series are:
0 1 1 2 3 5 8 13 21 34 ``````

In the above program, we have written a recursive method Fibonacci. In that method, we have given base case (recursion termination condition) if the given number is less than 2 then we will return that number. Otherwise, we will find the n-1 term [fibonacci(n-1)] and the n-2 term [fibonacci(n-2)] and we will return the sum of both terms. In the output, we can see that the first ten terms of the Fibonacci series are printed.

Time Complexity: O(2^n)
Auxiliary Space Complexity: O(1)

### Approach 3: Recursion + Memoization method for the Fibonacci series program in java

In this method, we will use the above recursion method the only difference is that we will use the memoization method with this approach. Memoization means we will store the answer to all the calculated terms so that we can use them again and we do not need to find that term again and again.

```class PrepBytes {
static int fibonacci(int n,int[] memo)
{
if (memo[n]!=-1){
return memo[n];
}
if (n < 2)
return n;

int current_term= fibonacci(n - 1,memo) + fibonacci(n - 2,memo);
memo[n]=current_term;
return current_term;
}
public static void main(String args[])
{
int[] memo= new int;
for(int i=0;i<10;i++){
if(i<2){
memo[i]=i;
}
else{
memo[i]=-1;
}
}

System.out.println("The first 10 terms of the Fibbonaci series are:");
for (int i = 0; i < 10; i++) {

System.out.print(fibonacci(i,memo) + " ");
}
}
}
```

Output

``````The first 10 terms of the Fibonacci series are:
0 1 1 2 3 5 8 13 21 34 ``````

In the above program, we have created an array of size 10 to store calculated terms and we will initialize the first two terms as 0 and 1 in that array. After that, we will use the same recursive method as above to find the nth term of the Fibonacci series. Now, when we call a method fibonacci to find the nth term we have two options. One is, if the nth term is already calculated then we will directly return the answer. Otherwise, we will calculate the answer and store the answer in an array while returning from the recursive call. In the output, we can see that the first ten terms of the Fibonacci series are printed.

Time Complexity: O(n)
Auxiliary Space Complexity: O(n)

## What is the Triangular number series?

When numbers are in triangular patterns it is called the Triangular number series. We can say a number is a triangular number if we can represent it in form of a triangular grid. Let’s understand what is the Triangle number series. In the above image, we can see that, we can put only one dot in the triangle so our first term will be 1. In the second triangle, we can place three dots so our second term will be 3. In the third and fourth triangles, we can place 6 and 10 dots respectively. So our third term is 6 and the fourth term is 10.

Observation: the nth term is the sum of the first n natural numbers.

``````T(1) = 1 = 1
T(2) = 1+2 = 3
T(3) = 1+2+3 = 6
T(4) = 1+2+3+4 =10
.
.
.
T(n) = the sum of the first n natural number``````

*The equation to find the sum of the first n natural numbers: (n(n+1))/2**

Let’s see various approaches to writing the Triangular number series program in java.

### Approach 1: Iterative method for the Triangular number series program in java

In this iterative approach, we will run a loop to find all the numbers of the Triangular number series using the above equation. Let’s see how to write iterative code for the Triangular series program in java.

```class PrepBytes {
public static void main(String[] args) {
int N=10;
System.out.println("The first 10 terms of the Triangular number series:");
for(int i=1;i<=N;i++){
int current_term=(i*(i+1))/2;
System.out.print(current_term+" ");
}

}
}```

Output

``````The first 10 terms of the Triangular number series:
1 3 6 10 15 21 28 36 45 55 ``````

In the above program, we run a loop from 1 to 10. During each iteration, we will find the current ith term using the above equation. In the output, we can see that the first ten terms of the Triangular number series are printed.

### Approach 2: Recursive method for the Triangular number series program in java

In this recursive method, we will use recursion to find the current term of the Triangular number series rather than the equation. Let’s see how to write iterative code for the Triangular series program in java.

```class PrepBytes {
static int triangular_number(int n){
//base case
if(n==1){
return 1;
}
else{
// recursive formula
return n+triangular_number(n-1);
}
}

public static void main(String[] args) {
int N=10;
System.out.println("The first 10 terms of the Triangular number series:");
for(int i=1;i<=N;i++){
int current_term=triangular_number(i);
System.out.print(current_term+" ");
}

}
}```

Output

``````The first 10 terms of the Triangular number series:
1 3 6 10 15 21 28 36 45 55 ``````

In the above program, we have created a recursive method triangular number to find the ith term of the Triangular number series. In this recursive method, our base case will be for the first term as we know the answer to the first term is 1. Our recursive relation will be the summation of the current term and remaining terms (n-1). In the output, we can see that the first ten terms of the Triangular number series are printed.

## What is the Pell series?

Pell series starts with the first term as 0 and the second term as 1. The next term will be an addition to the second last term and two times to the last term. Now, if we compute the third term it will be the sum of the second last term (0), and twice of the last term (21) is 2 (0+2). The fourth term will be the sum of the second last term (1) and twice of the last term (22) is 5 (1+4). We can find more terms using the same method.

*Relation of Pell series number: T(n) = T(n-2) + 2T(n-1)**

``````T(1) = 0
T(2) = 1
T(3) = 0+2*1 = 0+2 = 2
T(4) = 1+2*2 = 1+4 = 5
T(5) = 2+2*5 = 2+10 = 12
.
.
.
T(n) = T(n-2) + 2*T(n-1)``````

Let’s see various approaches to writing the Pell series program in java.

### Approach 1: Iterative method for the Pell series program in java

In this iterative approach, we will run a for loop to find every term of the Pell series. Let’s see how to write iterative code for the Pell series program in java.

```class PrepBytes {
public static void main(String[] args) {
int first_term=0;
int second_term=1;
int second_last_term=0;
int last_term=1;
System.out.println("The first 10 terms of the Pell series are:");
for(int i=0;i<10;i++){
if(i==0){
System.out.print(first_term+" ");
}
else if(i==1){
System.out.print(second_term+" ");
}
else{
int current_term=second_last_term+2*last_term;
System.out.print(current_term+" ");
second_last_term=last_term;
last_term=current_term;
}
}
}
}```

Output

``````The first 10 terms of the Pell series are:
0 1 2 5 12 29 70 169 408 985 ``````

In the above program, first, we have defined the initial two terms 0 and 1. After that, we have also created two more variables last_term and second_last_term to keep track of the last and second last term so far. Now, we will run a for loop 10 times to print the first 10 terms of the Fibonacci series. In the loop, if i is less than 2 then we will directly print the first two terms, and if i is greater than or equal to 2 then first we will compute the current term which equals the sum of twice of the last and second last term. Now, we will change the second last term as the last term and the last term as the current term to calculate further terms. In the output, we can see that the first ten terms of the Fibonacci series are printed.

Time Complexity: O(n)
Auxiliary Space Complexity: O(1)

### Approach 2: Recursive method for the Pell series program in java

In this approach, we will use base case as the first two terms and our recursive formula for the nth term will be (n-2)th term + 2*(n-1)th term. Let’s see how to write the Pell series program in java using the recursive method.

```class PrepBytes {
static int pell(int n)
{
if (n < 2)
return n;

return pell(n - 2) + 2* pell(n-1) ;
}
public static void main(String args[])
{
System.out.println("The first 10 terms of the Pell series are:");
for (int i = 0; i < 10; i++) {

System.out.print(pell(i) + " ");
}
}
}
```

Output

``````The first 10 terms of the Pell series are:
0 1 2 5 12 29 70 169 408 985 ``````

In the above program, we have written a recursive method pell. In that method, we have given base case (recursion termination condition) if the given number is less than 2 then we will return that number. Otherwise, we will find the 2n-1 term [2pell(n-1)] and the n-2 term [pell(n-2)] and we will return the sum of both terms. In the output, we can see that the first ten terms of the Pell series are printed.

Time Complexity: O(2^n) will be the time complexity for the Pell number series program in Java.
Auxiliary Space Complexity: O(1) will be the space complexity for the pell number series program in Java.

Conclusion
Number series programming in Java offers a fascinating journey into the world of mathematical patterns and logical sequences. By understanding and implementing different types of number series, you can enhance your problem-solving skills and gain insights into algorithmic thinking. Whether you’re generating Fibonacci numbers or working with arithmetic progressions, Java provides the tools you need to manipulate and analyze number series effectively. Exploring number series in Java not only helps you grasp fundamental programming concepts but also equips you with the skills to tackle diverse computational challenges.

## FAQ related to Number Series Program in Java

Here are some FAQs related to number series program in Java.

1. What are number series in programming?
Number series in programming are sequences of numbers that follow a certain pattern or rule. These patterns can be based on mathematical formulas, arithmetic progressions, geometric progressions, or other logical rules. Understanding and working with number series is essential for various applications, including data analysis, cryptography, and algorithm design.

2. Why is Java a suitable language for number series programming?
Java is a versatile and widely-used programming language known for its platform independence and strong support for object-oriented programming. It provides the necessary tools to work with numbers, mathematical operations, and loops, which are essential for generating and manipulating number series. Java’s syntax and extensive library make it an excellent choice for implementing algorithms involving number series.

3. What types of number series are commonly encountered?
Several types of number series are commonly encountered, such as arithmetic progressions (AP), geometric progressions (GP), Fibonacci series, prime number series, and more. Each type of series has its own unique pattern or rule governing the sequence of numbers. Understanding these patterns allows programmers to generate and analyze series efficiently.

4. How can I generate an arithmetic progression (AP) in Java?
To generate an arithmetic progression in Java, you can use a loop to increment the starting value by a fixed difference in each iteration. The formula for the nth term of an AP is: a_n = a_1 + (n – 1) * d, where a_n is the nth term, a_1 is the first term, n is the term number, and d is the common difference.

5. How do I create a Java program to generate the Fibonacci series?
The Fibonacci series is a sequence where each number is the sum of the two preceding ones, starting from 0 and 1. You can use a loop or recursion to generate the Fibonacci series in Java. A common approach is to use a loop to iteratively calculate and print each Fibonacci number based on the two preceding numbers.