2013/09/28

九九乘法Code

九九乘法表是很常見的作業,常常出現在剛講完迴圈的時候讓大家練習一下

我們先來看看這個最基本的C語言版本:

#include <stdio.h>

int main () {
    int i, j;
    for (i = 1; i <= 9; i++)
        for (j = 1; j <= 9; j++)
            printf("%d x %d = %2d%s", i, j, i * j, j == 9 ? "\n" : ", ");
    return 0;
}


這是VB

Dim i As Integer, j As Integer
Dim output As String

For i = 1 To 9

    For j = 1 To 9
        Dim v As Integer
        v = i * j 
        output = output & CStr(i) & " * " & CStr(j) & IIf(v < 10, " =  ", " = ") & CStr(v) & IIf(j = 9, vbNewLine, ""))
    Next 
Next

Print output