C语言 从a到b的数的乘法

nc1teljy  于 2023-06-21  发布在  其他
关注(0)|答案(2)|浏览(164)

我需要写一个算法,从a到b的数字相乘,而不需要输入(scanf)。像这样:

a = 2;
b = 6;

2 * 3
2 * 4
...
2 * 6

我有我的算法:

void main()
{
    int dist = 1;
    int a = 2;
    int b = 5;
    for (int i = a; a <= b; a++) {
        printf("%d", a * a++);
    }
}

但它不能正常工作

f2uvfpb9

f2uvfpb91#

这是因为在上面的例子中,a(a++)增加了两倍。另外,您还混淆了ai。正确的一个是:

int a = 2;
int b = 5;
for (int i = a; i <= b; i++)
{
    printf("%d * %d = %d\n", a, i, a * i);
}

它打印:
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10

lnvxswe2

lnvxswe22#

int main() {
int a = 2;
int b = 6;
int result = 1;  // Initialize the result variable to 1 for multiplication

// Perform multiplication from a to b
for (int i = a; i <= b; i++) {
    result *= i;
}

printf("The multiplication result from %d to %d is: %d\n", a, b, result);

return 0;

}

相关问题