Home > Backend Development > C++ > How to print stars in a diamond pattern using C language?

How to print stars in a diamond pattern using C language?

PHPz
Release: 2023-09-03 14:41:06
forward
1550 people have browsed it

How to print stars in a diamond pattern using C language?

Here, to print stars in a diamond pattern, we use nested for loops.

Our logic for printing stars in diamond pattern is as follows -

//For upper half of the diamond the logic is:
for (j = 1; j <= rows; j++){
   for (i = 1; i <= rows-j; i++)
      printf(" ");
   for (i = 1; i<= 2*j-1; i++)
      printf("*");
   printf("</p><p>");
}
Copy after login

Suppose let us consider rows=5, it prints the output as follows -

      *
     ***
    *****
   *******
  *********
Copy after login

//For lower half of the diamond the logic is:
for (j = 1; j <= rows - 1; j++){
   for (i = 1; i <= j; i++)
      printf(" ");
   for (i = 1 ; i <= 2*(rows-j)-1; i++)
      printf("*");
   printf("</p><p>");
}
Copy after login

Assuming row=5, the following output will be printed -

*******
  *****
  ***
   *
Copy after login

Example

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template