Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
#include<conio.h>
#include<stdio.h>
void main()
{
 int a;
 float b;
 a=1/3+1/3+1/3;
 printf("%d",a);
 getch();
 return;
}





here the output is :0
instead of 1 why??????????????

What I have tried:

i have worked out this code in turbo c++
but the output is always 0 instead of 1 why???????
Posted
Updated 3-Jan-18 0:57am
v2

1 solution

Because 1/3 is computed using the integer division. try

C
#include <stdio.h>
int main()
{
  int a;
  float b;
  b = 1.0f / 3 + 1.0f / 3 + 1.0f / 3;
  a = (int) b;
  printf("%d, %f\n",a, b);
  getchar();
  return 0;
}


And, please, stop using turbo C (it was good about 30 years ago). Use a modern compiler.
 
Share this answer
 
v4
Comments
Peter_in_2780 3-Jan-18 6:57am    
You might still get 0! The expression for b will evaluate to something like 1.0 - (one lsb)
Amit Razdan 3-Jan-18 10:13am    
thank u very much sir.....
CPallini 3-Jan-18 18:29pm    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900