×

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

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.

C program to find factorial of a number using Recursion

What does the term "Factorial of a Number" mean? In mathematics, factorial is the product of all positive integers that are less than or equal to a certain positive integer, and...

2 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

Decimal to Hexadecimal in C

Decimal to Hexadecimal in C Let us first understand the definitions of decimal and hexadecimal. In algebra, a decimal number is defined as a number whose whole number part and a...

3 minutes read.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

2 minutes read.

Recursion in C

Recursion is a programming technique that allows the programmer to call the function within the same function. The function which calls the same function is known as recursive function but a...

1 minute read.