Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7913415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:50:20+00:00 2026-06-03T13:50:20+00:00

I want to be able to print a diamond like this when the user

  • 0

I want to be able to print a diamond like this when the user enters 5 for the diamond. But also will work for any value that is odd and greater than 0.

enter image description here

I have a code that works to make a diamond for user input of 5 but won’t work for all odd number inputs..

 half = (size/2)+1;

 for (a=1; a <=  half ; a++) /*top to mid row of diamond*/
   {
     for (b=a; b<half;b++)
       {
     printf(" ");
       }
     for (c= size -2* a; c <=  half; c++)
       {
     printf("*");
       } 
      printf("\n");
   }
 for (a = 1; a < half;a++)
   {
     for (b = a; b>0;b--)
       {
     printf(" ");
       }
     for (c = size-2*a; c >0 ;c--)
       {
     printf("*");
       }
     printf("\n");
   }


  return 0;
}

Any help would be greatly appreciated.Thank you.

Mike

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:50:21+00:00Added an answer on June 3, 2026 at 1:50 pm

    Almost certainly homework so here’s a clue.

    Count the number of spaces before a line and the number of stars in that line. What you’re looking for is a relationsip between the line number and those two values.

    Then you can use two consecutive for loops, one increasing the star count and the other decreasing it.

    Within each of those loops would be two more consecutive loops to print out the required number of spaces followed by the required number of stars followed by a newline character.


    If you’re still having trouble after reading the above, consider this. For an input of (odd, as you state you enforce in your comments) n, the space count starts at (n - 1) / 2 and the star count at 1. For each subsequent line, the space count reduces by 1 and the star count increases by 2.

    That works up until the point where the space count reaches 0, then you turn around and go the other way, making sure you don’t print that middle line twice.

    Once the star count reaches 0, you’re done.

    Now you just have to turn that specification into code 🙂


    Now, since you’ve indicated in comments that you’re interested in making your own solution rather than just being handed code, I feel comfortable giving you something you can check your own solution against. Here’s the pseudo-code I would use:

    # Input data, check and init counters.
    
    input n
    make sure n is odd and greater than 2
    set numspaces to (n-1) / 2
    set numstars to 1
    
    # Gradually get wider until just before middle line.
    
    while numspaces > 0:
        for i = 1 to numspaces: output " "
        for i = 1 to numstars:  output "*"
        output newline
        subtract 1 from numspaces
        add 2 to numstars
    
    # Gradually get thinner until end.
    
    while numstars > 0:
        for i = 1 to numspaces: output " "
        for i = 1 to numstars:  output "*"
        output newline
        add 1 to numspaces
        subtract 2 from numstars
    

    And, as a final exercise, you can refactor:

        for i = 1 to numspaces: output " "
        for i = 1 to numstars:  output "*"
        output newline
    

    into a separate function, since it’s common between the two loops.


    And now, since you’ve got your own code working, here’s the Python code I used for proof of concept, included for completeness:

    def lineout (sp, st):
        s = ""
        for i in range (sp): s = "%s "%(s)
        for i in range (st): s = "%s*"%(s)
        print s
    
    n = 21
    numspaces = (n-1) / 2
    numstars = 1
    
    while numspaces > 0:
        lineout (numspaces, numstars)
        numspaces -= 1
        numstars += 2
    
    while numstars > 0:
        lineout (numspaces, numstars)
        numspaces += 1
        numstars -= 2
    

    You could probably write it more succinctly in Python if you used the more modern features but that would rather defeat the purpose of quick understanding and easy translation. Just change n to whatever number you want (odd and greater than two, providing the resultant diamond will fit in your terminal) and enjoy the output 🙂

              *
             ***
            *****
           *******
          *********
         ***********
        *************
       ***************
      *****************
     *******************
    *********************
     *******************
      *****************
       ***************
        *************
         ***********
          *********
           *******
            *****
             ***
              *
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array structure like this, which I'm able to print out just
I want to be able to print symbols that represent numbers using functions and
I have a long integer that I want to be able to print out
I'm new to python What I want is to be able to print content
I have some jquery code that i want to able to drag, drop, clone
I have a messages table that auto increments and I simply want to able
I want to be able to print out all of the changes to the
My users want to be able to print a report I've designed in VS
I am searching an xml file that looks like this: <producers> <producer> <name></name> <owner></owner>
I want to be able to print out a table with x rows and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.