HOW TO MAKE A HOLLOW DIAMOND IN C.

Here is the 1st algorithm in c language.

if you want any algorithm or program to understand and want a source code then let me know in the comments and if you want a video on it then also let me know in the comments.


HOW TO MAKE A HOLLOW DIAMOND IN C.


                   

this is your output!!


here (rows) is taken by the user and you know from the shape that row are equal to columns (rows=columns)

the first star is printed on half of the inputted rows that's why k=rows/2 but,

if rows are even then subtract 1 to get the middle index i.e 

if rows=6 then  k=(6-1)/2= 2.5 (but the compiler will ignore everything after the point because the datatype is int).

and if rows is odd then we don't have to subtract one.



if(i<totalrows/2)

it is for printing the above pattern 

the diamond is 2d shape so nested loops are required.

if(j==((totalrows-1)/2)+i || j==((totalrows-1)/2)-i )

 for printing stars .

and similarly the lower portion is drawn .



guys if you want to understand it by the video then let me know in the comments.


#include<stdio.h>
#include<conio.h>
int main()
{
int totalrows,i,j,k;
printf("enter the total rows : ");
scanf("%d",&totalrows);
if(totalrows%2==0)
    {k=((totalrows-1)/2);}
else if(totalrows%2!=0)
    {k=((totalrows)/2);}




for(i=0 ; i<totalrows ; i++)
{
    if(i<totalrows/2)
    {
    for(j=0;j<totalrows;j++)
    {
        if(j==((totalrows-1)/2)+i || j==((totalrows-1)/2)-i )
        {
            printf("*");
        }
        else
        {
            printf(" ");
        }
        
    }
    printf("\n");
    }
    else
    {
        for(j=0;j<totalrows;j++)
    {
        if(j==((totalrows-1)/2)+k || j==((totalrows-1)/2)-k)
        {
            printf("*");
        }
        else
        {
            printf(" ");
        }
        
    }k--;
    printf("\n");
    }
    
}
return 0;
}

subscribe for more coding algos and if you have any questions then drop them in the comment section!!!

codesInStyle



Comments

Post a Comment