UAB – 2006: Problem 3: Diamond Printer


This program will print a shape on the screen using asterisks (“*”) characters.

The user will be prompted to enter an ODD number between 1 and 99 (you may assume test cases will not be beyond this range and that all test cases will be odd numbers). The shape that will be printed resembles a diamond, where the number provided by the user represents the number of *’s printed on the middle line. The line above and below will be centered and will have 2 less *’s than the middle line. This reduction by 2 *’s for each line continues until a line with a single * is printed at the top and bottom of the figure.


// output 
Example 1:  
Enter an odd number (1-100): 1 
* 

Example 2: 
Enter an odd number (1-100): 5 
  * 
 *** 
***** 
 *** 
  *

#include <stdio.h>

int main ()
{
    printf ("Enter an odd number (1-100): ");

    int oddNumber;

    scanf ("%d", &oddNumber);

    int length = oddNumber / 2;

    for ( int i = 0; i <= length; i++ ) {
        for ( int j = length - i - 1; j >= 0; j-- )
            printf (" ");
        for ( int k = 1; k < 2 * (i + 1); k++ )
            printf ("*");
        printf ("\n");
    }

    for ( int i = 0; i < length; i++ ) {
        for ( int j = 1; j <= i + 1; j++ )
            printf (" ");
            oddNumber -= 2;
        for ( int k = oddNumber; k >= 1; k-- )
            printf ("*");
        printf ("\n");
    }

    return 0;
}

4 thoughts on “UAB – 2006: Problem 3: Diamond Printer

  1. Solved in Python 3.1.2:

    x = int(input(“Thickest point of diamond? (positive odd integer)”))

    for y in range(1, x, 2):
    print((“*” * y).center(80))

    for y in range(x, -1, -2):
    print((“*” * y).center(80))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s