ZeroJudge B971: Arithmetic Sequence

Given the first term, last term, and common difference of an arithmetic sequence, please output this arithmetic sequence.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The input consists of only one line containing three integers: the first term, last term, and common difference of the arithmetic sequence.Output the arithmetic sequence, with each term separated by a space.
1 9 21 3 5 7 9

Thought Process

Use a while loop with the termination condition being the starting point equal to the end point. Avoid using less than as the termination condition because negative numbers may be present.

Sample Code-ZeroJudge B971: Arithmetic Sequence

#include <iostream>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int start, finish, change;
    cin >> start >> finish >> change;
    while (start != finish)
    {
        cout << start << " ";
        start += change;
    }
    cout << start << "\n";
}

//Z.O.J. B971
//Dr. SeanXD

Comments