Print the Triangle Pattern Facing Downward Using For Loop in C Programming
This program is written in the C programming language, and it is used to print a square of asterisks (*) with a user-specified number of rows and columns.
- The program starts by including the standard input/output header file, "stdio.h". Then, the main() function is defined. Inside the main function, three variables are declared: "i" and "n" are used as counter variables in the nested for loops, and "a" is used to store the user input value.
- The program then prompts the user to enter a number using the printf() function and stores the value in the "a" variable using the scanf() function. The for loop is then used to repeatedly execute the code inside the loop for the number of times specified by the user. The for loop starts with "i" initialized to 1, and the loop continues until "i" is less than or equal to "a".
- The second for loop is nested inside the first for loop, and it is used to repeatedly execute the code inside the loop for the number of times specified by the user. The for loop starts with "n" initialized to 1, and the loop continues until "n" is less than or equal to "a".
- The printf() function inside the nested loop is used to print the symbol "*" on the screen each time the loop is executed. The value of "i" and "n" are incremented by 1 in each iteration of the loops using the increment operator (++) so that the loops will eventually terminate when "i" and "n" reach the value of "a".
- Finally, the program is exited by returning 0 from main. This program will print a square of the user-specified size and the square will be made of asterisk(*)
Source Code
#include<stdio.h>
int main()
{
int i,n,a;
printf("\nEnter the value:");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
printf("\n");
for(n=1;n<=a;n++)
{
printf(" *");
}
}
return 0;
}
To download raw file
Click Here
Output
Enter the value:5
* * * * *
* * * *
* * *
* *
*