首頁 > 後端開發 > 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學習者快速成長!