+ 2
How to form triangle
In c launguge which is fenced by stars
1 Antwort
+ 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;
}