How to form triangle | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to form triangle

In c launguge which is fenced by stars

13th Sep 2019, 2:18 PM
Ramya Shree
1 Answer
+ 2
/* The following prints a triangle filled with spaces and surrounded by asterisk(*). If you want to get the user to specify the triangle's size, add something like this to main just after the size declaration: printf("How large do you want the triangle?\n"); scanf("%d", &size); */ #include <stdio.h> void printChars(char c, int numRepeats) { for (int i = 0; i < numRepeats; i++) { printf("%c", c); } } void printSpaces(int numRepeats) { printChars(' ', numRepeats); } int main() { int size = 12; printSpaces(size); printf("*\n"); for (int i = 1; i < size; i++) { printSpaces(size - i); printf("*"); printSpaces(i * 2 - 1); printf("*\n"); } printChars('*', 1 + size * 2); return 0; }
13th Sep 2019, 5:49 PM
Josh Greig
Josh Greig - avatar