facebook like button

01 May, 2011

program 40: Diamond of $

The need:
     This is a simple program to print diamond of '$'. This program was is an extension of previous program - printing an equilateral triangle of star.

The code: 
--------------------------------------------
#include<stdio.h>
main()
{
    int j,i,k,l;
    char c;
    do
    {
        printf("Enter the number of stars in the base..");
        scanf("%d",&k);
        for (i=1;i<=k;i++)
        {
            for(l=0;l<(k-i);l++)
                printf(" ");
            for (j=0;j<i;j++)
                printf ("$ ");
            printf("\n");
        }
        for (i=1;i<=k;i++)
        {
            for(l=0;l<i;l++)
                printf(" ");
            for (j=1;j<=k-i;j++)
                printf ("$ ");
            printf("\n");
        }
        printf("\nEnter y to try more...press any other key exit...\t");
        c=getchar();
        c=getchar();
    }
    while(c=='y');
} 
-------------------------------------------- 

The approach: 
This program creates a diamond on the output screen. First it asks the user to input the number of stars in the base and gives the output and then asks the user whether he wants the program to stop or wants to draw one more diamond. To understand functionality read program39. You might want to take a look on the next program of this blog which is further extension of this program. Have a look that is cool.

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply