Floyd's triangle in C

  • برمجة سي c

Floyd's triangle in C

C program to print Floyd's triangle: a user inputs how many rows of the triangle to print. The first four rows of Floyd's triangle are:
1
2 3
4 5 6
7 8 9 10
In Floyd's triangle, the nth row contains n numbers. Total numbers in the triangle of n rows: n*(n+1)/2.

الأجوبة

#include <stdio.h>
int main()
{
  int n, i,  c, a = 1;

  printf("Enter the number of rows of Floyd's triangle to print\n");
  scanf("%d", &n);

  for (i = 1; i <= n; i++)
  {
    for (c = 1; c <= i; c++)
    {
      printf("%d ", a); // Please note space after %d
      a++;
    }
    printf("\n");
  }

  return 0;
}

output:

Enter the number of rows of Floyd's triangle to print

5

2 3 

4 5 6 

7 8 9 10 

11 12 13 14 15 

هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...