C Language Tasks for Beginners

 

 Code :

#include <stdio.h>
int main()
{
    int i, j, k;
    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= i; j++)
        {
            printf(" ");
        }
        for (k = 5; k >= i; k--)
        {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
  • Header File: #include <stdio.h> is needed to use printf() for printing output.
  • Main Function: The program starts from int main(). It returns 0 to indicate successful completion.
  • Outer Loop (Rows): The outer for loop (i = 1 to i <= 5) controls the number of rows, decreasing the number of stars printed on each successive row.
  • Inner Loop 1 (Spaces): The first inner for loop (j = 1 to j <= i) prints spaces to shift the stars to the right, creating an inverted pyramid structure.
  • Inner Loop 2 (Stars): The second inner for loop (k = 5 to k >= i) prints a star (*) followed by a space, with the number of stars decreasing as the row number increases.
  • New Line: printf("\n"); moves the output to a new line after printing each row.

Output :

* * * * *
 * * * *
  * * *
   * * 
    *