Star pattern programs in C – 1

Star pattern programs in C – 1

Pattern programs are widely asked questions in university exams and interviews. These type of questions test logic and concept of a programmer. Every pattern requires different logic. All type of patterns use nested for loops. Some of the pattern uses more than two for loops. Let’s see some star pattern programs in C.

1. Code

Q. Write C program to print following pattern.
*
**
***
****
*****

/*
Pattern -
    *
    **
    ***
    ****
    *****
*/

#include<stdio.h>

int main()
{
    int i,j,n;
    printf("\n Enter no of rows:");
    scanf("%d",&n);

    for (i = 0; i < n; i++)
    {
        for (j = 0; j <= i; j++)
        {
            printf("*");
        }

        printf("\n");
        
    }
    
    return 0;
}

Output –

Explanation –

This is the simplest pattern. In this, first for loop is for number of rows and inner for loop is for number of stars to print in each row. Inner for loop will execute itself till the value of j is less than i. After every successful execution of inner for loop, the compiler will goto next line. This happens because of printf(“\n”); statement.

Q. Write C program to print following pattern.
*****
****
***
**
*

/*
PATTERN - 
    *****
    ****
    ***
    **
    *
*/

#include<stdio.h>

int main()
{
    int i,j,n;
    printf("\n Enter no of rows:");
    scanf("%d",&n);

    for (i = 1; i <= n; i++)
    {
        for (j = 0; j <= n-i; j++)
        {
            printf("*");
        }

        printf("\n");
        
    }
    
    return 0;
}

Output –

Star Pattern programs in C

Explanation –

This is one of the simple patterns ever asked. In this, first for loop is for number of rows and inner for loop is for number of stars to print in each row. Inner for loop will execute itself till the value of j is less than n-i.That is we have to print a triangle in reverse manner. Here in first row number os stars will be equal to number of rows. In next row number of stars will be one less than previous row. This will take place till specified number of rows are printed. After every successful execution of inner for loop, the compiler will goto next line. This happens because of printf(“\n”); statement.

Q. Write C program to print following pattern.
*
**
***
****
*****

/*
PATTERN - 
        *
       **
      ***
     ****
    *****
*/

#include<stdio.h>

int main()
{
    int i,j,n;
    printf("\n Enter no of rows:");
    scanf("%d",&n);

    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n-i; j++)
        {
            printf(" ");
        }

        for (j = n-i; j < n; j++)
        {
            printf("*");
        }

        printf("\n");
        
    }
    
    return 0;
}

Output –

Star Pattern programs in C

Explanation –

This pattern looks complicated but is very easy if your nested for loop is clear. In this, first for loop is for number of rows. Inner first for loop is for number of blanks to print in each row. Second inner for loop is for printing star. In first inner for loop, number of blank spaces depend upon the row number. For eg if 5 is given as input then in first row number of blank spaces will be 4, in second row it will be 3 and so on. Number of blank spaces will decrease with the row. After every successful execution of inner for loop, the compiler will goto next line. This happens because of printf(“\n”); statement.

Q. Write C program to print following pattern.
*****
****
***
**
*

/*
PATTERN - 
    *****
     ****
      ***
       **
        *
*/

#include<stdio.h>

int main()
{
    int i,j,n;
    printf("\n Enter no of rows:");
    scanf("%d",&n);

    for (i = 0; i < n; i++)
    {
        for (j = i; j > 0; j--)
        {
            printf(" ");
        }

        for (j = n-i; j > 0; j--)
        {
            printf("*");
        }

        printf("\n");
        
    }
    
    return 0;
}

Output –

Star Pattern programs in C

Explanation –

This pattern looks complicated but is very easy if your nested for loop is clear. In this, first for loop is for number of rows. Inner first for loop is for printing star. Second inner for loop is for number of blanks to print in each row. The number of blank spaces depend upon the row number. For eg if 5 is given as input then in first row number of blank spaces will be 0, in second row it will be 1 and so on. Number of blank spaces will increase with the row. After every successful execution of inner for loop, the compiler will goto next line. This happens because of printf(“\n”); statement.

Q. Write C program to print pascal triangle.
*
***
*****
*******
*********

