首頁 > 後端開發 > C++ > 主體

編寫一個在C語言中列印數字模式的程序

PHPz
發布: 2023-09-05 20:13:07
轉載
1062 人瀏覽過

程式說明

數字模式是根據稱為模式規則的規則建立的數字序列。模式規則可以使用一個或多個數學運算來描述序列中連續數字之間的關係。

模式範例

模式1

1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
登入後複製

#模式2

        1
      1 2 3
    1 2 3 4 5
  1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9
  1 2 3 4 5 6 7
    1 2 3 4 5
      1 2 3
        1
登入後複製

演算法

Pattern 1:
i stands for rows and j stands for columns.
5 stands for making pattern for 5 Rows and Columns
Loop for each Row (i)
K is initialized to i
Loop for each Column (j)
Do the Pattern for the current Column (j)
Display the Value of K
Reinitialize the Value of K = k + 5 - j
Pattern 2:
First Row: Display 1
Second Row: Display 1,2,3
Third Row: Display 1,2,3,4,5
Fourth Row: Display 1,2,3,4,5,6,7
Fifth Row: Display 1,2,3,4,5,6,7,8,9
Display the same contents from 4th Row till First Row below the fifth Row.
登入後複製

範例

/* Program to print Numeric Pattern */
#include
int main(){
   int i,j,k;
   printf("Numeric Pattern 1");
   printf("

"); printf("

"); for(i=1;i<=5;i++){ k = i; for(j=1;j<=i;j++){ printf("%d ", k); k += 5-j; } printf("

"); } printf("

"); printf("Numeric Pattern 2"); printf("

"); printf("

"); for(i = 1;i<=5;i++){ for(j = i;j<5;j++){ printf(" "); } for(k = 1;k<(i*2);k++){ printf("%d",k); } printf("

"); } for(i = 4;i>=1;i--){ for(j = 5;j>i;j--){ printf(" "); } for(k = 1;k<(i*2);k++){ printf("%d",k); } printf("

"); } getch(); return 0; }

登入後複製

輸出

編寫一個在C語言中列印數字模式的程序

#

以上是編寫一個在C語言中列印數字模式的程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!