2.19.2012

Symbol rhombus

Q. Write a C program to print the following number structure:
        *
       ***
      *****
     *******
      *****
       ***
        *


Ans.


/*c program for symbol pyramid*/

#include<stdio.h>
#include<conio.h>
int main()
{
  int num,r,c,sp;
  printf("Enter number of rows : ");
  scanf("%d",&num);
  for(r=1; r<=num; r++)
  {
    for(sp=num-r; sp>=1; sp--)
        printf(" ");
    for(c=1; c<=r; c++)
        printf("*");
    for(c=r-1; c>=1; c--)
        printf("*");
    printf("\n");
  }
  for(r=1; r<=num; r++)
  {
    for(sp=r; sp>=1; sp--)
        printf(" ");
    for(c=1; c<=(num-r); c++)
        printf("*");
    for(c=num-r-1; c>=1; c--)
        printf("*");
    printf("\n");
  }
  getch();
  return 0;
}


/*********** OUTPUT ************/


12 comments:

  1. Thank you Sooooooooo much :)

    ReplyDelete
  2. This is the best rhombus program without a single error...
    Thank u very much!!!

    ReplyDelete
  3. could u please provide algorithms and for flowchart for this program?

    ReplyDelete
  4. Row number does not match, if you give input 7, the compiler gives 13 line as output,
    bcs, the compiler gets input 7+inside code 7-1=6, so, the compiler gives 7+6=13 lines.

    It's an error, can you explain plz... ... ... @ Admin

    ReplyDelete
    Replies
    1. @Jack DSauza,

      In above program we are making rhombus through lines (i.e. rows),

      Let's say user give input 3 then its mean that top part of rhombus will be make in three lines. And here no matter how much symbols comes in the columns.

      Hence, all the calculation in above symbol rhombus program calculated through line(rows) not column.

      Delete
  5. This is a program in terms of the number of columns:-
    /*


    *
    ***
    *****
    *******
    *****
    ***
    *
    */

    #include
    int main()
    {

    int m=42;
    printf("enter the number of columns\n");
    int num;
    scanf("%d",&num);
    for(int i=num;i>=1;i-=2)
    {
    for(int j=(i-1)/2;j>=1;j--)
    {
    printf(" ");


    }
    for(int k=num-i+1;k>=1;k--)
    {
    printf("%c",(char)m);
    }
    printf("\n");
    }
    for(int i=num-2;i>=1;i-=2)
    {
    for(int j=(num-i)/2;j>=1;j--)
    {
    printf(" ");
    }
    for(int k=i;k>=1;k--)
    {
    printf("%c",(char)m);
    }
    printf("\n");
    }
    return 0;
    }

    ReplyDelete
  6. *
    ***
    *****
    *******
    *****
    ***
    *
    can u use only two for loops and if condition

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

    ReplyDelete
  8. How to print this if input is given as 3

    0
    010
    01210
    0123210
    01210
    010
    0

    ReplyDelete
    Replies
    1. @ REMO JOHNSON

      your required number pyramid C program source code as following:

      #include"stdio.h"
      int main()
      {
      int r,c,num=3,n;
      for(r=0; r<=num; r++)
      {
      for(c=0; c<=r; c++)
      printf("%d",c);
      for(c=r-1; c>=0; c--)
      printf("%d",c);
      printf("\n");
      }
      for(r=1,n=num; r<=num; r++,n--)
      {
      for(c=0; c<=n-1; c++)
      printf("%d",c);
      for(c=num-r-1; c>0; c--)
      printf("%d",c);
      printf("\n");
      }
      getch();
      return 0;
      }

      Delete
  9. Hey how do you create the option in the output by using a function, void displayRhombus(int diagonal, char fillCharacter) ? : I want to create an option for 'enter the character and enter the number of rows'!

    Program execution sample:
    Please enter the character: #
    Please enter the diagonal of the rhombus: 7
    #
    ###
    #####
    #######
    #####
    ###
    #
    For example, if the function call is
    displayRhombus(3, ‘#’), then it should display:
    #
    ###
    #
    Or, if the function call is displayRhombus(5, ‘$’), then it should display:
    $
    $$$
    $$$$$
    $$$
    $

    ReplyDelete