C Exercises

Aug 18, 2020

C program to print mirrored rhombus star pattern

Ex: Write a C program to print mirrored rhombus star pattern. How to write a C program to print mirrored rhombus star pattern. C program to print mirrored rhombus star pattern.


Input from user:

Enter the number:

5


Expected output:

*****

 *****

  *****

   *****

    *****



  Step by step logic of the given program:


1. Accept input (number) from user declare variable say no.


2. Run a loop(outer loop), to iterate through rows and increment i by 1:

for(i=1; i<=no; i++)


3. After that to print spaces run another loop(inner loop):

for(j=1; j<i; j++)

{

printf(" ");

}


4. To print stars run another loop(inner loop) from 1 to no. Print star inside this loop:

for(k=1; k<=no; k++)

 {

printf("*");

 }


5. After printing all columns of a row, print new line (\n), for move it to next line.





Program:


#include<stdio.h>

void main()

{


 int i,j,k,no;

   /*Take input number from user which denotes number of rows and columns in pattern*/

   printf("Enter the number:\n");

   scanf("%d",&no);

   for(i=1; i<=no; i++)

   {

     /*Print spaces*/

     for(j=1; j<i; j++)

{

printf(" ");

}

     for(k=1; k<=no; k++)

{

printf("*");

}

     printf("\n");//for new line

 }

}


Above program shows the following output:


C program to print mirrored rhombus star pattern


No comments:

Post a Comment

If you have any doubts, please discuss here...👇