In this article we will teach you how to write a C Program To Print The Floyd Triangle Increased By 1. We have to write this program with our required pattern of Floyd triangle by using Loops. Loop is an iterative and repetition statement. We have to use for loop in this program to print the required pattern.
We can also use the while loop and do-while loop to draw the pattern. For loop is the simplest and easy loop as compared to the other types of loops. So let’s start creating the first and easy program of the loop to print the Floyd triangle increased by 1 number.
C Program To Print The Floyd Triangle Increased By 1:
In this program, we have to make two loops. A loop within a loop is called a nested loop. First, we have to initialize and declare the variable for both the loops that are i and j. Variable i is for the outer loop i is used as a row and variable j is for the inner loop which is used for the column numbers.
When i is equal to 1 in the first loop, it will move to the loop’s conditional part and check the condition. If the condition is true it will move to the nested loop and the value of I will be incremented from 1 to 2. But this is a post increment so in the nested loop, I will be used as 1.
If the condition of the first loop will not meet the necessary condition it will not move to the nested loop. In the 2nd loop j=1 then it will check the condition i is also equal to 1 so it will print the value as we take the variable count=0 in the program. And store the value in the pre-increment count.
This will print the value 1 because the count is equal to 0 at the start of the program. These steps will repeat themselves until the condition is true. When the condition is false, it will move to the body of the main and execute the statements of the main.
Program:
#include<stdio.h>
int main(){
int i,j,count=0;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
printf(“%d\t”,++count);
}
printf(“\n”);
}
}
Output:
In the above image it will clearly show the result without taking the number of rows and our output will be shown on the display screen.
//If you want to take values from the user//
If you want to take the rows from the user then you will declare another variable as we take num in this program for the number of rows. You will first print on the screen Enter the number of Rows and by using the scan function you will have to give the value of rows.
This num variable will be used in the first loop. This will give you the Floyd triangle of your own choice. Suppose in the upper section/program we give the condition that is less than or equal to 5. But here you will give the number of rows and make your condition to execute the loop.
#include<stdio.h> int main(){ int i,j,num,count=0; printf("ENTER THE NUMBER OF ROWS:\n"); scanf("%d",&num); for(i=1;i<=num;i++){ for(j=1;j<=i;j++){ printf("%d\t",++count); } printf("\n"); } }
Output:
In the above image you can see that compiler will ask for the value of rows that the user must have to enter. When we enter the number of rows is equal to 7 it will give the result according to our condition.