This post is completed by 10 users
|
Add to List |
540. find the Arithmetic Progression sequence
Given a first element a and common difference d of an arithmetic progression. Write a program to print the first n elements of progression.
wiki:
An arithmetic progression or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant. For instance, the sequence 5, 7, 9, 11, 13, 15, . . . is an arithmetic progression with a common difference of 2.
Example:
a = 1, d = 2, n = 5 output: 1 3 5 7 9 a = 2, d = 3, n = 4 output: 2 5 8 11
Solution:
Earlier we have seen - Find Nth-term of an arithmetic progression We will use the same logic here. Iterate from i = 1 to n and for each i, print a + (n-1)*d.
Output:
a = 1, d = 2, n = 5 1 3 5 7 9 a = 2, d = 5, n = 7 2 7 12 17 22 27 32
Reference: wiki