hello,
Can any one tell me how to print the patter

*
***
 *
for n =1
 **
****
****
 **
for n = 2
 ***
*****
*****
*****
 ***
for n = 3

the code i am having is

for(i=0;i<=n;i++)
{
   for(j=1;j<n-i;j++)
     putchar(' ');
   for(k=0;k<=i;k++)
    printf('* ');
  putchar('\n');
}

what will be the condition so that i will get that pattern
thanking you

Dani AI

Generated

The pattern is three parts: a single indented line of n asterisks, then n lines of n+2 asterisks, and finally the same indented line again. That is the concise outline gave; 's questions point to the exact C mapping (print a char, repeat a char, loop). The simplest, clear approach is a small helper that prints a given number of spaces and stars, then call it three times (top, middle repeated n times, bottom).

#include <stdio.h>

static void printline(int spaces, int stars)
{
    int i;
    for (i = 0; i < spaces; ++i) putchar(' ');
    for (i = 0; i < stars; ++i) putchar('*');
    putchar('\n');
}

int main(void)
{
    int n, i;
    if (scanf("%d", &n) != 1) return 1;
    if (n <= 0) return 0;

    printline(1, n);           /* top: one leading space, n stars */
    for (i = 0; i < n; ++i)    /* middle: n lines of n+2 stars */
        printline(0, n + 2);
    printline(1, n);           /* bottom */

    return 0;
}

Notes: use printline(1, n) if the top/bottom need exactly one leading space; change the first parameter to shift alignment. Avoid printing an extra space after each * (that doubles the visual width). The example above uses C89-style declarations so it compiles with older compilers; if using C99+ the function-local loop counters may be declared inline. This follows the structure discussed by and answers the mapping prompts from while fixing the off-by-one and spacing issues present in the original attempt by .

Recommended Answers

All 5 Replies

You're over-engineering the solution. It's really nothing more than this:

print ' '
print n '*'

for i = 0 to n do
  print n + 2 '*'
loop

print ' '
print n '*'

You're over-engineering the solution. It's really nothing more than this:

print ' '
print n '*'

for i = 0 to n do
  print n + 2 '*'
loop

print ' '
print n '*'

but how do i convert this code in to C code??

>but how do i convert this code in to C code??

print ' ' ---> How would you display a space in C?
print n '*' ---> How would you display a * as many times as n is equals to?

for i = 0 to n do
print n + 2 '*' ---> How would you display in C a * n times plus 2 more?
loop ---> How would you loop in C from 0 to n?

print ' ' ---> How would you display a space in C?
print n '*' ---> How would you display a * as many times as n is equals to?

>but how do i convert this code in to C code??

print ' ' ---> How would you display a space in C?
print n '*' ---> How would you display a * as many times as n is equals to?

for i = 0 to n do
print n + 2 '*' ---> How would you display in C a * n times plus 2 more?
loop ---> How would you loop in C from 0 to n?

print ' ' ---> How would you display a space in C?
print n '*' ---> How would you display a * as many times as n is equals to?

Okay I apologize .....

>but how do i convert this code in to C code??
There's a reason I didn't give you C code. ;)

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.