What is the next number? 0 3 8 15 24 35: In the previous article we have discussed about Java Program to Print Series 2 5 10 17 26 37 50…N
In this article we are going to see how to print the series 0 3 8 15 24 35 48…N by using Java programming language. 0 3 8 15 series in java.
Java Program to Print Series 0 3 8 15 24 35 48…N
0 3 8 15 24 35: On observing the pattern carefully, you can see this series is in in the form of i*i-1
where i
starts from 1 and goes on up to N.
When i=1: i*i-1 = 1*1-1 = 0
When i=2: i*i-1 = 2*2-1 = 3
When i=3: i*i-1 = 3*3-1 = 8 and so on.
So the series is like 0 3 8 15 … N
Example:
When value of N = 5 Then series: 0 3 8 15 24
Let’s see different ways to print the series 0 3 8 15 24 35 48…N
Method-1: Java Program to Print Series 0 3 8 15 24 35 48…N By Using For Loop
Approach:
- Declare an integer variable say ‘
n
’ which holds the Nth term of the series - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Let declare a integer variable ‘
result
‘ - Use a for loop from
i=1 to i<=n
incremented by 1 - Inside for loop find the value of
result = i*i-1
- 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 result; //for loop to print the series for (int i = 1; i <= n; i++) { result=i*i-1; System.out.print(" "+result); } } }
Output: Enter the Nth term “N” 5 0 3 8 15 24
Method-2: Java Program to Print Series 0 3 8 15 24 35 48…N By Using While Loop
Approach:
- Declare an integer variable say ‘
n
’ which holds the Nth term of the series - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Let declare a integer variable ‘
result
‘ - Declare and initialize an integer variable
i=1
- Continue a while loop till
i<=n
, wherei
incremented by 1. - Inside while loop find the value of
result = i*i-1
- Print the result in the series.
Program:
import java.util.*; public class Main { public static void main(String [] args) { 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 result; int i=1; while(i<=n) { result=i*i-1; System.out.print(" "+result); i++; } } }
Output: Enter the Nth term “N” 7 0 3 8 15 24 35 48
Method-3: Java Program to Print Series 0 3 8 15 24 35 48…N By Using User Defined Method
Approach:
- Declare an integer variable say ‘
n
’ which holds the Nth term of the series - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Call a user defined method
printSeries()
by passing n as parameter. - Inside method declare a integer variable ‘
result
‘ - Use a for loop from
i=1 to i<=n
incremented by 1 - Inside for loop find the value of
result = i*i-1
- 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 vale of Nth term “N”"); int n = s.nextInt(); // calling printSeries method to print the series printSeries(n); } //user defined method printSeries() to print the series public static void printSeries(int n) { int result; //for loop to print the series for (int i = 1; i <= n; i++) { result=i*i-1; System.out.print(" "+result); } } }
Output: Enter the vale of Nth term “N” 5 0 3 8 15 24
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Read Also: Java Program to Print Series 2 5 10 17 26 37 50…N
Test yourself:
- Write A Program To Print The Series 0 3 8 15?
- Java Program To Print Series 0 3 8 15?
- Write A Program To Print The Series 0,3,8,15 In Python?
- What Is The Next Number? 0 3 8 15 24 35?
- What Is The Next Number 0 3 8 15 24 35?
- 0,3,8,15,24,35
- 3+8+15+24 Upto N Terms
- 0 3 8 15 24 35 Next Number
- 0 3 8 15 Nth Term
Related Java Programs: