Series GP
PROBLEM :
Given the first 2 terms of Geometric Series tell the nth term of the series. Ex. floor(2.3) =2 and floor(-2.3) = -3.
Input:
First line contains an integer, the number of test cases 'T'. Each test case in its first line should contain two positive integer a and b (First 2 terms of GP). In the second line of every test case it contains of an integer N.
Output:
In each seperate line print the Nth term of the Geometric Progression. Print the floor of the answer.
Constraints:
1<=T<=30
-100<=a<=100
-100<=b<=100
1 <= N <= 5
Example:
Input:
2
2 3
1
1 2
2
Output:
2
2
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<math.h>
int Series_GP(int ,int ,int ) ;
int main()
{
int t,n,a,b ;
cin>>t ;
while(t--)
{
cin>>a>>b ;
cin>>n ;
cout<<Series_GP(a,b,n) ;
cout<<endl ;
}
return 0;
}
int Series_GP(int a,int b,int n)
{
float r ;
r=(float)b/a ;
float k=(float)a*pow(r,n-1) ;
;
return floor(k) ;
}
---------------------------------------------------------------------------------
Given the first 2 terms of Geometric Series tell the nth term of the series. Ex. floor(2.3) =2 and floor(-2.3) = -3.
Input:
First line contains an integer, the number of test cases 'T'. Each test case in its first line should contain two positive integer a and b (First 2 terms of GP). In the second line of every test case it contains of an integer N.
Output:
In each seperate line print the Nth term of the Geometric Progression. Print the floor of the answer.
Constraints:
1<=T<=30
-100<=a<=100
-100<=b<=100
1 <= N <= 5
Example:
Input:
2
2 3
1
1 2
2
Output:
2
2
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<math.h>
int Series_GP(int ,int ,int ) ;
int main()
{
int t,n,a,b ;
cin>>t ;
while(t--)
{
cin>>a>>b ;
cin>>n ;
cout<<Series_GP(a,b,n) ;
cout<<endl ;
}
return 0;
}
int Series_GP(int a,int b,int n)
{
float r ;
r=(float)b/a ;
float k=(float)a*pow(r,n-1) ;
;
return floor(k) ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment