C program to print Floyd's triangle

Here, we are going to learn how to print Floyd's triangle using C program? Submitted by Nidhi, on August 01, 2021

Problem Solution:

Read the total number of rows from the user, and then print Floyd's triangle.

Floyd's triangle:

Floyd's triangle is a triangular array of natural numbers. In this triangle, the rows of the triangle are filled with consecutive natural numbers, starting with a 1 in the top left corner.

Example:

Floyd's triangle

Program:

The source code to print Floyd's triangle is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to print Floyd's triangle

#include <stdio.h>

int main()
{
    int outer = 0;
    int inner = 0;
    int number = 1;
    int rows = 0;

    printf("Enter the total number of rows: ");
    scanf("%d", &rows);

    for (outer = 0; outer <= rows; outer = outer + 1) {
        for (inner = 1; inner < outer + 1; inner++) {
            printf("%d ", number);
            number = number + 1;
        }
        printf("\n");
    }

    return 0;
}

Output:

RUN 1:
Enter the total number of rows: 4

1 
2 3 
4 5 6 
7 8 9 10 

RUN2:
Enter the total number of rows: 10

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 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55 

RUN 3:
Enter the total number of rows: 15

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 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55 
56 57 58 59 60 61 62 63 64 65 66 
67 68 69 70 71 72 73 74 75 76 77 78 
79 80 81 82 83 84 85 86 87 88 89 90 91 
92 93 94 95 96 97 98 99 100 101 102 103 104 105 
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

Explanation:

Here, we read the total number of rows from the user and then printed Floyd's triangle using the nested loop on the console screen.

C Sum of Series Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.