C中算术级数系列的输出不正确

C中算术级数系列的输出不正确,c,C,这是我的作业: 这是我的代码: #include <stdio.h> int main() { int firstTerm, commonDifference, numberOfTerms, arithmeticSeries, i; printf("Please enter the first term: "); scanf("%d", &firstTerm); printf("Please

这是我的作业:

这是我的代码:

#include <stdio.h>

int main() {

   int firstTerm, commonDifference, numberOfTerms, arithmeticSeries, i;

   printf("Please enter the first term: ");
   scanf("%d", &firstTerm);

   printf("Please enter the common difference: ");
   scanf("%d", &commonDifference);

   printf("Please enter the number of terms: ");
   scanf("%d", &numberOfTerms);

    for (i = 0; i < numberOfTerms; i++) {

        apSeries = firstTerm, firstTerm + commonDifference, firstTerm + (commonDifference * 2), firstTerm + (commonDifference * 3);

        printf("The arithmetic progression series is: %d", arithmeticSeries);
    
    }
}

该系列的每个元素都是
firstTerm+i*commonDifference
。这就是暗示想要告诉你的

你不能像图中显示的那样一次分配所有的参数,这只是演示了算术关系

    printf("The arithmetic progression series is:");
    for (i = 0; i < numberOfTerms; i++) {
        arithmeticSeries = firstTerm + i * commonDifference;
        printf(" %d", arithmeticSeries);
    }
    printf("\n");
printf(“算术级数是:”);
对于(i=0;i

或者您可以根据算术级数添加'commonDi

,您已经知道公式是:

n项值=第一个no+(n-1)*公共差

从您的代码来看,打印部分很好,这意味着用户输入部分。你的主要问题是循环。所以我所做的是

for(i=1; i<=noOfTerms; i++) {
arithmeticSeries = firstTerm + ((i-1)*commonDifference);
prinf("The arithmetic progression series is: %d\n", arithmeticSeries);
}
不仅输出是错误的,而且根据我的循环,输出的数量也是错误的。我从1开始,得到一个正数,Z+。因此,我的输出如下:

The arithmetic progression series is: 1
The arithmetic progression series is: 3
The arithmetic progression series is: 5
The arithmetic progression series is: 7

我希望这能回答你的问题

apSeries=firstTerm,firstTerm+commonDifference,firstTerm+(commonDifference*2),firstTerm+(commonDifference*3)看起来您不知道逗号运算符在C中的作用。这段代码甚至可以编译吗
apSeries
是一个未定义的变量。@kaylum是的,这是因为我只是为了这个问题更改了变量,但忘记了更改所有变量。很抱歉您应该以
\n
结束printf,这样所有内容就不在一行了。@DavidSchwartz他试图复制作业提示部分的示例公式。它仍然没有给出我想要的答案,因为它打印出来了:算术级数系列是:1算术级数系列是:3算术级数级数是:5算术级数是:7,但它比另一个好,所以我就选它。非常感谢。这不可能是这段代码的输出,我从循环中取出了消息的第一部分。
The arithmetic progression series is: -1
The arithmetic progression series is: 1
The arithmetic progression series is: 3
The arithmetic progression series is: 5
The arithmetic progression series is: 7
The arithmetic progression series is: 1
The arithmetic progression series is: 3
The arithmetic progression series is: 5
The arithmetic progression series is: 7