### Solutions for Skillrack CSE1002 Inlab 4

Total time : 20 mins

Challenges : 1

### Question (Arithmetic Progression)

An Arithmetic Progression(AP) is described by the first term ‘f’ and the common difference’. The n-th term of an AP is described by a_n= f+(n-1)d. The progression 1,4,9 .. is an AP. Given the f,d n, k, p , q, Develop a program in C++ using the class, to represent an arithmetic progression and provide methods to compute:

(i) n-th term of the arithmetic progression

(ii) r such that a_r=k. If there is no r such that a_r=k, the program should print zero.

(iii) Absolute Difference between a_p and a_q

Input Format:

First line contains the first term of the AP

Next line contains the common difference d

Next line contains the value of k

Next line contains the value of p

Next line contains the value of q

Output Format:

First line should contain the nth of the arithmetic progression

Next line should contain the value of ‘r’

Next line should contain the absolute difference between a_p and a_q

Boundary Conditions:

a , d are integers

n,p,q are all positive integers

### Solution

#include

#include

using namespace std;

class apr

{

int first;

int diff;

public:

void get();

int compute_term(unsigned int n);

int find_r(int k);

int abs_diff(unsigned int p,unsigned int q);

};

void apr :: get() { cin>>first>>diff; } int apr :: compute_term(unsigned int n) { return(first+(n-1)*diff); } int apr :: find_r(int k) { if((k-first)%diff==0) return(((k-first)/diff)+1); else return(0); } int apr :: abs_diff(unsigned int p,unsigned int q) { return(abs((p-q)*diff)); }

int main()

{

apr ap1;

unsigned int n,p,q;

int k;

ap1.get();

cin>>n;

cout<<ap1.compute_term(n)<<endl; cin>>k;

cout<<ap1.find_r(k)<<endl; cin>>p>>q;

cout<<ap1.abs_diff(p,q)<<endl;

}

### Input

Array : 2 4 6 8 10 ... INPUT : 2 2 5 6 3 2

### Output

OUTPUT : 10 3 2

### Note

There might be some changes in the code so please be updated as I do not have the actual Test cases.

Thank you a lot bro

LikeLike