- 4

Solve the problem

Given real number a, natural number n. Calculate the expression shown on image:(1/a)+(1/a^2)+(1/a^4)+....+(1/a^2n) Example: Input: 2 1 Output: 0.75 Example: Input: 2.1 2 Output: 0.754367 #include <iostream> #include <cmath> using namespace std; int main() { double a; int n; double p=0; cin >> a; cin >> n ; for ( int i=1; i<= 2*n; i*=2 ) // in first time it will be " i=1 " // after,"i" will be multiplied by " 2 " p =p + 1/pow(a,i); cout << p; return 0; }

19th Sep 2020, 12:05 PM
Azat Malgazhdar
Azat Malgazhdar - avatar
1 Answer
+ 3
Your code is working according to your mentioned input output format what error you facing.
19th Sep 2020, 12:23 PM
AS Raghuvanshi
AS Raghuvanshi - avatar