I need to calculate the sum of the number a (double) to the power of n (int) in C programming language. Without the pow function!
I have done it this way, but it is very complex, I want something simpler.
If you know how to make this via Java or C++ or Pascal please reply too)
1/a2 + 1/a4 + 1/a6 ... + 1/a2n.
program sum;
var i, n: integer;
s, a, x: real;
f: boolean;
begin
repeat
write('n = '); readln(n);
if n < 1 then writeln('Error: n <= 0, reenter.')
until n >= 1;
repeat
write('a = '); readln(a);
if a = 0 then writeln('Error: a = 0, reenter.')
until a <> 0;
s := 0;
x := 1;
f := true;
for i := 1 to n do
begin
x := x / a / a;
if x = 0
then begin
writeln('Float rounding error.');
f := false;
break
end;
s := s + x
end;
if f then writeln('s = ', s);
readln
end;