Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to calculate area of Circle in Java?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 28-11-2009
Member
 
Join Date: May 2009
Posts: 1,187
How to calculate area of Circle in Java?
  

Hi,
I am second year computer engineer student. As per our syllabus we have java language. In our last exam in java paper one program is asked like How to calculate Area of Circle? I used various method but unable to write right program. That's why I decide to take help of you. If you know how to write this program then please give me.

Reply With Quote
  #2  
Old 28-11-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: How to calculate area of Circle in Java?

Code:
import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class CircleArea
 {

public static void main(String[] args)
 {
 
int r = 0;

System.out.println(" enter radius ");
try

{

BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));

r = Integer.parseInt(br1.readLine());

}

catch(NumberFormatException ne)

{

System.out.println("Invalid radius value" + ne);

System.exit(0);

}

catch(IOException ioe)

{

System.out.println("IO Error :" + ioe);

System.exit(0);

}
double a = Math.PI * r * r;

System.out.println("Area of  circle is " + a);

}
}
Reply With Quote
  #3  
Old 28-11-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: How to calculate area of Circle in Java?

Hey I am not writing any program for you rather I give you logic behind it. You have to use following formula to calculate area of circle.

The formula is as follows:

Area of circle=Pi *r*r

where r=radius of circle,pi=3.14

You have to take radius from the user and save it in "r".
Reply With Quote
  #4  
Old 28-11-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: How to calculate area of Circle in Java?

Code:
import java.io.*;
class AreaOfCircle
{
  public static void main(String[] args){
    int radius=0;
           try{
        BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter Radius of Circle  : ");
        radius = Integer.parseInt(br2.readLine());
        double area1 = java.lang.Math.PI*radius*radius;
        System.out.println("Area of Circle : "+area1);
        
      }
      catch(Exception e){
        System.out.println("Error : "+e);
      }        
  }
}
Reply With Quote
  #5  
Old 28-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: How to calculate area of Circle in Java?

This is one of the simplest and easiest progran.


Code:
class StaticCircle 
{
  public double r;
  double  pi=3.14;
  public void circle(double r)
  {
    this.r=r;

    }
    public double area()
    {
    double a1 = r*r;
    double piV= a1*pi;
    return piV;
    }
    public static void main(String[] args) 
    {
    StaticCircle  sCircle  = new StaticCircle ();
    sCircle.circle(10);
    double t = sCircle.area();
    System.out.println("The area is " + t);
  }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to calculate area of Circle in Java?"
Thread Thread Starter Forum Replies Last Post
How to calculate simple interest in java ? xanix Software Development 7 08-12-2011 11:47 PM
Program to calculate days difference between two dates in java. KADRI Software Development 5 16-03-2011 06:22 PM
micromax Q3 Vs Fly Circle Truster Portable Devices 10 27-03-2010 08:22 AM
How to Calculate Process Time in Java? Bhardwaj Software Development 3 08-12-2009 12:25 PM
How to draw a circle using TI 89 Bilal Windows Software 3 20-06-2009 11:55 AM


All times are GMT +5.5. The time now is 03:42 PM.