In this tutorial we will see how to calculate area and circumference of circle in Java. There are two ways to do this:
1) With user interaction: Program will prompt user to enter the radius of the circle
2) Without user interaction: The radius value would be specified in the program itself.
Program 1:
/** * @author: BeginnersBook.com * @description: Program to calculate area and circumference of circle * with user interaction. User will be prompt to enter the radius and * the result will be calculated based on the provided radius value. */ import java.util.Scanner; class CircleDemo { static Scanner sc = new Scanner(System.in); public static void main(String args[]) { System.out.print("Enter the radius: "); /*We are storing the entered radius in double * because a user can enter radius in decimals */ double radius = sc.nextDouble(); //Area = PI*radius*radius double area = Math.PI * (radius * radius); System.out.println("The area of circle is: " + area); //Circumference = 2*PI*radius double circumference= Math.PI * 2*radius; System.out.println( "The circumference of the circle is:"+circumference) ; } }
Output:
Enter the radius: 1 The area of circle is: 3.141592653589793 The circumference of the circle is:6.283185307179586
Program 2:
/** * @author: BeginnersBook.com * @description: Program to calculate area and circumference of circle * without user interaction. You need to specify the radius value in * program itself. */ class CircleDemo2 { public static void main(String args[]) { int radius = 3; double area = Math.PI * (radius * radius); System.out.println("The area of circle is: " + area); double circumference= Math.PI * 2*radius; System.out.println( "The circumference of the circle is:"+circumference) ; } }
Output:
The area of circle is: 28.274333882308138 The circumference of the circle is:18.84955592153876
Why r we using static scanner sc? What’s Math.PI?
What do you think Math.PI is, you fucking idiot. It’s pi or 3.14.
Hey asshole!
It’s good to see you bashing your fabulous vocabulary and knowledge at others. But I was merely confused about the function. So just STFU and get lost instead of swearing at others, you filthy pig. Never asked you for your opinion so stop acting like a fucking retard. Get a life.
there is no stupid questions, there is only stupid answers.
1.Write a program to create an integer array that allows user to enter numbers , display the numbers in forward and reverse order then find their sum and average. Also display the element at each index.
i do not have much programming knowledge.
can someone tell me the use of following lines?
1> Scanner sc = new Scanner(System.in);
why we use system.in ??? what does it mean? and what is it’s use?
2> double radius = sc.nextDouble();
Why nextDouble? what does it mean? and what is it’s use?
Hi Dear! Riya
System.in is an InputStream which is typically connected to keyboard input of console programs. System.in is not used as often since data is commonly passed to a command line Java application via command line arguments, or configuration files. In applications with GUI the input to the application is given via the GUI. This is a separate input mechanism from Java IO.
How do I round it off
Y we shouldn’t call the method can u explain in detail