• CONTEST ALERT - Experience the power of DDR5 memory with Kingston Click for details

pattern problem in c

Status
Not open for further replies.

ambika

learnhardy
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.....!!!!!
 
Last edited:

dheeraj_kumar

Legen-wait for it-dary!
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" );
}
 
OP
A

ambika

learnhardy
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.


 
Last edited:

vamsi360

Always confused
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");
}
 
Last edited:
OP
A

ambika

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

vamsi360

Always confused
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
 
Last edited:

QwertyManiac

Commander in Chief
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

Always confused
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

Commander in Chief
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

Always confused
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.
 
Status
Not open for further replies.
Top Bottom