Home > Backend Development > C++ > Write a program in C language that prints the reverse Floyd triangle

Write a program in C language that prints the reverse Floyd triangle

WBOY
Release: 2023-08-31 18:41:04
forward
1518 people have browsed it

Program Description

Freud's triangle is a right triangle 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 1 in the upper left corner

1                               15 14 13 12 11
2 3                             10 9 8 7
4 5 6                         6 5 4
7 8 9 10                       3 2
11 12 13 14 15                 1
<strong>Floyd&#39;s Triangle                </strong><strong>Reverse of Floyd&#39;s Triangle</strong>
Copy after login

Algorithm

Print Freud's triangle:

Accept the number of rows to print the Floyd&rsquo;s Triangle
Print value 1 for the Row 1
Print two values 2 and 3 in the next row
Print three values 4, 5 and 6 in the next row
Repeat till the number of rows specified
Copy after login

To print out the inverse of Freud’s triangle - the Chinese translation of

Accept the number of rows to print the reverse of Floyd&rsquo;s Triangle
Print the values in the reverse order as specified in the reverse of Floyd&rsquo;s Triangle
Copy after login

Example

is:

Example

/*Program to print the Reverse of Floyd&#39;s Triangle*/
#include<stdio.h>
int main() {
   int r,c=1;
   int rows,revrows,r1,c1,d;
   clrscr();
   printf("Enter number of rows to print the Floyd&#39;s Triangle: ");
   scanf("%d", &rows);
   printf("</p><p>");
   for (r=1;r<=(rows*(rows+1))/2;r++){
      printf("%d ",r);
      if(r==(c*(c+1))/2){
         printf("</p><p>");
         c++;
      }
   }
   printf("</p><p></p><p>");
   /*Printing the Reverse of Floyd&#39;s Triangle*/
   printf("Enter number of rows to print the reverse of Floyd&#39;s Triangle: ");
   scanf("%d",&revrows);
   printf("</p><p></p><p>");
   printf("Reverse of Floyd&#39;s Triangle</p><p>");
   printf("</p><p></p><p>");
   d = (revrows*(revrows+1))/2;
   for(r1=revrows;r1>=1;r1--){
      for(c1=r1;c1>=1;c1--,d--){
         printf("%4d", d);
      }
      printf("</p><p>");
   }
   getch();
   return 0;
}

Copy after login

Output

Write a program in C language that prints the reverse Floyd triangle

The above is the detailed content of Write a program in C language that prints the reverse Floyd triangle. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template