C (programming language)

# C program to print 1 3 6 10 15 21 28 36 triangular series

**C Program to Print 1 3 6 10 15 21 28 36 Triangular Series**

**Understanding Triangular Numbers**

- Triangular numbers are a sequence of numbers where each number is the sum of all natural numbers up to that point.
- They form a triangular pattern when represented visually, as shown below:

```
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
```

- The first few triangular numbers are 1, 3, 6, 10, 15, 21, 28, 36, and so on.

**Methods for Printing Triangular Numbers**

There are two common methods to print triangular numbers in C:

**Using a single loop****Using the formula for triangular numbers**

**1. Using a single loop**

C

`#`**include** <stdio.h>
int main() {
int n = 8; // Number of triangular numbers to print
int i, j, sum = 0;
printf("Triangular Series:\n");
for (i = 1; i <= n; i++) {
sum += i; // Calculate the current triangular number
printf("%d ", sum);
}
printf("\n");
return 0;
}

**Explanation:**

- The
`for`

loop iterates from 1 to`n`

. - Inside the loop,
`sum`

is calculated by adding the current value of`i`

to the previous value of`sum`

. - The calculated
`sum`

is then printed, representing the current triangular number.

**2. Using the formula for triangular numbers**

C

`#`**include** <stdio.h>
int main() {
int n = 8;
int i;
printf("Triangular Series:\n");
for (i = 1; i <= n; i++) {
int triangular = i * (i + 1) / 2; // Formula for triangular numbers
printf("%d ", triangular);
}
printf("\n");
return 0;
}

**Explanation:**

- The formula
`i * (i + 1) / 2`

directly calculates the`i`

-th triangular number. - The loop iterates through the desired number of terms and prints the calculated triangular numbers.

**Key Points:**

- Both methods produce the same output, so choose the one that you find more intuitive or efficient.
- Ensure proper indentation for readability.
- Consider using meaningful variable names to enhance code clarity.
- Validate user input to prevent unexpected behavior or errors.
- Explore different formatting options for output (e.g., vertical display, custom separators).
- Experiment with generating different triangular series (e.g., even-numbered triangular numbers, odd-numbered triangular numbers).