C Language Tasks for Beginners

 

 Code :


#include <stdio.h>
int main()
{
    int i, j, k;
    for (i = 5; i >= 1; i--)
    {
        for (j = 5; j >= i; j--)
        {
            printf(" ");
        }
        for (k = 1; 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 = 5 to i >= 1) controls the number of rows. The number of stars decreases with each row in a right-aligned format.
  • First Inner Loop (Spaces): The first inner for loop (j = 5 to j >= i) prints spaces (" ") to right-align the stars.
  • Second Inner Loop (Stars): The second inner for loop (k = 1 to k <= i) prints stars (*) starting from 1 up to the current row number i.
  • New Line: printf("\n"); moves the output to a new line after printing each row.

Output :

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