Question -
Given the first element and d of an Arithmetic progression , you have to find the nth element of that progression.
Example -
Input : a=1, d=2, n=6
Ouptut : 11
Approach -
We know the basic formula to find the nth element of any arithmetic progression is Tn= (a+(n-1)*d)
Implementation -
//To find the nth element of any Arithmetic progression
#include<iostream>
using namespace std;
int print(int a,int d,int n)
{
int Tn;
Tn=a+(n-1)*d;
return Tn;
}
int main()
{
int a,d,n;
cout<<"\t\t\tArithmetic progression "<<endl;
cout<<"Enter a : ";cin>>a;
cout<<"Enter d : ";cin>>d;
cout<<"enter n(term) : ";cin>>n;
int Tn=print(a,d,n);
cout<<"nth term of this progression is : "<<Tn<<endl;
return 0;
}
Time complixity - O(1)
Comments
Post a Comment