In the previous article we have discussed about Java Program to Print the Series 1 -2 6 -15 31 …N
In this article we are going to see how to print the series 10 5 60 15 110 …..N by using Java programming language.
Java Program to Print the Series 10 5 60 15 110 …..N
On observing the pattern carefully, we can see 1st and 2nd number are 10, 5 respectively
Then the next number follows a logic
10 5 10+50 = 60 5+10 = 15 60+50=110 15+10=25 and so on.
10 | 5 | 60 | 15 | 110 | 25 | … | N |
Let’s see different ways to print the series 10 5 60 15 110 …..N
Method-1: Java Program to Print the Series 10 5 60 15 110 …..N By Using For Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘n’ which holds the Nth term of the series.
- Prompt the user to enter a number as value of n.
- Let declare integer variable say ‘x’,’y’ and initialize it to 10,5 respectively.
- Use a for loop from i=1 to i<=n where the loop is incremented by 1
- Inside the for loop we will first check the position of the nth term If the position is odd then add 50 with ‘x’ else add 10 with ‘y’
- Print the result in the series.
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 the Nth term “N” "); int n = s.nextInt(); int x = 10, y=5; //for loop to print the series for(int i=1;i<=n;i++) { if(i%2!=0) { System.out.print(x+" "); x+=50; } else { System.out.print(y+" "); y+=10; } } } }
Output: Enter the Nth term “N” 5 10 5 60 15 110
Method-2: Java Program to Print the Series 10 5 60 15 110 …..N By Using While Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘n’ which holds the Nth term of the series
- Prompt the user to enter a number as value of n.
- Let declare integer variable say ‘x’, ‘y’ and initialize it to 10, 5 respectively
- Declare and initialize an integer variable i=1
- Continue a while loop till i<=n, where i is incremented by 1.
- Inside the while loop we will first check the position of the nth term If the position is odd then add 50 with ‘x’ else add 10 with ‘y’
- Print the result in the series.
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 the Nth term “N” "); int n = s.nextInt(); int i=1, x=10, y=5; while(i<=n) { if(i%2!=0) { System.out.print(x+" "); x+=50; } else { System.out.print(y+" "); y+=10; } i++; } } }
Output: Enter the Nth term “N” 7 10 5 60 15 110 25 160
Method-3: Java Program to Print the Series 10 5 60 15 110 …..N By Using User Defined Method
Approach:
- The same logic as method 1 but this time we are moving the for inside a user-defined method
- Create a Scanner class object.
- Prompt the user to enter the Nth term “N”
- Call a user defined method printSeries() by passing n as parameter.
- Inside the method use a for loop to find the series.
- Let declare integer variables say ‘x’, ‘y’ and initialize it to 10,5 respectively
- Use a for loop from i =1 to i<=n incremented by 1
- Inside the for loop we will first check the position of the nth term If the position is odd then add 50 with ‘x’ else add 10 with ‘y’
- Print the result in the series.
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 the value of Nth term 'N' "); int n = s.nextInt(); // calling printSeries method to print the series printSeries(n); } //printSeries metthod to print the series public static void printSeries(int n) { int x = 10, y = 5; //for loop to print the series for (int i = 1; i <=n; i++) { if(i%2!=0) { System.out.print(x+" "); x+=50; } else { System.out.print(y+" "); y+=10; } } } }
Output: Enter the value of Nth term 'N' 9 10 5 60 15 110 25 160 35 210
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Related Java Programs: