Question -
Given the value of a, d and number of terms(n). you have to print the Arithmetic progression.
Example -
Input : a=1, d=1 ,n=5
Output : 1, 2, 3, 4, 5
Input : a=2, d=2 ,n=10
Output : 2, 4, 6, 8, 10, 12, 14, 16, 18, 20
Approach -
nth term of any Arithmetic progression is a+(n-1)*d.
Implementation -
//To print Arithmetic progression
#include<iostream>
using namespace std;
void print(int a,int d,int n)
{
for(int i=1;i<=n;i++){
cout<<(a+(i-1)*d)<<" ";
}
}
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;
print(a,d,n);
return 0;
}
Time complixity - O(n)
Comments
Post a Comment