My code works fine until I try to calculate the Diameter. I ran the program while only calculating the area and circumference and it works. But as soon as I try to calculate the diameter I keep getting an error.
package calccircumference;
import java.util.Scanner;
public class CalcCircumference {
public static void main(String[] args) {
Scanner input = new Scanner( System.in );
int radius;
System.out.print("Enter radius of circle: ");
radius = input.nextInt();
System.out.printf("Diameter is %f\n",( 2*radius ));
System.out.printf( "Area is %f\n", ( Math.PI * radius * radius ) );
System.out.printf("Circumference is %f\n",(2 * Math.PI * radius));
}
}
[enter image description here][1]
[1]: https://i.stack.imgur.com/2X0Dp.png
>Solution :
An integer multiplied by an Integer will give you an Integer so change to
System.out.printf("Diameter is %f\n",( 2.0 *radius ));
Or use
System.out.printf("Diameter is %d\n",( 2 *radius ));