# pattern problem in c



## ambika (Dec 12, 2008)

I want only program logic for this pattern ....."a c letter pyramid in which first row have one c letter ,second row have three ,third row have 5 and so on.......
                                                     please don't assign me a  several printf() and tab and newline character.....as an option......!!!! 
   It be made with two for loops that is in one loop  tab is decreasing and letter "c"  is increasing in the second loops.??......or any other best option guys.....!!!!!


----------



## dheeraj_kumar (Dec 12, 2008)

This is fairly simple, although newbies have a tough time with this. You can do it as long as you understand how the no. of letters are generated.

for row 1, 1 letter
for row 2, 3 letters
for row 3, 5 letters
for row 4, 7 letters

you can identify a simple pattern. For row number 'n', it will contain '(2*n) - 1' letters.

```
for( i = 1; i <= n; i++ )
{
    printf( "\n" );
    for( j = 1; j <= ( (2*n) - 1 ); j++)
        printf( "c" );
}
```


----------



## ambika (Dec 13, 2008)

This is not the case .........this triangle is equilateral  triangle.....not a right angle triangle ......!!!!

I am new to this forum can anyone assign me ....how can i use drawing tools in this forum......my pattern not appearing right.


----------



## vamsi360 (Dec 13, 2008)

then use nested for loops one for dividing the screen into two and starting from the middle and the other to print the letter

here it goes......very difficult to type here

main()
{
int i,j,n;
printf("Enter the number of lines");
scanf("%d",&n);
for(i=1,i<=n;i++)
{
    for(j=1;j<=n-i;j++)
    {
         printf("    ");
    }
    for(j=1;j<=i;j++)
    {
         print("%4c",c);
    }
    printf("\n");
}
printf("\n");
}


----------



## ambika (Dec 13, 2008)

Hey ,vamsi360.... u r rignt .....i am just wanted this answer from someone ...please tell me how can this be done.


----------



## vamsi360 (Dec 13, 2008)

starting from the first.....in the second printf the gap between the quotes is 4 spaces and hence %4d in the 3rd printf.In the thread the spaces are gone

first as I said divide the screen to half so that you can start from the middle.Hence characters get displayed on both left and right.Rest you can easily understand if you get the main logic


----------



## ambika (Dec 13, 2008)

can anyone provide me a online c compiler....


----------



## QwertyManiac (Dec 13, 2008)

Why don't you figure out a way to print spaces if this following one is what you want:


```
*
  ***
 *****
  ***
   *
```

In C:

```
#include <stdio.h>

int main(int argc, char **argv) {
    int n = 5, i, j;
    printf("Enter no. of rows: ");
    scanf("%d", &n);
    for(i=1; i<n; i++) {
        for(j=0; j<n-i+1; j++) { printf(" "); }
        for(j=0; j<(2*i)-1; j++) { printf("c"); }
    printf("\n");
    }
    for(i=n; i>0; i--) {
        for(j=n-i+1; j>0; j--) { printf(" "); }
        for(j=(2*i)-1; j>0; j--) { printf("c"); }
    printf("\n");
    }
    return 0;
}
```

There are no online C "compilers", but a useful, limited interpreter might be found here.

Output of vamsi360's program:

```
c
      c   c
     c   c   c
    c   c   c   c
   c   c   c   c   c
```


----------



## vamsi360 (Dec 13, 2008)

hey qwerty i have said to adjust the spaces in quotes as I typed here and not pasted it from editor.Look at it with 4 spaces in quotes and %4c you will get the desired output


----------



## QwertyManiac (Dec 13, 2008)

I posted the output only cause I guessed the OP was looking to run it.


----------



## vamsi360 (Dec 13, 2008)

ok........output is correct(I compiled in 'ancient tc' )


----------



## ambika (Dec 13, 2008)

Qwerty!!! ur code works fine ........i got my answer thanks.


----------



## QwertyManiac (Dec 13, 2008)

Use [noparse]
	
	



```

```
[/noparse] tags to post any format as accurately as possible. (The problem you said earlier, about making the forum post accept your spaces.)


----------



## vamsi360 (Dec 14, 2008)

QwertyManiac said:


> Use [noparse]
> 
> 
> 
> ...


 
I have been using the quick reply board most fo the times and haven't seen that. Thankyou for that. For you it shows PHP code. FIrst I thought all the answers you posted in remaining threads are in PHP. I think there is a confusion there. Use code only.


----------



## QwertyManiac (Dec 14, 2008)

Heh, I use it for syntax highlighting. They haven't installed any other language plug-ins.


----------

