Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Qadratic expression  RSS feed

 
awad saleh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi m i solved the quadratic expression manually on paper , i got the value as zero. but when i did with this program, m getting the value as 4 . why?

[added code tags]
[ July 24, 2007: Message edited by: Fred Rosenberger ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because 2/3 is 0! When you run the program, it should show that x is 0.

If you divide two integers in Java, the result is an integer; 2/3 is 0.666... which is truncated to 0. You need to make at least one of the numbers a double if you want the result to be a double -- i.e.,

double x = 2.0/3.0;
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!