//1>program for piramid.
---------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,a,k=0,m;
clrscr();
scanf("%d",&n);
m=n*2;
a=m/2;
for(i=0;i<m;i++){
printf("\n");
if(i<a)k++;
else   k--;
for(j=0;j<m;j++){
if(i>=a||j<a-(k-1)||j>a+(k-1))
printf("  ");
else
printf(" *");
}
}
getch();
}

//2>program for Revrse piramid.
---------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,a,k=0,m;
clrscr();
scanf("%d",&n);
m=n*2;
a=m/2;
for(i=0;i<m;i++){
printf("\n");
if(i<a)k++;
else   k--;
for(j=0;j<m;j++){
if(i<a||j<a-(k-1)||j>a+(k-1))
printf("  ");
else
printf(" *");
}
}
getch();
}

//3>program for combind both piramid and Reverse Piramid.
---------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,a,k=0,m;
clrscr();
scanf("%d",&n);
m=n*2;
a=m/2;
for(i=0;i<m;i++){
printf("\n");
if(i<a)k++;
else   k--;
for(j=0;j<m;j++){
if(j<a-(k-1)||j>a+(k-1))
printf("  ");
else
printf(" *");
}
}
getch();
}

output........... 
for progaram 1
--------------------
   *
  ***
 *****
*******
------------------------
for progaram 2
--------------------
*******
 *****
  ***
   *
------------------------
for progaram 3
--------------------
   *
  ***
 *****
*******
 *****
  ***
   *

------------------------

Dani AI

Generated

This thread shows three simple patterns from (pyramid, reverse pyramid, combined). Useful practical improvements follow: make the code portable, validate input, and prefer clearer math and names so the output and intent are obvious.

Portability and style: avoid Turbo C/Borland-specific headers and functions such as conio.h, clrscr() and getch(); they are nonstandard and will not compile with modern toolchains. Use int main(void) with return 0;, check scanf's return value, and give variables descriptive names (for example row, spaces, stars) as already suggested by . Also cap n to a sensible value to prevent very wide lines that wrap on the terminal.

A compact, portable approach uses the direct formula that a pyramid row r has 2*r-1 stars and n-r leading spaces. Example implementation:

#include <stdio.h>

int main(void) {
    int n;
    if (scanf("%d", &n) != 1 || n <= 0) return 1;
    if (n > 80) n = 80; /* safety cap for typical terminals */

    for (int row = 1; row <= n; ++row) {
        for (int s = 0; s < n - row; ++s) putchar(' ');
        for (int s = 0; s < 2*row - 1; ++s) putchar('*');
        putchar('\n');
    }

    for (int row = n - 1; row >= 1; --row) {
        for (int s = 0; s < n - row; ++s) putchar(' ');
        for (int s = 0; s < 2*row - 1; ++s) putchar('*');
        putchar('\n');
    }

    return 0;
}

Troubleshooting notes: if the shape looks off, confirm the terminal uses a monospace font and that only single space characters are printed (mixing " " and " *" can misalign columns). Using the 2*row-1 formula removes the need for an accumulating k variable and makes the logic easier to verify.

Welcome to the forum, Dharma. :)

Two suggestions for your study in C:

1) for technical reasons, C should always have an int return from main. The correct format for main in these programs would be:

int main(void)

with

return 0; at the end of main.

I know void is taught sometimes, and shown in books, but that is wrong, and support for it may not continue in the future.

2) variable names like i,j,k,a,n, etc., are all good, but in this program, and in your programs in the future, it's a huge help to use descriptive variable names like row, rowNum, column or columnNum, etc. We all "get" and use i and j quite commonly, but the rest of those names - well, they could be improved.

Right now, I'm re-writing a program I wrote some years back (expanding it and upgrading it to 64 bit format), and it's a BIG help to have descriptive names for most of the variables where the purpose of it is not easily remembered years later.

commented: That was great suggestion.....Ill try to follow these from now on... +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.