Results 1 to 5 of 5

Thread: How to calculate area of Circle in Java?

  1. #1
    Join Date
    May 2009
    Posts
    1,191

    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.

  2. #2
    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);
    
    }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    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".

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    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);
          }        
      }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    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);
      }
    }

Similar Threads

  1. How to calculate simple interest in java ?
    By xanix in forum Software Development
    Replies: 7
    Last Post: 08-12-2011, 11:47 PM
  2. Replies: 5
    Last Post: 16-03-2011, 06:22 PM
  3. micromax Q3 Vs Fly Circle
    By Truster in forum Portable Devices
    Replies: 10
    Last Post: 27-03-2010, 08:22 AM
  4. How to Calculate Process Time in Java?
    By Bhardwaj in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 12:25 PM
  5. How to draw a circle using TI 89
    By Bilal in forum Windows Software
    Replies: 3
    Last Post: 20-06-2009, 11:55 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,709,138.39612 seconds with 16 queries