Write a C program to print V star pattern using for loop.
#include <stdio.h> int main() { int rows; printf("Enter V Shape Star Pattern Rows = "); scanf("%d", &rows); printf("Printing V Shape Star Pattern\n"); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { printf("*"); } for (int k = 1; k <= 2 * (rows - i); k++) { printf(" "); } for (int l = 1; l <= i; l++) { printf("*"); } printf("\n"); } }
C program to print the V star pattern of stars using a while loop
#include <stdio.h> int main() { int i, j, k, l, rows; printf("Enter V Shape Star Pattern Rows = "); scanf("%d", &rows); printf("Printing V Shape Star Pattern\n"); i = 1; while (i <= rows) { j = 1; while (j <= i) { printf("*"); j++; } k = 1; while (k <= 2 * (rows - i)) { printf(" "); k++; } l = 1; while (l <= i) { printf("*"); l++; } printf("\n"); i++; } }
Enter V Shape Star Pattern Rows = 13
Printing V Shape Star Pattern
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********** **********
*********** ***********
************ ************
**************************
This C example uses the do while loop to print the alphabetical V shape star pattern.
#include <stdio.h> int main() { int i, j, k, l, rows; printf("Enter V Shape Star Pattern Rows = "); scanf("%d", &rows); printf("Printing V Shape Star Pattern\n"); i = 1; do { j = 1; do { printf("*"); } while (++j <= i); k = 1; while (k <= 2 * (rows - i)) { printf(" "); k++; } l = 1; do { printf("*"); } while (++l <= i); printf("\n"); } while (++i <= rows); }
Enter V Shape Star Pattern Rows = 16
Printing V Shape Star Pattern
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********** **********
*********** ***********
************ ************
************* *************
************** **************
*************** ***************
********************************
In this C example, the ShapeVPattern function allows entering any character and prints the alphabetical V pattern of a given character.
#include <stdio.h> void ShapeVPattern(int rows, char ch) { for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { printf("%c", ch); } for (int k = 1; k <= 2 * (rows - i); k++) { printf(" "); } for (int l = 1; l <= i; l++) { printf("%c", ch); } printf("\n"); } } int main() { int rows; char ch; printf("Enter Character for V Pattern = "); scanf("%c", &ch); printf("Enter V Shape Star Pattern Rows = "); scanf("%d", &rows); printf("Printing V Shape Pattern\n"); ShapeVPattern(rows, ch); }
Enter Character for V Pattern = $
Enter V Shape Star Pattern Rows = 19
Printing V Shape Pattern
$ $
$$ $$
$$$ $$$
$$$$ $$$$
$$$$$ $$$$$
$$$$$$ $$$$$$
$$$$$$$ $$$$$$$
$$$$$$$$ $$$$$$$$
$$$$$$$$$ $$$$$$$$$
$$$$$$$$$$ $$$$$$$$$$
$$$$$$$$$$$ $$$$$$$$$$$
$$$$$$$$$$$$ $$$$$$$$$$$$
$$$$$$$$$$$$$ $$$$$$$$$$$$$
$$$$$$$$$$$$$$ $$$$$$$$$$$$$$
$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$