C code for diamond

KPZ

Well-known member
  • Apr 15, 2011
    17,331
    1,622
    113
    where I wanna be
    මචෝ මේකේ කරන්න ඕන දේ කෙටියෙන් කියපන් මට.
    මම බලන්නම් එතකොට
     

    member01

    Junior member
  • May 6, 2009
    205
    9
    18
    Sri lanka
    #include <stdio.h>


    char p ;
    char q ;// global variable
    char diamond ();
    void info (char l, char m);


    void main() //main program
    {
    char z,w;
    printf("****************************************** *****\n\n");
    diamond ();
    info (p,q);
    printf("****************************************** *****\n\n");

    }


    char diamond () //displays instructions to the user
    {


    printf("Eter the 1st character = ");
    scanf("%c",&p);
    printf("\n");
    printf("Eter the 2nd character = ");
    scanf("%c",&q);
    printf("\n");
    return (p,q);

    }

    void info (char l, char m) //prints shape of a diamond on the screen
    {
    int a,b,c ;
    int x,y,z;
    int temp =4;

    for( x=0 ; x<4 ;x++) //for the fist half of diamond
    {
    for( y=0 ; y<temp ;y++)
    {
    printf(" ");
    }
    temp--;
    printf("%c" ,l);
    for( z=0 ; z<2*x-1 ;z++)
    {
    printf("%c" ,m);
    }
    if(x != 0 ) printf("%c" ,l);
    printf(" ");

    }

    for(a=3; a>0 ;a--) //for the second half of diamond
    {
    for(b=a-1; b<4; b++)
    {
    printf(" ");
    }
    printf("%c" ,l);
    for(c=0; c<2*(a-1)-1 ; c++)
    {
    printf("%c" ,m);
    }
    if(a != 1){printf("%c" ,l);}
    printf("\n");

    }
    }
     

    shanakaplus

    Well-known member
  • Aug 19, 2007
    2,001
    89
    48
    37
    kurunegala
    #include <stdio.h>


    char p ;
    char q ;// global variable
    char diamond ();
    void info (char l, char m);


    void main() //main program
    {
    char z,w;
    printf("****************************************** *****\n\n");
    diamond ();
    info (p,q);
    printf("****************************************** *****\n\n");

    }


    char diamond () //displays instructions to the user
    {


    printf("Eter the 1st character = ");
    scanf("%c",&p);
    printf("\n");
    printf("Eter the 2nd character = ");
    scanf("%c",&q);
    printf("\n");
    return (p,q);

    }

    void info (char l, char m) //prints shape of a diamond on the screen
    {
    int a,b,c ;
    int x,y,z;
    int temp =4;

    for( x=0 ; x<4 ;x++) //for the fist half of diamond
    {
    for( y=0 ; y<temp ;y++)
    {
    printf(" ");
    }
    temp--;
    printf("%c" ,l);
    for( z=0 ; z<2*x-1 ;z++)
    {
    printf("%c" ,m);
    }
    if(x != 0 ) printf("%c" ,l);
    printf(" ");

    }

    for(a=3; a>0 ;a--) //for the second half of diamond
    {
    for(b=a-1; b<4; b++)
    {
    printf(" ");
    }
    printf("%c" ,l);
    for(c=0; c<2*(a-1)-1 ; c++)
    {
    printf("%c" ,m);
    }
    if(a != 1){printf("%c" ,l);}
    printf("\n");

    }
    }

    mcn meka vada karanwada kiyala baluwada?
     

    shanakaplus

    Well-known member
  • Aug 19, 2007
    2,001
    89
    48
    37
    kurunegala
    #include <stdio.h>


    char p ;
    char q ;// global variable
    char diamond ();
    void info (char l, char m);


    void main() //main program
    {
    char z,w;
    printf("****************************************** *****\n\n");
    diamond ();
    info (p,q);
    printf("****************************************** *****\n\n");

    }


    char diamond () //displays instructions to the user
    {


    printf("Eter the 1st character = ");
    scanf("%c",&p);
    printf("\n");
    printf("Eter the 2nd character = ");
    scanf("%c",&q);
    printf("\n");
    return (p,q);

    }

    void info (char l, char m) //prints shape of a diamond on the screen
    {
    int a,b,c ;
    int x,y,z;
    int temp =4;

    for( x=0 ; x<4 ;x++) //for the fist half of diamond
    {
    for( y=0 ; y<temp ;y++)
    {
    printf(" ");
    }
    temp--;
    printf("%c" ,l);
    for( z=0 ; z<2*x-1 ;z++)
    {
    printf("%c" ,m);
    }
    if(x != 0 ) printf("%c" ,l);
    printf(" ");

    }

    for(a=3; a>0 ;a--) //for the second half of diamond
    {
    for(b=a-1; b<4; b++)
    {
    printf(" ");
    }
    printf("%c" ,l);
    for(c=0; c<2*(a-1)-1 ; c++)
    {
    printf("%c" ,m);
    }
    if(a != 1){printf("%c" ,l);}
    printf("\n");

    }
    }

    fds.jpg


    :( :growl::growl::growl::growl:
     

    KPZ

    Well-known member
  • Apr 15, 2011
    17,331
    1,622
    113
    where I wanna be
    මේකෙ වෙන්න ඕනි main එක ඇතුලේ info function එක call වෙන්න. info call උනාම එකේදී තමයි user ට කියන්නේ Letters 2ක් input කරන්න කියලා.
    ඊට පස්සේ ඒ letters 2 parameters 2ක් විදිහට යවලා diamond function එක call වෙනවා.

    එහෙම නේද?
     

    shanakaplus

    Well-known member
  • Aug 19, 2007
    2,001
    89
    48
    37
    kurunegala
    මේකෙ වෙන්න ඕනි main එක ඇතුලේ info function එක call වෙන්න. info call උනාම එකේදී තමයි user ට කියන්නේ Letters 2ක් input කරන්න කියලා.
    ඊට පස්සේ ඒ letters 2 parameters 2ක් විදිහට යවලා diamond function එක call වෙනවා.

    එහෙම නේද?

    ow mcn mama meka c++ walin liyapuwama wada kara ban.... C walin liyapuwama thama aul giye.. :(:(:(
     

    Snowl

    Member
    Oct 1, 2011
    1,016
    137
    0
    Virtual Realm
    it's already been developed by someone so why are you bothering to reinvent the wheel.
    Code:
    #include <stdio.h>
    int main()
    {
    int i,j,k;
    
    printf("Select Size \n");
    scanf("%d",&k);
    
    
    for(i=1; i<=k; i++)
    
    {
    
    for(j=1; j<=k-i; j++)
    
    {
    printf(" ");
    }
    
    for(j=1; j<=2*i-1; j++)
    
    {
    printf("*");
    }
    
    printf("\n");	
    
    }
    
    for(i=1; i<k; i++)
    
    {
    
    for(j=1; j<=i; j++)
    
    {
    printf(" ");
    }
    
    for(j=2*k-2*i-1; j>=1; j--)
    
    {
    printf("*");
    }	
    
    printf("\n");	
    
    }	
    
    return(0);	
    
    }
     

    nagaya

    Member
    Mar 18, 2007
    12,671
    194
    0
    I've written this some time ago to display another shape,you can modify it and use accordingly

    Code:
    #include <stdio.h>
    int main(){
    int x,y;
    for( x=1;x<10;x++){
    for( y=0;y<x;y++)
    printf("*");
    for( y=0;y<(10-x);y++)
    printf("~");
    for( y=0;y<(10-x);y++)
    printf("~");
    for( y=0;y<x;y++)
    printf("*");
    printf("\n");
    }
    
    for( x=9;x>0;x--){
    for( y=0;y<x;y++)
    printf("*");
    for( y=0;y<(10-x);y++)
    printf("~");
    for( y=0;y<(10-x);y++)
    printf("~");
    for( y=0;y<x;y++)
    printf("*");
    printf("\n");
    }
    }

    output

    Code:
    D:\>a
    *~~~~~~~~~~~~~~~~~~*
    **~~~~~~~~~~~~~~~~**
    ***~~~~~~~~~~~~~~***
    ****~~~~~~~~~~~~****
    *****~~~~~~~~~~*****
    ******~~~~~~~~******
    *******~~~~~~*******
    ********~~~~********
    *********~~*********
    *********~~*********
    ********~~~~********
    *******~~~~~~*******
    ******~~~~~~~~******
    *****~~~~~~~~~~*****
    ****~~~~~~~~~~~~****
    ***~~~~~~~~~~~~~~***
    **~~~~~~~~~~~~~~~~**
    *~~~~~~~~~~~~~~~~~~*