this program i made.. should take an input 'no' from the user.. and print the following pattern..

0
0 n+1
0 n+1 n+2
0 n+1 n+2 n+3
0 n+1 n+2 n+3 n+4 ....... n
0 n+1 n+2 n+3 n+4.. n-1
0 n+1 n+2 n+3 n+4
0 n+1 n+2 n+3
0 n+1 n+2
0 n+1
0

lyk if i enter 2 it shuold print:

0
0 1
0 1 2
0 1

i made this program.. it gives the correct output .. but its caught in some infinite loop which is causing the problem and i cant seem to figure out WHERE exactly is the problem.. i made this program considering the number of lines that would be taken by output.. lyk if we considered the first line as line 0, the last line would always be 2*no.. and divided thsi program into two parts..first part is till the mid line where the numbers are printed til the 'no' entered..and thesecond part is the line after the mid line..which is always line=no+1..heres the code..please check it and corect it ASAP..

#include<stdio.h>
#include<conio.h>

int main()
{
    int no,line, i,j,k,m,end_of_part2,l,no_of_numbers;

    printf("Enter number to get pattern up to that number..");
    scanf("%d", &no);



    for(i=0;i<=no;i++)
    {
      for(j=0;j<=i;j++)
      {
        printf("%d ", j);

      }
      printf("\n");
      }

      line=no+1;
      end_of_part2=(2*no);


      k=line;

      while(k<=end_of_part2)

      {
        for(l=no;l>=1;l--)
        {

          no_of_numbers=l-1;                
          for(m=0;m<=no_of_numbers;m++)
          {
            printf("%d ", m);
          }
            printf("\n");

        }
        k=2*no;
      }

      getch();
      return 0;

}

Recommended Answers

All 2 Replies

Reposting the code so I can see it better.

#include<stdio.h>
#include<conio.h>

int main() {
    int no, line, i, j, k, m, end_of_part2, l, no_of_numbers;

    printf ("Enter number to get pattern up to that number..");
    scanf ("%d", &no);

    for (i = 0;i <= no;i++) {
        for (j = 0;j <= i;j++) {
            printf ("%d ", j);
        }
        printf ("\n");
    }

    line = no + 1;
    end_of_part2 = (2 * no);


    k = line;

//    while (k <= end_of_part2) {
    for (l = no;l >= 1;l--) {
        no_of_numbers = l - 1;
        for (m = 0;m <= no_of_numbers;m++) {
            printf ("%d ", m);
        }
        printf ("\n");
    }
//        k = 2 * no;
//    }

    getch();
    return 0;
}

I didn't understand the use of while loop on line 23, so i commented it out.. it works!

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.