Saturday 14 July 2012

Floyd's Triangle in C

Floyds's triangle - 

 Its is a right-angled triangular array of natural numbers , used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner: 

1
2 3
4 5 6
7 8 9 10



Sample Output -



Code -


/* C program to print Floyd's triangle:- This program prints Floyd's triangle. Number of rows of Floyd's triangle to print is entered by the user. First four rows of Floyd's triangle are as follows :-
1
2 3
4 5 6
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers. */

#include<stdio.h>

#include<conio.h>

int main()
{
    int number_rows = 0, rows = 0, coloumn = 0, number = 1;

    printf("\n\n\t\t __ Program to print Floyd's Triangle in C __");

    printf("\n\n\n  Enter the number of Rows - ");

    scanf("%d",&number_rows);

    printf("\n\n");

    for(rows = 1; rows <= number_rows; rows++)
    {
        printf("\t");

        for(coloumn = 1; coloumn <= rows; coloumn++)
        {
            printf("%d ", number);
           
            number++;    //__ Number variable represents the numbers to be printed ___
        }

        printf("\n\n");
    }

    getch();

    return 0;

}



No comments:

Post a Comment