Why output is always 0?(JAVA) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why output is always 0?(JAVA)

I am making a program for finding the volume of a cone by taking radius and height of it as inputs. But the output for volume is always 0 no matter whatever the input for radius and height is. This is my program import java.util.*; class volume { public static void main(String args[]) { Scanner in=new Scanner(System.in); int r,h; double v=0.0; System.out.println("Enter radius and height"); r=in.nextInt(); h=in.nextInt(); v=(1/3)*(22/7)*r*r*h; System.out.println("Volume of cone="+v); } }

27th Jan 2019, 1:16 PM
Dhruv K
Dhruv K - avatar
3 Answers
+ 12
Integer/integer will give an integer in java, the decimal will be removed & integer value will be returned.
27th Jan 2019, 2:29 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 5
1/3 uses integer division as both operands are integers (1/3 == 0). Similarly, 22/7 == 3. Use 1.0/3 and 22.0/7.
27th Jan 2019, 1:50 PM
Diego
Diego - avatar
+ 4
Try it like this. I think the divisions are integers in your code. v=((double)1/3)*((double)22/7)*r*r*h;
27th Jan 2019, 1:49 PM
Paul
Paul - avatar