In this tutorial you will learn about the C Program to Print Floyd’s Triangle and its application with practical example.
C Program to Print Floyd’s Triangle
In this tutorial, we will learn to create a C program that will Print a Floyd’s Triangle using C programming.
Prerequisites:-
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C Programming.
Floyd’s Triangle:-
Floyd’s triangle is a right-angled triangle that contains mathematical numbers from 1 and goes up to the required point.
Printing a Floyd’s:-
The C language is a very powerful programming language. In C programming we can perform many operations with the help of codings. The c language is very easy to create any shape. In this program, we will learn to create Floyd’s triangle with the help of some code.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
STEP 1: START the program STEP 2: first declare the variables STEP 3: then start the parent for loop STEP 4: now make the child for loop STEP 5: now use child loop to start drawing the Floyd's STEP 6: Now this for loop will also execute under the parent for loop STEP 7: Print "\n" for changing the line before the increment of the main loop STEP 8: increment of the main for loop STEP 9: return the zero value for main function |
Program:-
To print a Floyd’s Triangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> int main() { //variable declaration int num, i, j, k = 1; //Taking the number of roes a input printf( " Enter the number of rows wanted in Floyd's triangle: \n"); scanf( "%d", &num); // using nested for loop. // The main for loop define the rows and check rows condition. for (i = 1; i <= num; i++) { // Sub for loop check j should be less than equal to 1 and print the data. for (j = 1; j <= i; j++) { // printing the floyd's triangle using the loop. printf(" %2d", k++); // printing the numbers. } printf("\n"); } return(0); } |
Output:-
In the above program, we have first initialized the required variable.
- i = it will hold the integer value to control parent for loop.
- j = it will hold the integer value to control child for loop.
- k = it will hold the integer value to control child for loop.
- num = it will hold the integer value for the number of rows.
Taking input Number of rows
Nested For loop the body of the program
Printing Numbers