Solved

1/2+3/2

Posted on 2000-04-09
1
162 Views
Last Modified: 2010-04-02
double i;
i=1/2+3/2;
cout<<i;

can you explain why the o/p is 1?
0
Comment
Question by:skundu
1 Comment
 
LVL 22

Accepted Solution

by:
nietod earned 5 total points
ID: 2698281
the probkem is that you are doing the math with integer values which will be truncated.   So for example 1/2 truncates to an integer value of 0 and 3/2 (1.5) truncates to a value of 1.  so you get

i = 0 + 1;

which is 1.

Try specifyihg floating point numbers instead, like

i=1.0/2.0+3.0/2.0;
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question