What is series ?
Series program in java, so what is series ? Mathematically, a series is an ordered collection of numbers which have been combined. Each number in the sequence is known as a term within it; when all terms have been added up together, we refer to this total as being all of their terms combined.
Consider this collection:
1 + 2 + 3 + 4 + 5
The numbers 1, 2, 3, and 5 add up to form a total of 5.
The term “series” is used in a variety of mathematical calculations. They can be either finite or infinite, with finite series having a predetermined number of terms while infinite series having many. There are various kinds of series such as geometric, arithmetic and power ones; each having their own formulae for calculating the total value across all these different equations.
Series program in java code
package com.slb.java;
import java.util.Scanner;
public class SeriesTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
int sum = 0;
for (int i = 0; i < n; i++) {
int term = (int) Math.pow(2, i);
sum += term;
System.out.print(term);
if (i != n - 1) {
System.out.print(" + ");
}
}
System.out.println(" = " + sum);
System.out.println(" Total sum " + sum);
}
}
Output:
Enter the value of n: 15
1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 + 512 + 1024 + 2048 + 4096 + 8192 + 16384 = 32767
Total sum 32767
Code Explanation:
This program reads in one integer value input number n from the user and calculates a series of 20+21 + 22…+2(n-1). It uses an for loop to loop through values from 0 up to 1, using Math.pow() method which raises two numbers by two powers of i. It adds each term to the running sum (sum) in same order and prints out entire series using If statement which determines printing option; with + symbol after each word for output accuracy.
Fibonacci series program in java
The Fibonacci series of numbers is a well-known mathematical series named after its creator, Italian mathematics professor Leonardo Fibonacci. All numbers in this sequence are the product of two numbers before them, beginning at zero and ending with 1.
The prime numbers in the Fibonacci series are:
From 0, 1 to 5, 8, 13, 21 to 55, in numerical order from zero to one: 89; 144; 233; 377…
It is evident that each number in this series (beginning at number 3) represents the sum of two preceding numbers. For instance, 2 is created from 1 plus 1, while 3 represents the product of two numbers together and so on.
The Fibonacci series has numerous fascinating properties and applications in mathematics, science and art. It can be observed throughout nature as well, such as plant growth patterns or spiral patterns found in galaxies and shells.
Programmers often refer to the Fibonacci series when discussing looping and recursion techniques, which can be implemented using various programming languages like Java. Lets see how to implement it in java.
Different ways to implement Fibonacci series in java
Using Loops
This method is the most popular and straightforward. It involves using a for or while loop to repeatedly repeat values from zero up to the number n (where n is the number of terms that make up this series) and calculate the Fibonacci number for every step. Here’s an example:
public static void printFibonacciUsingLoop(int n) {
int a = 0, b = 1;
for (int i = 0; i < n; i++) {
System.out.print(a + " ");
int c = a + b;
a = b;
b = c;
}
}
Using recursion
Recursion is a method for implementing Fibonacci series programs. It involves formulating an algorithm that runs with smaller inputs until it reaches an intermediate case, where the answer is known.
public static int fibonacciUsingRecursion(int n) {
if (n == 0 || n == 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
Using memoization
Memoization is a method for speeding up recursive algorithms by caching the results of costly function calls and then returning them when similar inputs are used repeatedly. This can significantly enhance Fibonacci calculations, especially when dealing with large numbers of values.
public static int fibonacciUsingMemoization(int n, int[] memo) {
if (n == 0 || n == 1) {
return n;
}
if (memo[n] != 0) {
return memo[n];
}
memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo);
return memo[n];
}
Series program in java for Sum
public class SeriesSumSLB {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number for sum series: ");
int n = sc.nextInt();
int sum = 0;
for(int i = 1; i <= n; i++) {
sum += i;
}
System.out.println("The sum of the series 1 + 2 + 3 + … + " + n + " is " + sum);
}
}
First, we import the Scanner class in order to receive user input via console.
After that, we create a main method in which we create a Scanner object and prompt the user to enter their number.
We process user input by reading it with the nextInt method of the Scanner class and saving it into variable n.
Here, we define and initialize a variable sum to 0.
Utilizing a for loop, we iterate from 1 to n and add each number to the sum variable.
Finally, we print out the sum of our series using System.out.println while concatenating all relevant strings and variables.
When running, this program will prompt users to enter a number. When this number has been entered, the program will calculate and display its sum over all series from 1 up to that particular number.
Sum for Odd numbers
import java.util.Scanner;
public class OddSeriesSumSLB {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms in the series: ");
int n = scanner.nextInt();
int sum = 0;
for (int i = 1, j = 1; i <= n; i++, j += 2) {
sum += j;
}
System.out.println("Sum of the series is " + sum);
}
}
Sum for Even numbers
import java.util.Scanner;
public class EvenSeriesSum {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms in the series: ");
int n = scanner.nextInt();
int sum = 0;
for (int i = 1, j = 2; i <= n; i++, j += 2) {
sum += j;
}
System.out.println("Sum of the series is " + sum);
}
}
Conclusion
Overall, each approach has its own advantages and drawbacks; ultimately, the optimal choice depends on factors such as performance requirements, input size, and programming style.
We Also saw few other series program in java for sum like sum of odd numbers, sum of even numbers, etc
Checkout other java Tutorials here