This post is completed by 7 users
|
Add to List |
539. Find the Nth-term in a given arithmetic progression
Given a first element a and common difference d of an arithmetic progression. Write a program to find the nth term in that 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: 9 a = 2, d = 5, n = 4 output: 17
Solution:
The formula for finding the nth term in an arithmetic progression:
Nth term = a + (n-1)*d.
Output:
a = 1, d = 2, n = 5 5th term: 9 a = 2, d = 5, n = 4 4th term: 17
Reference: wiki
Also read: Find the Arithmetic Progression sequence