C program to print heart star pattern

Write a C program to print heart star pattern using for loop. The pattern should look like
  *****     *****
 *******   *******
********* *********
*******************
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *

Required knowledge:

Basic C programming, For loop, Nested loop

Program:

/**
 * C program to print heart star pattern 
 */

#include <stdio.h>

int main()
{
    int i, j, n;

    printf("Enter value of n : ");
    scanf("%d", &n);

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

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

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

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

        printf("\n");
    }

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

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

        printf("\n");
    }

    return 0;
} 
Output:
C program to print heart star pattern
Happy coding ;)

View all common star patterns.
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

4 comments:

  1. output is not shown..after enter number it will gone

    ReplyDelete
    Replies
    1. The program is correct.But if you are using Turbo C compiler the press Alt + F5 to see the output or else add a line system("pause"); just before return 0; to pause the screen.

      Delete
  2. I heart fully appreciate your work.it is my request to you to add detailed manual execution of given code if possible

    ReplyDelete
    Replies
    1. Thank's Murali, I also have been thinking about adding details about execution of the program and I am working on that. Surely you will see that in sometime.

      Again great thanks to you.

      Delete