首页 > 后端开发 > C++ > 正文

C程序以X形式显示数字

WBOY
发布: 2023-08-26 20:05:06
转载
677 人浏览过

C程序以X形式显示数字

参考下面的算法,编写C程序以显示X形状的数字。

算法

Step 1: Start
Step 2: Declare variables
Step 3: Read number of rows
Step 4: for loop satisfies<ul class="list"><li>if(i==j || i+j==rows-1)<ul class="list"><li>print i+1</li></ul></li><li>Print " "</li></ul>Step 5: Print new line
Step 6: Stop
登录后复制

The logic to print numbers in X pattern is as follows −

for(i=0;i<rows;i++){
   for(j=0;j<rows;j++){
      if(i==j || i+j==rows-1){
         printf("%d",i+1);
      }else{
         printf(" ");
      }
   }
   printf("</p><p>");
}
登录后复制

Program

Following is the C program to display the numbers in X pattern −

#include<stdio.h>
main(){
   int i,j,rows;
   printf("Enter number of rows:</p><p>");
   scanf("%d",&rows);
   for(i=0;i<rows;i++){
      for(j=0;j<rows;j++){
         if(i==j || i+j==rows-1){
            printf("%d",i+1);
         }else{
            printf(" ");
         }
      }
      printf("</p><p>");
   }
}
登录后复制

输出

当上述程序被执行时,它产生以下结果 −

Enter number  of rows:10
1           1
  2        2
    3    3
      4 4
       55
       66
      7 7
    8     8
  9        9
10          10
登录后复制

以上是C程序以X形式显示数字的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!