gpt4 book ai didi

c - 如何计算乘法表的总和

转载 作者:行者123 更新时间:2023-11-30 14:33:29 27 4
gpt4 key购买 nike

我已经生成了表格,想要找到表格的总和。

代码如下:

#include <stdio.h>

int main()
{
int no, i, mult, sum;

printf("Enter the number you wish to multiply: \n");
scanf("%d", &no);

printf("Enter how many times you want to multiply the number: \n");
scanf("%d", &mult);

printf("Multiplication is as follows: \n");

for( i = 1; i <= mult; i++ )
{
printf("%d X %d = %d\n", no, i, i * no);
}
}

如何获取整个表格的总和?

最佳答案

在开始时初始化 sum = 0,然后每次打印一个值时将其添加到总和中。

#include <stdio.h>
#include <stdlib.h>
#include<conio.h>

int main()
{
int no,i,mult,sum=0;

printf("Enter the number you wish to multiply: \n");
scanf("%d", &no);

printf("Enter how many times you want to multiply the number: \n");
scanf("%d", &mult);

printf("Multiplication is as follows: \n");

for(i = 1; i <= mult; i++)
{
printf("%d X %d = %d\n", no,i,i*no);
sum = sum + i*no
}
printf("sum = %d", sum);
}

关于c - 如何计算乘法表的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59286173/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com