In the previous article, we have discussed about Java Program to Print Cube Number Series 1 8 27 64 … N
In this article we are going to see how to print Triangular number series 1 3 6 10 15 …N by using Java programming language.
Java Program to print Triangular Number Series 1 3 6 10 15 …N
On observing the pattern carefully, we can see the numbers in the series are triangular number.
Example:
n*(n+1))/2)
i.e. (1*(1+1))/2 = 2/2 = 1
(2*(2+1))/2 =6/2 = 3
(3*(3+1))/2 =12/2 = 6
Let’s see different ways to print Triangular number series 1 3 6 10 15 …N
Method-1: Java Program to Print Triangular Number Series 1 3 6 10 15 …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 from i=1 to i<=n
- Inside for loop, Print the result as i*(i+1))/2)
Program:
import java.util.*; public class Main { public static void main(String[] args) { //Scanner class object created Scanner s = new Scanner(System.in); //taking input of 'n' value from user System.out.println("Enter the nth value of the series:"); int n=s.nextInt(); //printing the series by using for loop for(int i = 1; i<=n; i++) { System.out.print(( i*(i+1)/2)+ " "); } } }
Output: Enter the nth value of the series: 10 1 3 6 10 15 21 28 36 45 55
Method-2: Java Program to Print Triangular Number Series 1 3 6 10 15 …N By Using While 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 while loop from i=1 to i<=n
- Inside while loop, Print the result as i*(i+1))/2)
Program:
import java.util.*; public class Main { public static void main(String[] args) { //Scanner class object created Scanner s = new Scanner(System.in); //taking input of 'n' value from user System.out.println("Enter the the nth value of the series:"); int n=s.nextInt(); //printing the series by using for loop int i = 1; while(i<=n) { System.out.print((( i*(i+1))/2)+ " "); i++; } } }
Output: Enter the the nth value of the series: 10 1 3 6 10 15 21 28 36 45 55
Method-3: Java Program to Print Triangular Number Series 1 3 6 10 15 …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 Scanner class object.
- Prompt the user to enter a number as value of
n
. - Use a for loop from i=1 to i<=n
- Print the result as i*(i+1))/2)
Program:
import java.util.*; public class Main { public static void main(String[] args) { //Scanner class object created Scanner s = new Scanner(System.in); //taking input of 'n' value from user System.out.println("Enter the nth value of the series:"); int n=s.nextInt(); // method call, to print the series m1(n); } public static void m1(int n) { //printing the series by using for loop for(int i = 1; i<=n; i++) { System.out.print(((i*(i+1))/2) + " "); } } }
Output: Enter the nth value of the series: 10 1 3 6 10 15 21 28 36 45 55
Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples
Answer these:
- Write a program to print following 1 3 6 10 15 21 28 sequence using for loop?
- Triangular assortment program in java?
- Write a program to print the pattern for the given n number of rows?
- Triangle numbers hackerrank solution?
- Write a program to find sum of the factors in triangular numbers for a given input?
- Find n th term of series 1 3 6 10 15 21 in java?
- 1 3 6 10 series in java?
- 13610 series sum?
Related Java Programs: