DIGIT-ARC read-only archive · snapshot: July 23, 2026

pattern problem in c

15 posts · originally from https://geek.digit.in/community/threads/pattern-problem-in-c.104246/

A
ambika Original post #1
Dec 12, 2008 at 5:11 PM
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 at 5:56 PM
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.
Code:
for( i = 1; i <= n; i++ )
{
    printf( "\n" );
    for( j = 1; j <= ( (2*n) - 1 ); j++)
        printf( "c" );
}
A
ambika #3
Dec 13, 2008 at 7:21 PM
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 at 7:54 PM
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");
}
A
ambika #5
Dec 13, 2008 at 8:05 PM
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 at 8:06 PM
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
A
ambika #7
Dec 13, 2008 at 8:19 PM
can anyone provide me a online c compiler....
QwertyManiac
Dec 13, 2008 at 8:24 PM
Why don't you figure out a way to print spaces if this following one is what you want:

Code:
   *
  ***
 *****
  ***
   *

In C:
PHP:
#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:
Code:
       c
      c   c
     c   c   c
    c   c   c   c
   c   c   c   c   c
vamsi360
Dec 13, 2008 at 8:31 PM
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 at 8:35 PM
I posted the output only cause I guessed the OP was looking to run it. :)
vamsi360
vamsi360 #11
Dec 13, 2008 at 8:42 PM
ok........output is correct(I compiled in 'ancient tc' )
A
ambika #12
Dec 13, 2008 at 9:06 PM
Qwerty!!! ur code works fine ........i got my answer thanks.
QwertyManiac
Dec 13, 2008 at 9:14 PM
Use [noparse]
Code:
[/noparse] tags to post any format as accurately as possible. (The problem you said earlier, about making the forum post accept your spaces.)
vamsi360
vamsi360 #14
Dec 14, 2008 at 10:38 AM
Use [noparse]
Code:
[/noparse] tags to post any format as accurately as possible. (The problem you said earlier, about making the forum post accept your spaces.)

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 at 11:04 AM
Heh, I use it for syntax highlighting. They haven't installed any other language plug-ins.