Subscribe

RSS Feed (xml)

Thursday, February 5, 2009

Write a C program to construct a pyramid of numbers.

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

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main( )
{
int r, c, num=1 ,len;
clrscr( );
printf("Enter the required no. of rows:");
scanf("%d", &len);
for( r=1; r< =len; r++ ) {
printf("\n");
for(c=1 ; c<=r; c++) {
printf("%2d", num);
num++; }
}
getch( );
return 0;
}
Explanation:-
Here 'r' indicates row,
'c' indicates column and 'len' indicates length of the pyramid.
And lastly 'num=1' for generating pyramid of numbers.
This 'c<=r' is required becoz when row is incremented, column value is also incremented.
Note:- If U have any doubts regarding this program, contact with me through my email-id.

114 comments:

  1. how to write a c program to generate pyramid in the form
    1
    2 3 2
    3 4 5 4 3

    ReplyDelete
    Replies
    1. #include
      int main()
      {
      int r,s,st,n,m=1;
      int nst=n;
      printf("Enter size");
      scanf("%d",&n);
      for(r=1;r<=n;r++)
      {
      for(s=1;s<=n-r;s++)
      {
      printf("");
      }
      for(st=1;st<=2*r-1;st++)
      {
      printf("%d",m++);
      printf(" ");
      }
      printf("\n");
      }
      }

      Delete
    2. mr.mujahid islam:
      your code in above has an error. My suggestion is to dry run it and rectify your error.
      its in the 3rd for loop....

      Delete
  2. ******
    *****
    ****
    ***
    **
    *
    what the logik is set on this type of question

    ReplyDelete
    Replies
    1. #include
      int main()
      {
      int r,s,st,n;
      int nst=n;
      printf("Enter size");
      scanf("%d",&n);
      for(r=n;r>0;r--)
      {
      for(s=n-r;s>0;s--)
      {
      printf("");
      }
      for(st=r;st>0;st--)
      {
      printf("*");
      printf(" ");
      }
      printf("\n");
      }
      }

      Delete
  3. i
    i i
    i i i
    i i i i
    what the logik is set on this type of question

    ReplyDelete
    Replies
    1. include
      include
      void main()
      {
      int m,n,p;
      printf("enter number");
      scanf("%d",&n);
      for(m=1;m<=n;m++);
      {
      for(P=1;p<=m;p++)
      print("i");
      printf("\n");
      }
      getch();
      }

      Delete
  4. what is the logic for this type?
    1
    2 2
    3 3 3
    4 4 4 4
    5 5 5 5 5

    ReplyDelete
    Replies
    1. #include
      int main()
      {
      int r,s,st,n;
      int nst=n;
      printf("Enter size");
      scanf("%d",&n);
      for(r=1;r<=n;r++)
      {
      for(s=1;s<=n-r;s++)
      {
      printf("");
      }
      for(st=1;st<=r;st++)
      {
      printf("%d",r);
      printf(" ");
      }
      printf("\n");
      }
      }

      Delete
    2. int main()
      {
      int n,i,j;
      printf("Enter size");
      scanf("%d",&n);
      for(i=1;i<=n;i++)
      {
      for(j=1;j<=i;j++)
      {
      printf("%d\t",i);
      }
      printf("\n");
      }
      }

      Delete
  5. 1
    1 1
    1 2 1
    1 3 3 1
    how to write a c program to get a pyramid like this????????

    ReplyDelete
    Replies
    1. #include
      long peramir(int);
      int main()
      {
      int n,i,c;
      printf("Enter peramie size");
      scanf("%d",&n);
      for(i=0;i<n;i++)
      {
      for(c=0;c<=(n-i-2);c++)
      printf("");

      for(c=0;c<=i;c++){
      printf("%ld",peramir(i)/(peramir(c)*peramir(i-c)));
      printf(" ");
      }
      printf("\n");
      }
      }
      long peramir(int n)
      {
      int c;
      long f=1;
      for(c=1;c<=n;c++)
      f=f*c;
      return f;
      }

      Delete
  6. how to write a c program for this
    1
    222
    33333
    4444444
    555555555

    ReplyDelete
    Replies
    1. #include
      int main()
      {
      int r,s,st,n,m=1;
      int nst=n;
      printf("Enter size");
      scanf("%d",&n);
      for(r=1;r<=n;r++)
      {
      for(s=1;s<=n-r;s++)
      {
      printf("");
      }
      for(st=1;st<=2*r-1;st++)
      {
      printf("%d",r);
      printf(" ");
      }
      printf("\n");
      }
      }

      Delete
    2. #include
      int main()
      {
      int i,j,n;
      printf("enter the no. of rows");
      scanf("%d",&n);
      for(i=1;i<=n;i++)
      {
      printf('\n");
      for(j=1;j<=((i^2)-((i-1)^2));j++)
      {
      printf("%d",i);
      }
      }
      }

      Delete
  7. how to write a program like
    ------1
    -----222
    ----33333
    ---4444444
    --555555555 -indicates space

    ReplyDelete
  8. I have to do a pyramid that depending on the number that the user gives, will display the lines of a pyramid that correspond:
    The pyramid has to look like this:
    1
    232
    34543
    4567654
    567898765
    67890109876
    7890123210987
    890123454321098
    90123456765432109
    0123456789876543210

    So if the user gives a number 3 the display will be:
    1
    323
    34543
    How do I do it?

    My e-mail is jlblue05@hotmail.com

    Please help me, thank YOU!

    ReplyDelete
    Replies
    1. #include

      main()
      {
      int n, c, d, num = 1, space;

      scanf("%d",&n);

      space = n - 1;

      for ( d = 1 ; d <= n ; d++ )
      {
      num = d;

      for ( c = 1 ; c <= space ; c++ )
      printf(" ");

      space--;

      for ( c = 1 ; c <= d ; c++ )
      {
      printf("%d", num);
      num++;
      }
      num--;
      num--;
      for ( c = 1 ; c < d ; c++)
      {
      printf("%d", num);
      num--;
      }
      printf("\n");

      }

      return 0;
      }

      Delete
  9. 1
    121
    12321
    1234321


    how will i get this

    ReplyDelete
  10. How to get this in generic format:
    ------*
    -----*-*
    ----*-*-*
    ---*-*-*-*
    --*-*-*-*-*
    -*-*-*-*-*-*
    *-*-*-*-*-*-*
    ignore the "-"

    ReplyDelete
    Replies
    1. int a = 7;
      for (int ii = 1; ii <= 7; ii++)
      {

      for (int j = 1; j <= ii; j++)
      {

      for (int k = 1; k < a; k++)
      {
      Console.Write(" ");
      if (j > 1)
      {
      break;
      }
      }
      Console.Write("*");
      if (ii == 7)
      {
      Console.Write(" ");
      }
      }
      Console.WriteLine();
      a--;
      }
      Console.ReadLine();

      Delete
    2. hey thnx yaar i also have same doubt ..thnx for helping

      Delete
    3. #include
      int main()
      {
      int i,j,k,n;
      scanf("%d",&n);
      for(i=1;i<=n;i++)
      {
      for(j=n;j>=i;j--)
      {
      printf(" ");

      }
      for(k=1;k<=i;k++)
      printf("* ");
      printf("\n");
      }
      return 0;
      }

      Delete
  11. how to write programm
    ABCDEFGFEDCBA
    ABCDE EDCBA
    ABCD DCBA
    ABC CBA
    AB BA
    A A

    ReplyDelete
    Replies
    1. write a code for this program

      Delete
  12. how to generate the following "pyramid" of digits,using nested loops in C++.....i really need your help...........
    1
    232
    34545
    4567654
    567898765
    67890109876
    7890123210987
    890123454321098
    90123456765432109
    0123456789876543210

    ReplyDelete
  13. oh i forgot...." DO NOT WRITE OUT 10 MULTI-DIGIT STRINGS. INSTEAD,DEVELOP A FORMULA TO GENERATE THE APPROPRIATE OUTPUT FOR EACH LINE. "


    THANKS FOR THE HELP...........

    ReplyDelete
  14. how to generate the following output in C.
    *
    * *
    * * *
    * * * *
    * * * * *
    * * * *
    * * *
    * *
    *

    ReplyDelete
    Replies
    1. #include
      using namespace std;
      int main()
      {
      int i,j,n,m;
      cout<<"Enter pyramid size\n";
      cin>>n;
      for(m=0;m<2;m++)
      {
      if(m==0){
      for(i=1;i<=n;i++)
      {
      for(j=1;j<=n-i;j++)
      cout<<"";

      for(j=1;j<=i;j++)
      cout<<"*";
      cout<<"\n";
      }
      }
      if(m==1){
      for(i=n;i>0;i--)
      {
      for(j=n-i;j>0;j--)
      cout<<"";
      for(j=i;j>0;j--)
      cout<<"*";
      cout<<"\n";
      }
      }

      }
      }

      Delete
  15. how wil get this in centre instead of linear aligned in c language
    1
    121
    12321
    1234321


    instead in

    1
    121
    12321
    1234321
    123454321

    ReplyDelete
    Replies
    1. #include
      int main()
      {
      int i,j,k,n;
      scanf("%d",&n);
      for(i=1;i<=n;i++)
      {
      for(j=1;j<=i;j++)
      {
      printf("%d",j);

      }
      for(k=i-1;k>=1;k--)
      printf("%d",k);
      printf("\n");
      }
      return 0;
      }

      Delete
  16. 1
    01
    101
    0101
    10101
    how to write program

    ReplyDelete
    Replies
    1. @autoreleasepool {

      // insert code here...
      // NSLog(@"Hello, World!");
      int i,j,n,num;
      NSLog(@"enter the number:=");
      scanf("%d",&n);
      for (i=1;i<=n;i++)
      {
      printf("\n" );
      for(j=1;j<=i;j++)
      {

      if ((i%2)==0)
      {
      num=(j%2==0)?1:0;
      printf("%d",num);
      /*if ((j%2)==0)
      {
      num=1;
      printf("%d",num);
      }

      else
      {
      num=0;
      printf("%d",num);
      }*/
      }

      else
      {
      num=(j%2==0)?0:1;
      printf("%d",num);

      /*if((j%2)==1)
      {

      num=1;
      printf("%d",num);
      }
      else
      {
      num=0;
      printf("%d",num);
      }*/
      }
      }
      }


      }
      return 0;
      }
      //
      //1
      ///01
      //101
      //0101
      //10101

      Delete
    2. using System;
      class BinTri
      {
      public static void Main()
      {
      int i,j,c;
      c=0;
      for(i=1;i<=5;i++)
      {
      c=i;
      for(j=1;j<=i;j++)
      {
      if((c%2)==0)
      {
      Console.Write(0+" ");
      }
      else
      {
      Console.Write(1+" ");
      }
      c++;
      }
      Console.WriteLine();
      }

      }
      }

      Delete
  17. how to write the program for
    1
    2 3
    4 5 6
    7 8 9 10

    ReplyDelete
    Replies
    1. #include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
      #include(conio.h)
      int main( )
      {
      int r, c, num=1 ,len;
      clrscr( );
      printf("Enter the required no. of rows:");
      scanf("%d", &len);
      for( r=1; r< =len; r++ ) {
      printf("\n");
      for(c=1 ; c<=r; c++) {
      printf("%2d", num);
      num++; }
      }
      getch( );
      }

      Delete
  18. This comment has been removed by the author.

    ReplyDelete
  19. A
    B*B
    C * C
    D*****D
    C * C
    B*B
    A
    HOW TO WRITE THIS PROGRAM???

    ReplyDelete
    Replies
    1. hi darling plz visit www.dobariya.com

      Delete
  20. How to print this?
    123454321
    1234 4321
    123 321
    12 21
    1 1

    ReplyDelete
  21. Replies
    1. please display the solution of this program

      Delete
    2. #include
      #include
      void main()
      {
      int i,j;
      clrscr();
      for(i=0;i<6;i++)
      {
      printf("\n\n");
      for(j=1;j<=i;j++)
      printf("%d",j);
      }
      getch();
      }



      Delete
  22. what is solution of this program ?
    a
    2 1
    b c a
    4 3 2 1

    ReplyDelete
  23. what is the solution for

    123454321
    2345432
    34543
    454
    5

    ReplyDelete
  24. *******
    *****
    ***
    *
    how to get this format in C/C++ please give specific code

    ReplyDelete
    Replies
    1. #include
      #include
      void main()
      {
      int i,j,n=4;
      clrscr();
      for(i=1;i<=n;i++)
      {
      for(j=7;j>=(2*i-1);j--)
      {
      printf("*");
      }
      printf("\n");
      }
      getch();
      }

      Delete
  25. 1
    2 2
    3 3 3
    4 4 4 4
    5 5 5 5 5 logic is
    void main()
    {int n=1,num;
    clrscr();
    printf("enter a number of row u want a peramid\n ");
    scanf("%d",&num);
    for(int i=1;i<=num;i++)
    {for(int j=1;j<=i;j++)
    { printf("%d",n);
    }

    printf("\n");n++;
    }
    getch();
    }
    contact me is any other query on madke.kiran@gmail.com

    ReplyDelete
  26. program for a pyramid has to look like this:
    1
    232
    34543
    4567654
    567898765
    67890109876
    7890123210987
    890123454321098
    90123456765432109

    ans:

    main()
    {
    int r,c,num=1,len,i;

    printf("enter length");
    scanf("%d",&len);

    for(r=1;r<=len;r++)
    {
    printf("\n");

    for(c=1;c<=r;c++)
    {
    printf("%d",num);
    num++;
    }
    num--;

    i=r-1;
    for(c=1;c<=i;c++)
    {
    num--;
    printf("%d",num);
    }
    num++;
    }
    getch();
    }

    ReplyDelete
  27. 1=1
    1+2=3
    1+2+3=6
    1+2+3+4=10
    HOW TO WRITE THIS PROGRAM?

    ReplyDelete
  28. Gausiya said
    ------------1
    ---------2-----2
    -------3----3-----3
    ----4----4----4-----4
    Ans:
    In java:
    class Ght
    {
    public static void main(String args[])
    {
    for(int i=1;i<5;i++)
    {
    for(int j=i;j<5;j++)
    {
    System.out.print(" ");
    }
    for(int j=1;j<=i;j++)
    {
    System.out.print(" ");
    System.out.print(i);
    }
    System.out.println(" ");
    }

    }
    }

    ReplyDelete
  29. how to print this

    _______________ &
    _____________ &___&
    ___________ &_______&
    __________ & & & & & & & &

    ReplyDelete
  30. how to write this program in C?
    ************
    ************
    ************
    ************
    ************
    ************
    ************
    ************
    ************
    ************
    ************
    ************

    ReplyDelete
    Replies
    1. #include
      #include
      void main()
      {
      int i,j,n=12;
      clrscr();
      for(i=1;i<=n;i++)
      {
      for(j=1;j<=n;j++)
      {
      printf("*");
      }
      printf("\n");
      }
      getch();
      }

      Delete
  31. how to write this in C?
    [0 space]************
    [1 space]************
    [2 space]************
    [3 space]************
    [4 space]************
    [5 spaces]************
    [5 spaces]************
    [4 space]************
    [3 space]************
    [2 space]************
    [1 space]************
    [0 space]************

    While putting a comment, it was removing the space before the pattern so i have to write it down.

    ReplyDelete
  32. How to write code for this

    54321
    4321
    321
    21
    1

    ReplyDelete
  33. write code for
    1
    2 3
    4 5
    please help

    ReplyDelete
  34. This comment has been removed by the author.

    ReplyDelete
  35. how to get the following pyramid?

    1
    2 1 2
    3 2 1 2 3
    4 3 2 1 2 3 4

    ReplyDelete
  36. please do reply fast ............if anyone knows

    ReplyDelete
  37. what is the program for

    1
    2 2
    3 3
    4 4
    3 3
    2 2
    1

    ReplyDelete
  38. what is the program for
    _________1
    ________2_2
    _______3_ _3
    ______4_ _ _4
    _______3_ _3
    ________2_2
    _________1

    ReplyDelete
  39. what is the program for
    A
    AB
    ABC
    ABCD
    ABCDE
    by using loop. iam in 11th just a beginner..!

    ReplyDelete
    Replies
    1. #include
      main()
      {
      int n,i,j,k=1,t=0;
      char a[10]={' ','A','B','C','D','E','F','\n'};
      printf("Enter\n");
      scanf("%d",&n);
      for(i=1;i<=n;i++)
      {

      for(k=1;k<=i;k++)
      {
      printf("%c\t",a[k]);

      }

      printf("\n");
      }
      }

      Delete
  40. Write a program to print thre following output:
    *
    * *
    * * *
    * * * *

    ReplyDelete
    Replies
    1. #include
      #include
      void main()
      {
      int i,j,num;
      clrscr();
      printf("\nenter the number of rows:");
      scanf("%d",&num);
      for(i=1;i<=num;i++)
      {
      for(j=1;j<=i;j++)
      {
      printf("*");
      }
      printf("\n");
      }
      getch();
      }

      Delete
  41. _______________*
    _____________*-*
    ___________*_*_*
    _________*_*_*_*
    above output to print

    ReplyDelete
  42. thanks for this!!

    ReplyDelete
  43. 3) Write a program to print following pyramid:
    A
    ABA
    ABCBA
    ABCDCBA
    ABCBA
    ABA
    A

    ReplyDelete
  44. write a program to print the fallowing pyramid


    2

    2 4

    2 4 16

    2 4 16 256

    ReplyDelete
  45. write a program to find the palindroms in a file and print them if the file consisting of tab as a delimiter

    ReplyDelete
  46. write a c program for
    *****
    ****
    ***
    **
    *

    ReplyDelete
  47. i want a code for this program
    1 2 3 4
    16 15 14 5
    11 12 13 6
    10 9 8 7

    ReplyDelete
  48. WAP to generate the following.
    0
    0 1 0
    0 1 2 1 0
    0 1 2 3 2 1 0

    ReplyDelete
  49. how to write the program form print this..
    *
    **
    ***
    ****
    *****

    ReplyDelete
  50. pascal's triangle.
    #include
    #include
    void main()
    {
    int i,j,A[10][10];
    clrscr();
    printf("ENTER THE NUMBER OF ROWS\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
    {
    if(y=((y==0)!!(i==y)))
    {
    A[i][j];
    }
    }
    }
    else
    A[i][j]=A[i-1][j-1]+A[i-1][j];
    for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
    {
    printf("%d",A[i][j]);
    }
    }
    getch();
    }

    ReplyDelete
  51. how to generate
    **********
    ****--****
    ***----***
    **------**
    *--------*

    ReplyDelete
  52. how to print this patern
    *
    * *
    * *
    * *

    ReplyDelete
  53. WAP to genarate
    1--------1
    12------21
    123----321
    1234--4321
    1234554321

    ReplyDelete
  54. how to print
    A
    CBC
    DDCDD
    EEEDEEE
    FFFFEFFFF

    ReplyDelete
  55. 1
    11
    121
    1331
    14641

    How to write a c program to get a pyramid like this?

    ReplyDelete
    Replies
    1. pls sent the programe this patern

      Delete
  56. what is the code of this:
    1
    2 2
    3 3 3
    4 4 4 4
    5 5 5 5 5
    4 4 4 4
    3 3 3
    2 2
    1

    ReplyDelete
    Replies
    1. #include
      int main()
      {
      int r,s,st,n,m;
      int nst=n;
      printf("Enter size");
      scanf("%d",&n);
      for(m=0;m<2;m++)
      {
      if(m==0){
      for(r=1;r0;r--)
      {
      for(s=n-r;s>0;s--)
      {
      printf("");
      }
      for(st=r;st>0;st--)
      {
      printf("%d",r);
      printf(" ");
      }
      printf("\n");
      }
      }
      }
      }

      Delete
    2. Please i want to display
      1 2 3 4 5
      1 2 3 4
      1 2 3
      1 2
      1
      this output.... HELP ME!!

      Delete
  57. ***1
    **123
    *12345
    *1234567
    123456789
    how can i get this output???any1 give me the code for this????thnks

    ReplyDelete
  58. How to program that will display this:

    *
    * * *
    * * * * *
    * * * * * * *

    ReplyDelete
  59. How to create a program that display this:

    ________*
    ______* * *
    ____* * * * *
    __* * * * * * *

    ReplyDelete
  60. Can Someone Help Me With This???

    _________________________1
    ______________________1__2__1
    ___________________1__2__4__2__1
    ________________1__2__4__8__4__2__1
    _____________1__2__4__8__16_8__4__2__1
    __________1__2__4__8__16_32_16_8__4__2__1
    _______1__2__4__8_16__32_64_32_16_8__4__2__1
    ____1__2__4__8_16_32_64_128_64_32_16_8_4__2__1

    ReplyDelete
  61. can you print
    ##
    ###
    ####
    #####
    ######
    #######
    ########
    #########
    ##########

    ReplyDelete
  62. using function

    1
    2 3
    4 5 6
    7 8 9 1
    2 3 4 5 6

    if there were five rows....

    ReplyDelete
  63. how to create following:
    1
    2 1
    3 2 1

    ReplyDelete
  64. and this one too.........
    -----1
    ---2-1
    3-2-1

    where '-' is a space......

    ReplyDelete
  65. write a program to print the following?
    *
    **
    ***
    ****
    *****

    ReplyDelete
  66. ----1
    --2-3-4
    5-6-7-8-9

    (- indicates spaces)

    pls help to print this pattern.

    ReplyDelete
    Replies
    1. #include
      int main()
      {
      int i,j=0,k,n;
      scanf("%d",&n);
      for(i=1;i<=n;i++)
      {
      for(k=n;k>=i;k--)
      printf(" ");
      for(j=j;j<=i*i;j++)
      {
      if(j==0)
      continue;
      printf(" %d ",j);
      j=j;
      }
      printf("\n");
      }
      return 0;
      }

      Delete
    2. #include
      int main()
      {
      int i,j,k,n;
      printf("Enter");
      scanf("%d",&n);
      int num=1;
      for(i=1;i<=n;i=i+1)
      {

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

      for(k=num;k<=i*i;k++)
      {
      printf(" %d",num);

      num++;

      }
      printf("\n");
      }

      }

      Delete
  67. how to generate this pyramid in C -
    1
    1 2
    1 2 4
    1 2 4 7

    ReplyDelete
  68. creating pyramid of numbers is the basic lab c program used in many institutions. Thank you for sharing this program.
    regards:
    srinath reddy.
    admin of Programming Tutorials for Beginners

    ReplyDelete
  69. 4444444
    4333334
    4322234
    4321234
    4322234
    4333334
    4444444
    Any one Solve it !

    ReplyDelete
    Replies
    1. #include
      #include
      #include
      void main()
      {
      int arr[10][10];
      int limit,col,row,r,c,val,max=0;
      int width;
      printf("enter the limit");
      scanf("%d",&limit);
      width=limit*2-1;
      for(r=1;r<=width;r++)
      {
      for(c=1;c<=width;c++)
      {
      row=abs(limit-r);
      col=abs(limit-c);
      max=row;
      if(col>row)
      max=col;
      val=max+1;
      arr[r][c]=val;
      printf("%d",arr[r][c]);
      }
      printf("\n");
      }
      }

      Delete
  70. Can anyone tell me how to print the following pyramid
    1
    2 2
    3 3 3
    4 4 4 4

    ReplyDelete
  71. how to print
    1
    2 6
    3 7 10
    4 8 11 13
    5 9 12 14 15

    ReplyDelete
  72. hey i want to write this plz help
    1
    12
    123
    1234
    12345
    1234
    123
    12
    1
    with for loop

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. This comment has been removed by the author.

      Delete
  73. what s the program to print the following..??

    1
    1 2 1
    1 2 3 2 1

    ReplyDelete
  74. ........0
    ......101
    ....21021
    ..3210123
    .432101234
    54321012345
    What is the code for this in c++..
    please give me code its argent !

    ReplyDelete
  75. . Write a program to display a pyramid
    1
    32
    654
    10987

    ReplyDelete