Area of a Circle without new Class and Math Class-Example0009
This example is example for using Scanner class to get input from user and calculate area of a circle.Using of Class Math is also shown
Also casting from double to float is also shown
package com.swprogramdeveloper;
Keyword: java, aoc, scanner, user input, casting, math class
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
final float PI=3.14F;
Scanner askinput= new Scanner(System.in);
System.out.print("Please Enter Radius of the Circle:");
float radius= askinput.nextFloat();
//Using Mathematical expression
float area= PI * radius * radius;
System.out.print("Area of Circle(PIXRXR):");
System.out.println(area);
//Using Math Class
//Need Explicit Casting- Casting: changing types from double to float- Math.pow returns double
float area1= PI * (float)Math.pow(radius,2);
System.out.print("Area of Circle(Using Math Class for Power(PIXR*2):");
System.out.println(area1);
}
}
Output0009
==========
Please Enter Radius of the Circle:7
Area of Circle(PIXRXR):153.86002
Area of Circle(Using Math Class for Power(PIXR*2):153.86
Process finished with exit code 0
Comments
Post a Comment