You are on page 1of 5

9.Write a Program in JAVA to calculate area, perimeter and volume of shapes using functions.

import java.io.*;

public class calculate

public static void main(String args [])throws IOException

InputStreamReader read=new InputStreamReader(System.in);

BufferedReader in=new BufferedReader(read);

double r;int l,b,h;

System.out.println("enter the values of r");

r=Double.parseDouble(in.readLine());

System.out.println("enter the values of l");

l=Integer.parseInt(in.readLine());

System.out.println("enter the values of b");

b=Integer.parseInt(in.readLine());

System.out.println("enter the values of h");

h=Integer.parseInt(in.readLine());

calculate ob=new calculate();

double p=0;double p1=0;int q=0;int q1=0;int ra=0;int ra1=0;int t=0;float t1=0;int v=0;int c=0;

p=ob.area(r);

p1=ob.perimeter(r);

q=ob.area(l);

q1=ob.perimeter(l);

ra=ob.area(l,b);

ra1=ob.perimeter(l,b);
t=ob.area(b,h);

t1=ob.perimeter(l,b,h);

v=ob.volume(l,b,h);

c=ob.volume(l);

System.out.println("The area of circle "+p);

System.out.println("The perimeter of circle "+p1);

System.out.println("The area of square "+q);

System.out.println("The perimeter of square "+q1);

System.out.println("The area of rectangle "+ra);

System.out.println("The perimeter of rectangle "+ra1);

System.out.println("The area of triangle "+t);

System.out.println("The perimeter of triangle "+t1);

System.out.println("The volume of cuboid "+v);

System.out.println("The volume of cube "+c);

double area(double r)

return(3.14*r*r);

double perimeter(double r)

return(3.14*r*2);

int area(int l)

{
return(l*l);

int perimeter(int l)

return(4*l);

int area(int l,int b)

return(l*b);

int perimeter(int l,int b)

return(2*(l+b));

float area(float b,float h)

return((1/2)*b*h);

float perimeter(int l,int b,int h)

return(l+b+h);

int volume(int l,int b,int h)

return(l*b*h);
}

int volume(int l)

return(l*l*l);

OUTPUT
enter the values of r

35

enter the values of l

23

enter the values of b

23

enter the values of h

121

The area of circle 3846.5

The perimeter of circle 219.8

The area of square 529

The perimeter of square 92

The area of rectangle 529

The perimeter of rectangle 92

The area of triangle 2783

The perimeter of triangle 167.0

The volume of cuboid 64009


The volume of cube 12167

You might also like