/*
PATTERN - 
      *
     ***
    *****
   *******
  *********
*/

#include<stdio.h>

int main()
{
    int i,j,n;
    printf("\n Enter no of rows:");
    scanf("%d",&n);

    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n-i; j++)
        {
            printf(" ");
        }

        for (j = n-i; j < n; j++)
        {
            printf("*");
        }

        for (j = n-i+1; j < n; j++)
        {
            printf("*");
        }

        printf("\n");
        
    }
    
    return 0;
}

Output –

Star Pattern programs in C

Explanation –

Pascal’s triangle is one of the interesting and widely asked pattern in university/college exams. It looks complicated but is very simple. This pattern uses 4 for loops. One outer for loop for number of rows and three inner for printing stars. First inner for loop prints spaces. Depending upon number of rows user give as input blank space is printed. For eg if 5 is given as input then in first row number of blank spaces will be 4, in second row it will be 3 and so on. Number of blank spaces will decrease with the row.

Second inner for loop is for printing left hand side triangle. In first iteration the loop prints first star in second iteration two stars and so on. Third inner for loop is for right hand side triangle. Here in first iteration the loop prints 0 star, in second loop it prints one star and so on. This is because the second for loop has already printed one star. And this is how we get a pascal’s triangle of star pattern.

Q. Write C program to print downwards/reverse pascal triangle.
*********
*******
*****
***
*

/*
PATTERN - 
    *********
     *******
      *****
       ***
        *
*/

#include<stdio.h>

int main()
{
    int i,j,n;
    printf("\n Enter no of rows:");
    scanf("%d",&n);

    for (i = 1; i <= n; i++)
    {
        for (j = i-1; j > 0; j--)
        {
            printf(" ");
        }

        for (j = n-i+1; j > 0; j--)
        {
            printf("*");
        }

        for (j = 1; j <= n-i; j++)
        {
            printf("*");
        }

        printf("\n");
        
    }
    
    return 0;
}

Output –

Star Pattern programs in C

Explanation –

This one is tricky but same as normal pascal’s triangle. This pattern uses 4 for loops. One outer for loop for number of rows and three inner for printing stars. First inner for loop prints spaces. Depending upon number of rows user give as input blank space is printed. For eg if 5 is given as input then in first row number of blank spaces will be 0, in second row it will be 1 and so on. Number of blank spaces will increase with the row.

Second inner for loop is for printing left hand side triangle. In first iteration the loop prints number of stars equal to the number of rows entered by user in second iteration one less star and so on. Third inner for loop is for right hand side triangle. Here in first iteration the loop prints one less star than the second inner for loop. And this is how we get a pascal’s triangle of star pattern.

2. Parts of program-

1. #include is a pre-processor directive. It is used to include header files.
2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. int is data type. It indicates that the program returns a value.
5. main() is main function that indicates the compiler that the user written programs starts from here.
6. print/cout is the keyword use to print a message.
7. scanf/cin is the keyword use to store values in variables.
8. return statement is use to return a value. If any error occurs the program will return 0.

Conclusion-

So that’s how we print different star pattern programs in C. We hope it was clear to you. If you find it useful then do share it with your programming buddies and friends. Also if you have any doubt regarding any post do tell us in the comment section.

For daily updates do follow us on Instagram. Visit again!

ErrorFreeProgram.

8 COMMENTS

comments user
wamu car donation

I have found very interesting your article.It’s pretty worth enough for me.
In my view, if all website owners and bloggers maxe good content as
you did, the web will be a lot more useful than ever before.

    comments user
    ErrorFreeProgram

    We are glad that you shared your thoughts. We at errorfreeprogram also think the same. And we will keep on adding meaningful content to the web. We hope you will keep supporting us!

comments user
Hairstyles

I抳e recently started a web site, the information you provide on this web site has helped me greatly. Thanks for all of your time & work.

    comments user
    ErrorFreeProgram

    We are glad you find our content useful!

comments user
Wedding Hairstyles

Thanks – Enjoyed this post, is there any way I can receive an email sent to me whenever you make a fresh article?

Leave a Reply

Your email address will not be published. Required fields are marked *