C Program to Print Heart Shape Star Pattern

Ex: Write a C program to print heart shape star pattern. How to write a C program to print heart shape star pattern. C program  to print heart shape star pattern.


Input from user:

Enter the number: 5

Expected output:
    **     **
  ****   ****
   *********
     *******
       *****
         ***
           *




  Step by step logic of the given program:


1. Accept input number from user.

2. To print upper part of the pattern:
  • Print left peak:
     **
   ****

  • Print right peak:
     **
   ****

3. After that print lower part means inverted triangle:
*********
  *******
    *****
      ***
        *

4. Then you will get heart shape pattern:
    **     **
  ****  ****
  *********
    *******
      *****
        ***
          *




Program:

#include<stdio.h>
void main()
{
int no,i,j,k,z;
printf("Enter the number:\n");
scanf("%d",&no);
/*To print upper part*/
z=no*2;
for(i=no/2;i<=no;i=i+2)
{
/*To print left peak */
for(j=1;j<no-i;j=j+2)
{    
                    printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
/*To print right peak*/
for(j=1;j<=no-i;j++)
{
    printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
printf("\n");
}
/*To print lower part means inverted triangle*/
for(i=1;i<=no;i++)
{
for(j=1;j<i;j++)
{
printf(" ");
}
for(k=1;k<z;k++)
{
printf("*");
}
z-=2;
printf("\n");
}
}


Above program shows the following output:

C program to print heart shape star pattern, heart pattern in C



C program to print inverted half right side of pyramid star pattern

 Ex: Write a C program to print inverted half right side of pyramid star pattern. How to write a C program to print inverted half right side of pyramid star pattern. C program to print inverted half right side of pyramid star pattern.


Input from user:

Enter the number:

5


Expected output:

*****

****

***

**

*



  Step by step logic of the given program:


1. Accept input number from user declare variable say no.


2. Run outer for loop from 1 to no which shows rows in pattern:

for(i=1;i<=no;i++)


3. Run inner for loop from i to no which shows columns in pattern:

for(j=i;j<=no;j++)


4. Inside the inner loop print star:

printf("*");


5. For next row print \n.





Program:


#include<stdio.h>

void main()

{

int i,j,no;

printf("Enter the number:\n");

scanf("%d",&no);

for(i=1;i<=no;i++)

{

for(j=i;j<=no;j++)

{

printf("*");

}

printf("\n");

}

}



Above program shows the following output:


C program to print inverted half right side of pyramid star pattern



C program to print inverted half left side of pyramid star pattern

 Ex: Write a C program to print inverted half left side of the pyramid star pattern. How to write a C program to print inverted half left side of the pyramid star pattern. C program to print inverted half left side of the pyramid star pattern.



Input from user:

Enter the number:

5


Expected output:

*****

  ****

    ***

      **

        *



  Step by step logic of the given program:


1. Accept input number from user declare variable say no.


2. Run outer for loop from 1 to no which shows number of rows:

for(i=1;i<=no;i++)


3. Run inner for loop from 1 to i-1 to print spaces: 

for(j=1;j<i;j++)

{

printf(" ");

}


4. Run another one inner for loop from i to no to print star:

for(k=i;k<=no;k++)

{

printf("*");

}


5. For next row print \n.




Program:


#include<stdio.h>

void main()

{

int i,j,k,no;

printf("Enter the number:\n");

scanf("%d",&no);

/*Run loops*/

for(i=1;i<=no;i++)

{

                /* Print spaces from second row, in incremented manner*/

for(j=1;j<i;j++)

{

printf(" ");

}

                /* Print stars in decremented manner*/

for(k=i;k<=no;k++)

{

printf("*");

}

                /* For next row*/

printf("\n");

}

}



Above program shows the following output:


C program to print inverted half left side of pyramid star pattern

C program to print half left side of the pyramid star pattern

 Ex: Write a C program to print half left side of the pyramid star pattern. How to write a C program to print half left side of the pyramid star pattern. C program to print half left side of the pyramid star pattern.


Input from user:

Enter the number:

5


Expected output:

        *

      **

    ***

  ****

*****



  Step by step logic of the given program:


1. Accept input number from user declare variable say no.


2. Run outer loop from 1 to no which shows the number of rows in pattern:

for(i=1;i<=no;i++)


3. Run first inner loop from i to no-1 to print spaces:

for(j=i;j<no;j++)


4. Run second inner loop from 1 to i to print star:

for(k=1;k<=i;k++)


5. For next row print \n.




Program:


#include<stdio.h>

void main()

{

int no,i,j,k;

printf("Enter the number:\n");

scanf("%d",&no);

/*Run loops*/

for(i=1;i<=no;i++)

{

for(j=i;j<no;j++)

{

    printf(" ");

}

for(k=1;k<=i;k++)

{

    printf("*");

}

/*For new row*/

        printf("\n");

}


}


Above program shows the following output:


C program to print half left side of the pyramid star pattern



C program to print half right side of pyramid star pattern

 Ex: Write a C program to print half right side of  the pyramid star pattern. How to write a C program to print half right side of the pyramid star pattern. C program to print half right side of the pyramid star pattern.



Input from user:

Enter the number:

5


Expected output:

*

* *

* * *

* * * *

* * * * *



  Step by step logic of the given program:


1. Accept input number from user declare variable say no.


2. Run outer for loop from 1 to no which shows rows in pattern:

for(i=1;i<=no;i++)


3. Run inner for loop from 1 to  i which shows columns in pattern:

for(j=1;j<=i;j++)


4. Inside the inner loop print star:

printf("*");


5. For next row print \n.





Program:


#include<stdio.h>

void main()

{

int no,i,j;

printf("Enter the number:\n");

scanf("%d",&no);

/*Run loops*/

for(i=1;i<=no;i++)

{

for(j=1;j<=i;j++)

{

/* print star*/

printf("*");

}

/*For new row*/

printf("\n");

}

}



Above program shows the following output:


C program to print half right side of pyramid star pattern





C program to print hollow square star pattern

 Ex: Write a C program to print hollow square star pattern. How to write a C program to print hollow square star pattern. C program to print hollow square star pattern.


Input from user:

Enter the number:

7


Expected output:

C program to print hollow square star pattern




  Step by step logic of the given program:

1. Accept input number from user declare variable say no.


2. Run outer loop from 1 to no which will shows rows of pattern:

for(i=1;i<=no;i++)


3. Run inner loop from 1 to no which will shows columns of pattern:

for(j=1;j<=no;j++)


4. Using if statement give conditions to print stars:

if(i==1||i==no||j==1||j==no)


5. Otherwise print spaces (to print it like hollow).


6. For next row use new line (\n).




Program:


#include<stdio.h>

void main()

{

int no,i,j;

printf("Enter the number:\n");

scanf("%d",&no);

/*Run loops from 1 to no*/

for(i=1;i<=no;i++)

{

for(j=1;j<=no;j++)

{

/*Give conditions to print stars*/

                if(i==1||i==no||j==1||j==no)

{

printf("*");

}

else

{

/*Otherwise print spaces*/

                             printf(" ");

}

}

/*For new row*/

        printf("\n");

}

}



Above program shows the following output:


C program to print hollow square star pattern


C program to print Rhombus star pattern

Ex: Write a C program to print Rhombus star pattern. how to print Rhombus star pattern using for loop. C Program to print Rhombus star Pattern.
In below code we  print Rhombus star pattern.



Input from user:

Enter the number: 5

Expected output:

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




  Step by step logic to print Rhombus star pattern.

1. Accept input (number) from user declare variable say no.


2. Run a loop(outer loop), to iterate through rows and increment i by 1:

for(i=1; i<=no; i++)


3. After that to print spaces run another loop(inner loop):

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


4. To print stars run another loop(inner loop) from 1 to no. Print star inside this loop:

for(k=1; k<=no; k++)

 {

printf("*");

 }


5. After printing all columns of a row, print new line (\n), for move it to next line.





Program:

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

#include<stdio.h>
void main()
{
int i,j,k,no;

printf("Enter the number:\n");/*take input number from user which denotes number of rows*/
scanf("%d",&no);

for(i=1;i<=no;i++)
{
for(j=1;j<=no-i; j++)
{
printf(" ");
}
for(k=1;k<=no;k++)
    {
      printf("*");
    }
    printf("\n");//for new line
}
}

Above program shows the following output:

C program to print Rhombus star pattern

C program to print mirrored rhombus star pattern

Ex: Write a C program to print mirrored rhombus star pattern. How to write a C program to print mirrored rhombus star pattern. C program to print mirrored rhombus star pattern.


Input from user:

Enter the number:

5


Expected output:

*****

 *****

  *****

   *****

    *****



  Step by step logic of the given program:


1. Accept input (number) from user declare variable say no.


2. Run a loop(outer loop), to iterate through rows and increment i by 1:

for(i=1; i<=no; i++)


3. After that to print spaces run another loop(inner loop):

for(j=1; j<i; j++)

{

printf(" ");

}


4. To print stars run another loop(inner loop) from 1 to no. Print star inside this loop:

for(k=1; k<=no; k++)

 {

printf("*");

 }


5. After printing all columns of a row, print new line (\n), for move it to next line.





Program:


#include<stdio.h>

void main()

{


 int i,j,k,no;

   /*Take input number from user which denotes number of rows and columns in pattern*/

   printf("Enter the number:\n");

   scanf("%d",&no);

   for(i=1; i<=no; i++)

   {

     /*Print spaces*/

     for(j=1; j<i; j++)

{

printf(" ");

}

     for(k=1; k<=no; k++)

{

printf("*");

}

     printf("\n");//for new line

 }

}


Above program shows the following output:


C program to print mirrored rhombus star pattern


C program to concatenate strings using pointers

 Ex: Write a C program to concatenate strings using pointers. How to write a C program to concatenate strings using pointers. C program to concatenate strings using pointers.



Input from user:

Enter first string:

codefor

Enter second string:

hunger


Expected output:

Concatenated string:

codeforhunger




  Step by step logic of the given program:


1. Accept two strings from user declare variables say str1 and str2.


2. Run while loop till end of first string:

while(*s1)

     {

  s1++;

     }


3. Run while loop till end of second string and inside the while loop copy second string to first string:

while(*s2)

        {

*s1=*s2;

s1++;

s2++;

}


4. After that end first string with NULL(\0).


5. Last, print concatenated string str1.




  C program to concatenate two strings using pointers:


#include<stdio.h>

#define max 100//declare max size 100

int main()

{

char str1[max],str2[max];

        /*declare pointers for str1 and str2*/

char *s1=str1,*s2=str2;

printf("Enter first string:\n");

gets(str1);

printf("Enter second string:\n");

gets(str2);

while(*s1)/*till end of the first string*/

{

s1++;/*point to the next character*/

}

while(*s2)/*till end of the second string*/

{

*s1=*s2;

s1++;

s2++;

}

/*Make sure that str1 end with NULL*/

    *s1='\0';

    printf("Concatenated string is:\n%s",str1);

return 0;

}



Above program shows the following output:


C program to concatenate strings using pointers