Archive for the ‘Coding’ Category

C Program: Empty Diamond (or Triangle)

Here is another C program for displaying an empty diamond. This is another C program code for displaying a triangle. The user will input the base for the diamond (or triangle). The maximum input for this program is 40 although can easily be changed when implemented.

C program for an empty diamond

#include 
#include 

int base, row, column, temp;

main()
{
    // Input Area - do-while is used to validate such that only inputs from 0 to 40 are accepted
    do
    {
         system(”cls”);
         printf(”Enter base : “);
         scanf(”%d”, &base);
    } while ((base<0) || (base>40));

    //Output Area
    temp=(2*base)-1;
    for (row=1;row<=temp;row++)
    {
        for (column=1;column<=temp;column++)
        {
          if ((row<=base) && ((column==((base-1)+row)) || (column==((base+1)-row)))) //upper half of the triangle
                 printf("*",column);
          else if ((row>base) && ((column==(((3*base)-1)-row)) || (column==((row+1)-base)))) //lower half of the triangle
                 printf(”*”,column);
          else
              printf(” “);
        }
        printf(”\n”);
     }
    system(”pause”);
}

C Program: Multiplication Table

Here is a C program for printing a multiplication table based on a user input n. The resulting table will be n x n. The maximum input is limited to 15 in order for the results to readable. Higher input will scroll the results.

#include 
#include 

int number, row, column, result;

main(void)
{
    /*Input Area - Input number must be from 0 to 15, else input loop will continue */

    do
    {
          system(”cls”);
          printf(”Enter max value: “);
          scanf(”%d”, &number);
          result=number;
    } while ((number<0)|(result>15));

    /* Output Area - row and column are used as counters and
        initialized to 0 as to requirements.
       Final values are compared to the input number
       Inner do-while prints per column in a line.
       Outer do-while moves the cursor to the next line
    */

    row=0;
    do
    {
        column=0;
        do
        {
              result=row*column;
              if (result<1)
                  printf("");
              else if (result<10)
                  printf("  %d  ", result);
              else if (result<100)
                  printf(" %d  ", result);
              else if (result<1000)
                  printf("%d  ", result);
              else
                  printf("");
              column++;
        } while (column<=number);

        printf("\n");
        row++;
    } while (row<=number);
    system("pause");

The above code will generate this output.

multiplication table output of a  C program

Format Specifiers in C

The following format specifiers can be used when programming in C.


%d (or %i) int
%c char
%f float
%lf double
%s string
%x hexadecimal

By specifying an integer between the % and the format specifier, the number of spaces displayed can be controlled easily.


%9d will display 9 spaces before a character is displayed
%09 will display 9 zeros (0) before a character is displayed
%-9d a negative sign will left align the result
%-09d a zero(0) format will be ignored when a negative sign is used
%1.1f an integer after the period (.) specifies the number of decimal places

Other specifiers that may be needed to control the output


\n new line
\t tab
\b backspace