×

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

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 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.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

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.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

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.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

What is required in each C Program?

Each C program must require one function, i.e., main() function. It is because when we execute the C program, C compiler looks for the main() function, and from here only...

5 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.