Series of triangular numbers – CSKC
Sign In

Search

Code Learning #CPP#2864    siteicon   siteicon  

Series of triangular numbers

Program to print the triangular series like – 1 3 5 10 15 ……….

Learning Objectives

  • The concept of triangular series 1 3 5 10 15 ..   . A series that represents the balls that can be placed over each other to form an equilateral triangle.

Source Code

TC++ #2864

 

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Alternate (?) :

Code Understanding

int j=1,k=1; //Initialisation of variables

int terms; cout<<“Give no. of terms :”; cin>>terms;
Collecting number of terms from the user

for(int tc=1;tc<=terms;++tc) //Series loop upto the terms count
{
cout<<k<<” “;  //Printing of the term
j+=1; k+=j;        //Next term creation logic
}

Notes

  • Triangular numbers as shown in bold here by adding the another series of 2, 3,4,5 to the previous number.
    1
      2  3  3  6  4  10  5  15  6  21  7   28….
    That is why the logic says j=j+1 followed by k=k+j so that the j series moves 1 each time and k series moves by j each time.
  • Some times the triangular series is written from 0 like 0 1 3 6 10 15 … etc. In that case you can initialise both j and k as 0 and you will get the result.
  • The interesting behaviour of a triangular series can be studied at https://en.wikipedia.org/wiki/Triangular_number

Common Errors

  • Some times student forget to initialise both j and k. Unless initial values are given the series will not move properly.


Suggested Filename(s): triangular-series.cpp, tri-ser.cpp



Share

Created: 20-Jan-2018 20:37 | Updated: 20-Jan-2018 20:50|