1
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>

int main()
{   float t=1/2;
    printf("\n%.4f",t);
    return 0;
  }

i'm trying to write a program where this section of the code is needed. The program gives wrong output and ive identified the section which is causing the problem. Instead of 0.500000 im getting 0.00000.In this program ive used constants to find whether its working or not. cant seem to find the problem. please help. Thanks.

  • "The program gives wrong output" - always consider the program gives exactly the output your code should generate! If that's not the output **you want**, you wrote the wrong code. Use a debugger and read about the language featgures you use. Before asking. – too honest for this site Jun 19 '17 at 17:07

1 Answers1

1

I'll link you back to Why I am getting zero in float expressions like 1/2? which has the answer.

TLDR: When you do 1/2, you're doing integer division instead of float division and getting 0. Try either 1.0/2, 1/2.0, or 1.0/2.0.

ZeldaZach
  • 478
  • 1
  • 9
  • 18