-4

I'm trying to make and print a series of the form 1/n; where n is a natural number.

int Number;
  float NumberInverse, NumberInverseNext;
  for ( Number = 1; Number < 1000; Number++)

if I try to print 'Number' after this, I get a series of natural numbers from 1-1000, as I'd expect. But if I do

NumberInverse = 1/Number;

and try to print NumberInverse, I get 0 as output. I'm not sure what I'm doing wrong and what I should be doing.

EDIT : This is not a duplicate of the question mentioned as even after changing

 NumberInverse = 1/Number;

to

NumberInverse = 1/(float)Number;

I can't get the series of 1/n which was the original question

  • Integer math. Both terms are `int`, so the math is `int`. Widen at least one term first `1/(float)Number;` – Elliott Frisch Sep 02 '18 at 09:56
  • You mention that you cannot get the series of 1/n, but it seems like a trivial error, like misplaced `printf`. To make your question really clear, post the whole code, including any statements that print information. Post also the output you get and the output you want to get. See also this: [mcve]. – anatolyg Sep 02 '18 at 12:30

1 Answers1

1

try casting, the answer in your case is an integer so you will get 0, try this:

int Number;
  float NumberInverse, NumberInverseNext;
  for ( Number = 1; Number < 1000; Number++) {
  NumberInverse = 1/(double)Number;
  printf("%f \n",NumberInverse);
  }
maha
  • 643
  • 5
  • 20