Arithmetic Progression
Coding Problem Keys
Arithmetic Progression
Problem Statement
Given the second and the third terms of an AP (-10⁶ <= a₂, a₃ <= 10⁶), find the nth (1 <= n <= 1000) term of the sequence.
Input Specification
Input1: Second element of series (Integer)
Input2: Third element of series (Integer).
Input3: Total number of elements in the series (Integer).
Output Specification
Return the nth element of the series.
Example 1
input1: 1
input2: 2
input3: 4
Output 1
output: 3
Explanation
a₂ = 1, a₃ = 2, n = 4 d = 1, aₙ = a₄ = 3 (d refers to the common difference between adjacent terms in an arithmetic progression).
Example 2
input1: 5
input2: 8
input3: 4
Sample Output 2
Output: 11
Explanation
a₂ = 5, a₃ = 8, n = 4 d = 3, aₙ = a₄ = 11 (d refers to the common difference between adjacent terms in an arithmetic progression).
Solution
Programming Language: Python 3 Language
a2=int(input()) a3=int(input()) n=int(input()) d=a3-a2 a=a2-d print(a+(n-1)*d)# Published By PKJCODERS
Programming Language: C Language
#include <stdio.h> int NthTerm(int a2, int a3, int n){ int d,a; d=a3-a2; a=a2-d; return a+(n-1)*d; } int main(){ int a2, a3, n;scanf("%d%d%d", &a2, &a3, &n); printf("%d",NthTerm(a2, a3, n)); }// Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments