Skip to content
Home » Floyd’s triangle Pattern in C using recursion

Floyd’s triangle Pattern in C using recursion

Learn about Floyd’s triangle in C using recursion in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Floyd’s triangle in C using recursion

Floyd’s triangle is a right-angled triangular array of natural numbers that is commonly used in computer science classes. It bears the name Robert Floyd. It is defined by completing the triangle’s rows with successive integers beginning with a 1 in the top left corner.

Program:

#include<stdio.h>
int row=1;
int a = 1;
void printFloyd(int n)
{
  if(n<=0) return;

  for(int i=1; i<=row; i++)
  printf("%-5d",a++);

  printf("\n");
  row++;
  printFloyd(n-1);
}

int main()
{
  int n;
  printf("Enter number of rows: ");
  scanf("%d",&n);
  printFloyd(n);
  return 0;
}

Output 1:

Enter number of rows: 5
1    
2    3    
4    5    6    
7    8    9    10   
11   12   13   14   15 

Output 2:

Enter number of rows: 4
1    
2    3    
4    5    6    
7    8    9    10  

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
C Program For Floyd’s Triangle
Program for Printing Pattern using Loops in C