Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find sum of numbers C lang

I need to calculate the sum of the number a (double) to the power of n (int). Without pow function !!!!!
I will be very grateful.

I have done 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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1/a2 + 1/a4 + 1/a6 … + 1/a2n.

 
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.```

>Solution :

In C:

double bPow = 1;
double sum = 0
double b = a * a;

for (int i = 0; i < n; i++)
{
    bPow *= b;
    sum += 1 / bPow;
}

This gives you the sum of all 1/a^x for x = [2, 4, …, 2n] .

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading