In: Computer Science
A triangular number is the sum of the n natural numbers from 1 to n.
For example:
Write a program segment (using a loop), that calculates and then prints the integer n and its triangular number.
Source Code:
Output:
Code in text format (See above images of code for indentation):
#include <iostream>
using namespace std;
/*main function*/
int main()
{
/*varibles*/
int n,i,tnum=0;
/*read n value from user*/
cout<<"Enter n value: ";
cin>>n;
/*using loop calculate triangular number*/
for(i=1;i<=n;i++)
tnum+=i;
/*print number and triangular number*/
cout<<"The number n is: "<<n<<" and
its triangular number is "<<tnum;
return 0;
}