Solved

1/2+3/2

Posted on 2000-04-09
1
157 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now