12.23.2011

Number pattern

Q. Write a C program to display the following pattern:
 1
 2   3
 4   5    6
 7   8    9    10
11  12   13    14  15

Ans.

/*program to design the above pattern*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int r,c,n=1;
 clrscr();
 for(r=1; r<=5; r++)
 {
  for(c=1; c<=r; c++)
  {
    printf("%3d",n++);
  }
  printf("\n");
 }
 getch();
}

Output:-

 1
 2   3
 4   5    6
 7   8    9    10
11  12   13    14  15

3 comments:

  1. this program is error, some change are required;

    #include

    main()
    {

    int r,c, n=1;

    for(r=1; r<=5; r++)
    {
    for(c=1; c<=r; c++)
    {
    printf("%3d",n);
    n++;
    }
    printf("\n");
    }

    getchar();
    }

    ReplyDelete
  2. last code ma error che
    getch();

    ReplyDelete
  3. Error has been fix.
    Thanks Kesav and Raj chovatiya for improving C programming.

    ReplyDelete