C、java、go以及python语言打印九九乘法表

发布时间 2023-09-06 16:34:43作者: wbnyua

后续会更新其他语言

C语言

#include <stdio.h>

int main() {
    for(int row = 1; row <= 9;row++){
        for(int col = 1; col <= row;col++){
            printf("%d x %d = %2d\t", row, col, row*col);
        }
        printf("\n");
    }
    return 0;
}

Java语言

public class Main {
    public static void main(String[] args) {
        for (int row = 1; row <= 9; row++) {
            for (int col = 1; col <= row; col++) {
                System.out.printf("%d x %d = %2d\t",row,col,row*col);
            }
            System.out.println();
        }
    }
}

go语言

package main

import "fmt"

func main() {
	for row := 1; row <= 9; row++ {
		for col := 1; col <= row; col++ {
			fmt.Printf("%d x %d = %2d\t", row, col, row*col)
		}
		fmt.Println()
	}
}

python语言

def format_num(data):
    if len(data) <= 1:
        return " " + data
    return data


if __name__ == '__main__':
    result = ''
    for row in range(1, 10):
        for col in range(1, row + 1):
            result += str(row) + " x " + str(col) + " = " + format_num(str(row * col)) + "\t"
        result += "\n"
    print(result)