使用 for 迴圈列印乘法表的 C 程式
一個for 迴圈是一個重複控制結構,它讓你可以有效地編寫迴圈,使之可以執行特定次數。
演算法
下面是一個在 C 語言中使用 for 迴圈列印乘法表的演算法 −
Step 1: Enter a number to print table at runtime. Step 2: Read that number from keyboard. Step 3: Using for loop print number*I 10 times. // for(i=1; i<=10; i++) Step 4: Print num*I 10 times where i=0 to 10.
示例
以下是列印給定數字的乘法表的 C 程式 −
#include <stdio.h> int main(){ int i, num; /* Input a number to print table */ printf("Enter number to print table: "); scanf("%d", &num); for(i=1; i<=10; i++){ printf("%d * %d = %d
", num, i, (num*i)); } return 0; }
輸出
執行以上程式時,將產生以下結果 −
Enter number to print table: 7 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63 7 * 10 = 70
廣告