
Originally Posted by
Salem
Start with better variable names.
Don't be afraid to create further ones to add meaning.
Don't be afraid to create functions to do bits of repetitive work.
Code:
void print_symbol(char c, int n) {
for ( int i = 0 ; i < n ; i++ ) putchar(c);
}
int main ( ) {
for ( row = 0 ; row < 5 ; row++ ) {
num_spaces = row;
num_stars = row;
print_symbol(' ', num_spaces);
print_symbol('*', num_stars);
printf("\n");
}
}
Now all you need is the right calculations.
I tried your approach and I have written code. I get expected output but there is one problem I am getting first empty column and I don't know how to remove it
Code:
#include<stdio.h>
void print_symbol(char c, int n) {
for ( int i = 0 ; i < n ; i++ )
putchar(c);
}
void print_symbol2(char c, int n) {
for ( int i = 5 ; i >=n ; i-- )
putchar(c);
}
int main ( )
{
int row, num_spaces, num_stars;
for ( row = 5 ; row > 0; row-- )
{
num_spaces = row;
num_stars = row;
print_symbol(' ', num_spaces);
print_symbol2('*', num_stars);
printf("\n");
}
}