Thursday, February 13, 2014

C Tutorial: Floyd Triangle

Floyd's triangle 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.

Source Code:

#include <stdio.h>

int main() {
int rows, a,  b, number = 1;

printf("Number of rows to print:");
scanf("%d",&rows);

for ( a = 1 ; a <= rows ; a++ ) {
for ( b = 1 ; b <= a ; b++ ) {
printf("%d ", number);
number++;
}
printf("\n");
}

}

Output is as:
                         

No comments:

Post a Comment