I need to get an output like:
1 1
11 11
111 111
1111111
I tried a lot and came to the following program. But i'm getting output like:
1 1
11 11
111 111
1111111
Please help me correct this program. The preview of the shape was not coming properly so I attached a .txt file to the thread. Please refer to it for proper shape I need. Thanks!!

#include<stdio.h>
#include<conio.h>
void main()
{
     int i,j,k;
     for(i=1;i<=4;i++)
     {
                      if(i==4)
                      {
                              for(k=0;k<7;k++)
                              {
                                              printf("1");
                              }break;
                      }
                for(j=0;j<i;j++)
                {
                                printf("1");
                }
                for(k=3;k>=i;k--)
                {
                               printf("  ");                     
                }     
                for(j=0;j<i;j++)
                {
                                printf("1");
                }
                printf("\n");
     }
     getch();
}

Recommended Answers

All 10 Replies

change k to 2.

int width = 7;

Then replace your blank printing loop with:

for(k=width;k>(i*2);k--)
                {
                               printf(" ");                     
                }

Note that the loop above prints just ONE space char, at a time, instead of two space chars, as the current loop you have, does.

@fpl25, changing k to 2 doesn't help.
@Adak, yeah thanks it worked. If I put just one space in my code instead of two then also i get some other type of error. Why is that?

That code works fine. Did you paste it in, or just type it?

Did you get the change from <= to just < in the for loop?

Any other problem, you'll have to post your whole updated program, because I tested the above, and it works with your previous code.

Are you getting an error about:
"function should return a value in main()" ?

void main() is not permitted in standard C. Only int main() with a return 0 (if normal), at the end of the function.

No. I pasted it. If I put, by removing '=' :

for(k=3;k>i;k--)
{
printf(" ");
}

then also o\p is different.

That's not the code I posted for you. If you want to use code that doesn't work, then I can't help you.

No error like that.
My updated code:

#include<stdio.h>
#include<conio.h>
void main()
{
     int i,j,k;
     int width=7;
     for(i=1;i<=4;i++)
     {
                      if(i==4)
                      {
                              for(k=0;k<7;k++)
                              {
                                              printf("1");
                              }break;
                      }
                for(j=0;j<i;j++)
                {
                                printf("1");
                }
                   
                   for(k=3;k>i;k--)
                   {
                                   printf(" ");
                   } 
                for(j=0;j<i;j++)
                {
                                printf("1");
                }
                printf("\n");
     }
     getch();
}

What is your question or problem with this code?

Hey Your Code Works Just Fine.. Thanks for that. We misunderstood, I was speaking about modification of my code.

You're welcome. Carry on! ;)

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.