×

Floyd’s triangle in C

Floyd’s triangle in C

Floyd’s triangle is named after a person Robert Floyd. It is a right-angled triangle that is of natural numbers starting from 1 and consecutively selects the upcoming more significant number in the sequence. It can also be defined as filling the rows of a triangle with consecutive numbers, starting with the number 1 on the top left corner.

E.g.:

            1
            2  3
            4  5  6
            7  8  9  10 

Algorithm:

Step 1: input the number of rows to be printed. Let us consider the variable ‘n’ for it.

Step 2: initialize another variable ‘i’ and iterate it for ‘n’ times. This will later print the rows.

Step 3: initialize another variable ‘j’ and iterate it for inner iterations.

Step 4: initialize and print a variable named ‘k’.

Step 5: increment the initialized variable ‘k’.

Step 6: include a newline character (escape sequence) after each iteration.

Step 7: return.

Pseudocode:

A pseudocode is a notation resembling a simplified programming language that is used in program design. Pseudocode for Floyd’s triangle is:

 start
 for i = 1 to n do
             for j = 1 to i do
 print k
 increment k
             end for
             print newline
             end for
             end 

Program code to get floyd’s triangle:

 #include <stdio.h>
 #include <conio.h>
 #include <math.h>
 int main()
 {
 int n, i, j;
 int n = 5;
 int k = 1;
 for (1 = 1; i <= n; i++)
 {
 for (j = 1; j <= i; j++)
 {
 printf (“ %3d”, k++);
 }
 printf (“\n”);
 }
 return 0;
 } 

Output:

 1
 2     3
 4     5    6 
 7     8    9   10
 11  12  13  14  15 
 #include <stdio.h>
 #include <conio.h>
 #include <math.h>
 int main()
 {
 int n, i, j;
 int k = 1;
 printf (“Enter the number of rows of the Floyd’s triangle to be printed \n”);
 for (1 = 1; i <= n; i++)
 {
 for (j = 1; j <= i; j++)
 {
 printf (“ %3d”, k);
 k++;
 }
 printf (“\n”);
 }
 return 0;
 } 

Output:

 Enter the number of rows of the Floyd’s triangle to be printed:
 7
 1
 2     3
 4     5    6
 7     8    9   10
 11   12  13  14  15 
 16   17  18  19  20  21
 22   23  24  25  26  27  28 
  • The above program codes print right angled Floyd’s triangle.
  • The only loop used to execute and try this is for loop.
  • The header files used are stdio.h and conio.h using the “inlcude” preceding with # which infer that the header files need to be accessed before the compilation. There are called the preprocessor directives. Stdio header is used for standard input output. This is used for getting the input from the user (keyboard) and output the result to the monitor (screen). Conio header file is used in old MS_DOS compilers to create a text user interface. It will not be a part of the C programming language.
  • The main() function is declared with the return type as an integer. Declare four variables ‘n’, ‘i’, ‘j’, ‘k’ and assign the value of ‘k’ to 1.
  • The printf() function is used for displaying the message “Enter the number of rows for required structure of floyd’s triangle”. Read the same using scanf() function which fetches the value from the user and stores it in variable ‘n’.
  • Include two loops in the form of nested for. First for loop will count using the variable ‘i’ as 1 and check the condition i <= n. Again, the second nested for loop going from k = 1 till k <= i.
  • Then output the function which is used within nested for to print all the numbers and increment the value of ‘k’ by 1.
  • Once the inner loop is executed, printf() will take over the printing cursor to the next line using the escape sequence or newline character ‘\n’.

Reverse of Floyd’s triangle:

 #include <stdio.h>
 #include <conio.h>
 int main()
 {
 int i, j, n;
 printf (“Enter the number of rows to be printed in the Floyd’s triangle: \n”);
 scanf (“%d”, &n);
 printf (“Reverse of Floyd’s triangle is: \n”);
 int k = (n * (n + 1)) /2;
 for (i = n; i >= 1; i--)
 {
 for (j = i; j >= 1; j--)
 {
 printf (“ %4d”, k);
 K--;
 }
 printf (“\n”);
 }
 return 0;
 } 

Output:

 Enter the number of rows to be printed in the Flyod’s triangle:
 5
 Reverse of Floyd’s triangle is:
 10    9   8   7
 6     5   4 
 3     2  
 1  

Related Topics

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Evaluation of Arithmetic Expression in C

We can use the "eval" function in C to evaluate an arithmetic expression stored as a string. However, using "eval" is generally considered bad practice and can lead to security...

4 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Volatile in C

Introduction A volatile keyword is a qualifier in C. Qualifiers are nothing but keywords which are used to modify the properties of a variable. Qualifiers are of two types: 1) Const The const type...

3 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

Local Labels in C

Anyone who has written programs in the C programming language is required to be familiar with the "go to" and "labels" used in C to navigate between functions. "Local labels"...

4 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Infix to Postfix program in C

Infix Expression: In infix expression, an operator is placed between the two operands. Example: x + y, here operator + is placed between operands x and y. Postfix Expression: In...

3 minutes read.

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.