Step by step logic of the given program:
- Print left peak:
- Print right peak:
In this tutorial you will learn star pattern programs with their solutions. You can practice it to enhance your programming logic. I hope this exercises will help you to understand working of looping statements, if-else statements and more.
Quick links:
1. Write a C program to print square star pattern.
2. Write a C program to print rhombus star pattern.
3. Write a C program to print mirrored rhombus star pattern.
4. Write a C program to print hollow square star pattern.
5. Write a C program to print pyramid star pattern.
6. Write a C program to print inverted pyramid star pattern.
7. Write a C program to print half left side of pyramid star pattern.
8. Write a C program to print half right side of pyramid star pattern.
9. Write a C program to print inverted half left side of pyramid star pattern.
10. Write a C program to print inverted half right side of pyramid star pattern.
11. Write a C program to print hollow diamond star pattern.
12. Write a C program to print swastika star pattern.
13. Write a C program to print heart shape star pattern.
Keep visiting, Keep learning.....🙃
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:
*****
****
***
**
*
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:
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:
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:
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:
*
* *
* * *
* * * *
* * * * *
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:
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:
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:
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.
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:
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
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.
#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: