C Exercises

Aug 18, 2020

C program to print Rhombus star pattern

Ex: Write a C program to print Rhombus star pattern. how to print Rhombus star pattern using for loop. C Program to print Rhombus star Pattern.
In below code we  print Rhombus star pattern.



Input from user:

Enter the number: 5

Expected output:

    *****
   *****
  *****
 *****
*****




  Step by step logic to print Rhombus star pattern.

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<=no-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:

/***C program to print Rhombus star pattern***/

#include<stdio.h>
void main()
{
int i,j,k,no;

printf("Enter the number:\n");/*take input number from user which denotes number of rows*/
scanf("%d",&no);

for(i=1;i<=no;i++)
{
for(j=1;j<=no-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 Rhombus star pattern

No comments:

Post a Comment

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