Skip to content

Instantly share code, notes, and snippets.

@suryansh011
Last active May 31, 2021 16:03
Show Gist options
  • Save suryansh011/a03eebc1aff1ef9039025904f435d1b0 to your computer and use it in GitHub Desktop.
Save suryansh011/a03eebc1aff1ef9039025904f435d1b0 to your computer and use it in GitHub Desktop.
Pattern in C
/*
* 1 2 3 4 5 6 7 8 9 <------ x (i)
* 1 * * * * * * * * *
* 2 * * * * * * * *
* 3 * * * * * *
* 4 * * * *
* 5 * *
* ^
* |
* |
* y
* (j)
*/
#include<stdio.h>
int main() {
int x,y;
printf("Enter a positive odd whole number: ");
scanf("%d",&x);
if (x%2 == 0 || x<0) {
printf("This program takes only positive odd whole numbers!\n");
}
else {
y=(x+1)/2;
int i,j;
for(j=1;j<=y;j++)
{
for(i=1;i<=x;i++)
{
if(i<=(y+1-j) || i>=(y+j-1))
{
printf("*");
}
else {
printf(" ");
}
}
printf("\n");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment