﻿ C Program: Print the Floyd's Triangle - w3resource

# C Exercises: Print the Floyd's Triangle

## C For Loop: Exercise-22 with Solution

Write a program in C to print Floyd's Triangle.
The Floyd's triangle is as below :

```1
01
101
0101
10101
```

Visual Presentation: Sample Solution:

C Code:

``````#include <stdio.h> // Include the standard input/output header file.
int main() {
int i, j, n, p, q; // Declare variables to store input and control loop indices.

printf("Input number of rows : "); // Prompt the user for input.
scanf("%d", &n); // Read the value of 'n' from the user.

for (i = 1; i <= n; i++) // Loop for the number of rows.
{
if (i % 2 == 0) // Check if 'i' is even.
{
p = 1;
q = 0;
}
else // If 'i' is odd.
{
p = 0;
q = 1;
}

for (j = 1; j <= i; j++) // Loop for each element in the row.
{
if (j % 2 == 0) // Check if 'j' is even.
printf("%d", p); // Print 'p' if 'j' is even.
else
printf("%d", q); // Print 'q' if 'j' is odd.
}

printf("\n"); // Move to the next line after printing a row.
}

return 0; // Return 0 to indicate successful execution.
}

```
```

Sample Output:

```Input number of rows : 5
1
01
101
0101
10101
```

Flowchart: C Programming Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