C程序以X形式显示数字

WBOY
WBOY 转载
2023-08-26 20:05:06 387浏览

C程序以X形式显示数字

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

算法

Step 1: Start
Step 2: Declare variables
Step 3: Read number of rows
Step 4: for loop satisfies
  • if(i==j || i+j==rows-1)
    • print i+1
  • Print " "
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("

"); }

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:

"); 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("

"); } }

输出

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

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,如有侵犯,请联系admin@php.cn删除