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

PKJ said…
Thank You

Popular Posts

Top Scoring Batsman - TEST ODI 20-20

Largest Possible Number With Digits

Highly Profitable Months

Minimum Sum of Non-Negative Elements

Count Embedded Integers in String

String - Vowels Position Sum

Desktop Products