In the previous article, we have discussed about Java Program to Find the Sum of Series 1!/1 + 2!/2 + 3!/3 + 4!/4+ 5!/5 + N!/N
In this article we are going to see how to print the series print series -1 4 -7 10 -13 16 -19 … N by using java programming language.
Java Program to Print the Series -1 4 -7 10 -13 16 -19 … N
On observing the pattern carefully, we can see Odd numbers are assigned -ve sign and even number assigned to +ve sign.
The numbers in the series are added by 3 with respect to the previous number.
For example:
-1 | 4 | -7 | 10 | -13 | 16 | -19 | 22 | -25 |
1 1+3 4+3 7+3 10+3 13+3 16+3 19+3 22+3
Let’s see different ways to print the series -1 4 -7 10 -13 16 -19 … N
Method-1: Java Program to Print the Series -1 4 -7 10 -13 16 -19 … N By Using for Loop
Approach:
- Declare an int variable say ‘
n
’ which holds the nth value of the series. - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Use a for loop and iterate
from i=1 to i<=n
- Inside loop by taking if-else statement check the number if it is even number then print it as
+i
if the number is odd number then print it as-i
where ‘i
‘ is the number.
Program:
import java.util.*; public class Main { public static void main(String[] args) { //creating object of Scanner class Scanner s = new Scanner(System.in); //Taking input of number of elements in the series System.out.println("Enter number of elements of the series: "); int n=s.nextInt(); //declaring int variable a and initializing its value as 1 int a=1; //for loop to print the series for(int i = 1; i<=n; i++) { //checking if the value is even then print it with + symbol if(i%2==0) { System.out.print(a+" "); } //else the value is odd and printing it with - symbol else { System.out.print(-1*a+" "); } //increasing the value a by 3 a+=3; } } }
Output: Enter number of elements of the series: 5 -1 4 -7 10 -13
Method-2: Java Program to Print the Series -1 4 -7 10 -13 16 -19 … N By Using User Defined Method
Approach:
- Here the logic is same just implement that with in a user defined method.
Program:
import java.util.*; public class Main { public static void main(String[] args) { //creating object of Scanner class Scanner s = new Scanner(System.in); //Taking input of number of elements in the series System.out.println("Enter number of elements of the series: "); int n=s.nextInt(); //declaring int variable a and initializing its value as 1 int a=1; //calling a user defined method printSeries(n,a); } //method to print the series public static void printSeries(int n, int a) { //for loop to print the series for(int i = 1; i<=n; i++) { //checking if the value is even then print it with + symbol if(i%2==0) { System.out.print(a+" "); } //else the value is odd and printing it with - symbol else { System.out.print(-1*a+" "); } //increasing the value a by 3 a+=3; } } }
Output: Enter number of elements of the series: 55 -1 4 -7 10 -13
Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.
Related Java Programs: