Z
Z
Zhenia Bel2021-10-23 22:55:44
C++ / C#
Zhenia Bel, 2021-10-23 22:55:44

Why doesn't my float count correctly?

#include <stdio.h>
#include <Windows.h>

int main() {
  int i = 1, j = 1, s1 = 1, s2 = 1;
  float y;
  y = 1. / (s1 + s2);

  while (i < 50)
  {
    i++;
    s1 += i;
    while (j<10)
    {
      j++;
      s2 += j;
    }
    
  }
  printf("Y = %g", y);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2021-10-23
@GavriKos

At least they spelled it wrong. The output of the program is 0.5. 1 \ (1 + 1) like just 0.5 should be. And what did you expect?
By the way, the loop is useless - it does not affect y in any way

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question