-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriangleSequence.cpp
40 lines (31 loc) · 923 Bytes
/
TriangleSequence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// This Triangular Number Sequence is generated from a pattern of dots that form a triangle. The first 5 numbers
// of the sequence, or dots, are:
// 1, 3, 6, 10, 15
// This means that the first triangle has just one dot, the second one has three dots, the third one has 6 dots
// and so on.
// Write a function that returns the number of dots when given its corresponding triangle number of the sequence.
// Examples
// triangle(1) ➞ 1
// triangle(6) ➞ 21
// triangle(215) ➞ 23220
#include <iostream>
using namespace std;
int triangle(int n)
{
int finalOutput = 0;
for (int i = 1; i <= n; i++)
{
finalOutput = finalOutput + i;
}
return finalOutput;
}
int main()
{
cout << triangle(1) << endl;
cout << triangle(2) << endl;
cout << triangle(3) << endl;
cout << triangle(4) << endl;
cout << triangle(5) << endl;
cout << triangle(6) << endl;
return 0;
}