# Arithmetic sequence C language

2022-11-24 21:34:12

# Title of topic: Arithmetic sequence

Time limit:1000msMemory limit:256M

Description

An arithmetic sequence is a sequence that can be expressed as a, a+b, a+2b,..., a+nb (n=0,1,2,3,...).In this problem a is a non-negative integer and b is a positive integer.Now three integers are given, representing the first item a, the last item and the tolerance b of the arithmetic sequence respectively, and the sum of the sequence is calculated.

Enter a description:

Enter three integers separated by spaces.The first number is the first item, the second number is the last item, and the third number is the tolerance.

Output Description:

The output occupies one line and contains an integer, which is the sum of the arithmetic progression.If the given three integers cannot form an arithmetic progression, return -1.

Example

Example 1

Enter 2 11 3

Output 26

Tips

none

``#include int solution(int a, int n, int b){int result=0;int i;int t=n;int flag=a;// TODO: write code hereif(a<0||b<=0){return -1;}if(a==t)return a;for(i=b;at){return -1;}}while(result){flag=flag+b;a=a+flag;--result;}return a;}int main() {int m;int n;int p;scanf("%d", &m);scanf("%d", &n);scanf("%d", &p);int result = solution(m, n, p);printf("%d", result);return 0;}``

The test questions are from CSDN

https://chowdera.com/2022/328/202211242126584776.html