Program to print triangular number series till n
Last Updated :
08 Feb, 2023
A triangular number or triangle number counts objects arranged in an equilateral triangle, as in the diagram on the right. The n-th triangular number is the number of dots composing a triangle with n dots on a side, and is equal to the sum of the n natural numbers from 1 to n.
Examples :
Input : 5
Output : 1 3 6 10 15
Input : 10
Output : 1 3 6 10 15 21 28 36 45 55
Explanation :
For k = 1 and j = 1 -> print k ( i.e. 1);
increase j by 1 and add into k then print k ( i.e 3 ) update k
increase j by 1 and add into k then print k ( i.e 6 ) update k
increase j by 1 and add into k then print k ( i.e 10 ) update k
increase j by 1 and add into k then print k ( i.e 15 ) update k
increase j by 1 and add into k then print k ( i.e 21 ) update k
.
.
and so on.
Approach used is very simple. Iterate for loop till the value given n and for each iteration increase j by 1 and add it into k, which will simply print the triangular number series till n.
Below is the program implementing above approach:
C++
#include <iostream>
using namespace std;
void triangular_series( int n)
{
int i, j = 1, k = 1;
for (i = 1; i <= n; i++) {
cout << k << " " ;
j = j + 1;
k = k + j;
}
}
int main()
{
int n = 5;
triangular_series(n);
return 0;
}
|
C
#include <stdio.h>
void triangular_series( int n)
{
int i, j = 1, k = 1;
for (i = 1; i <= n; i++) {
printf ( " %d " , k);
j = j + 1;
k = k + j;
}
}
int main()
{
int n = 5;
triangular_series(n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void triangular_series( int n)
{
int i, j = 1 , k = 1 ;
for (i = 1 ; i <= n; i++) {
System.out.printf( "%d " , k);
j = j + 1 ;
k = k + j;
}
}
public static void main(String[] args)
{
int n = 5 ;
triangular_series(n);
}
}
|
Python3
def triangular_series( n ):
j = 1
k = 1
for i in range ( 1 , n + 1 ):
print (k, end = ' ' )
j = j + 1
k = k + j
n = 5
triangular_series(n)
|
C#
using System;
class GFG {
static void triangular_series( int n)
{
int i, j = 1, k = 1;
for (i = 1; i <= n; i++) {
Console.Write(k + " " );
j += 1;
k += j;
}
}
public static void Main()
{
int n = 5;
triangular_series(n);
}
}
|
PHP
<?php
function triangular_series( $n )
{
$i ; $j = 1; $k = 1;
for ( $i = 1; $i <= $n ; $i ++)
{
echo ( " " . $k . " " );
$j = $j + 1;
$k = $k + $j ;
}
}
$n = 5;
triangular_series( $n );
?>
|
Javascript
<script>
function triangular_series( n)
{
let i, j = 1, k = 1;
for (i = 1; i <= n; i++)
{
document.write(k+ " " );
j = j + 1;
k = k + j;
}
}
let n = 5;
triangular_series(n);
</script>
|
Output :
1 3 6 10 15
Time complexity : O(n)
Auxiliary Space : O(1), since no extra space has been taken.
Alternate Solution :
The solution is based on the fact that i-th Triangular number is sum of first i natural numbers, i.e., i * (i + 1)/2
C++
#include <iostream>
using namespace std;
void triangular_series( int n)
{
for ( int i = 1; i <= n; i++)
cout << i*(i+1)/2 << " " ;
}
int main()
{
int n = 5;
triangular_series(n);
return 0;
}
|
C
#include <stdio.h>
void triangular_series( int n)
{
for ( int i = 1; i <= n; i++)
printf ( " %d " , i*(i+1)/2);
}
int main()
{
int n = 5;
triangular_series(n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void triangular_series( int n)
{
for ( int i = 1 ; i <= n; i++)
System.out.printf( "%d " ;, i*(i+ 1 )/ 2 );
}
public static void main(String[] args)
{
int n = 5 ;
triangular_series(n);
}
}
|
Python3
def triangular_series(n):
for i in range ( 1 , n + 1 ):
print ( i * (i + 1 ) / / 2 ,end = ' ' )
n = 5
triangular_series(n)
|
C#
using System;
class GFG {
static void triangular_series( int n)
{
for ( int i = 1; i <= n; i++)
Console.Write(i * (i + 1) / 2 + " " );
}
public static void Main()
{
int n = 5;
triangular_series(n);
}
}
|
PHP
<?php
function triangular_series( $n )
{
for ( $i = 1; $i <= $n ; $i ++)
echo ( " " . $i * ( $i + 1) /
2 . " " );
}
$n = 5;
triangular_series( $n );
?>
|
Javascript
<script>
function triangular_series( n)
{
for (let i = 1; i <= n; i++)
document.write( " " + i * (i + 1)/2);
}
let n = 5;
triangular_series(n);
</script>
|
Output :
1 3 6 10 15
Time complexity : O(n)
Auxiliary Space : O(1) , since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...