显示 X 形图案中数字的 C 程序
请参考下面给出的算法,了解用于显示 X 形图案中数字的 C 程序。
算法
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 " "
以 X 形图案打印数字的逻辑如下 -
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("
");
}程序
以下是显示 X 形图案中数字的 C 程序 -
#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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP