首页  >  文章  >  后端开发  >  在C语言中编写一个程序来打印实心和空心菱形图案

在C语言中编写一个程序来打印实心和空心菱形图案

WBOY
WBOY转载
2023-08-29 09:33:141039浏览

程序说明

打印如下所示的实心和空心菱形图案

在C语言中编写一个程序来打印实心和空心菱形图案

算法

对于空心菱形 -

Accept the Number of Rows for Hollow Rhombus from the User
Create a Hollow Rhombus containing the same number of Rows specified by the User.
Print the first row containing the number of stars same as the number of rows.
Print the second row containing the first and last star as show in the output and leave the spaces between first and the last star.
Do the same till you reach the last Row.
Print the last row containing the number of stars same as the number of rows.

For Solid Rhombus −

Accept the Number of Rows for Solid Rhombus from the User
Create a Solid Rhombus containing the same number of Rows specified by the User.
Print the first row containing the number of stars same as the number of rows.
Do the same till you reach the last Row.

Example

的中文翻译为:

示例

/* Program to print Hollow and Solid Rhombus star pattern */
#include <stdio.h>
int main() {
   int r, c, rows; //Hollow Rhombus
   int r1,c1, rows1; //Solid Rhombus
   clrscr();
   printf("Enter the Number of rows for Hollow Rhombus Pattern: ");
   scanf("%d", &rows);
   printf("</p><p>");
   for(r=1; r<=rows; r++){
      for(c=1; c<=rows-r; c++){
         printf(" ");
      }
      for(c=1; c<=rows; c++){
         if(r==1 || r==rows || c==1 || c==rows)
         printf("*");
         else
         printf(" ");
      }
      printf("</p><p>");
   }
   printf("</p><p>");
   printf("Enter the Number of rows for Solid Rhombus Pattern: ");
   scanf("%d", &rows1);
   printf("</p><p>");
   for (r1=1; r1<=rows1; r1++){
      for (c1=1; c1<=rows1-r1;c1++){
         printf(" ");
      }
      for (c1=1; c1<=rows1; c1++){
         printf("*");
      }
      printf("</p><p>");
   }
   getch();
   return 0;
}

输出

在C语言中编写一个程序来打印实心和空心菱形图案

以上是在C语言中编写一个程序来打印实心和空心菱形图案的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除