Program to find area of circle, rectangle, triangle using different methods
Program:
import java.util.*;
class Area
{
double circle(int r){
return(3.14*r*r);
}
int rectangle(int l, int w){
return(l*w);
}
double triangle(int b, int h){
return(0.5*b*h);
}
}
class MethodDemo
{
public static void main(String args[])
{
int r,l,w,b,h;
Scanner sc=new Scanner(System.in);
Area a=new Area();
System.out.println("To calculate area of Circle, enter its radius");
r=sc.nextInt();
System.out.println("To calculate area of Reactangle, enter its length
and width");
l=sc.nextInt();
w=sc.nextInt();
System.out.println("To calculate area of Triangle, enter its base and
height");
b=sc.nextInt();
h=sc.nextInt();
System.out.println("Area of Circle: "+a.circle(r));
System.out.println("Area of Rectangle: "+a.rectangle(l,w));
System.out.println("Area of Triangle: "+a.triangle(b,h));
}
}
Output:
import java.util.*;
class Area
{
double circle(int r){
return(3.14*r*r);
}
int rectangle(int l, int w){
return(l*w);
}
double triangle(int b, int h){
return(0.5*b*h);
}
}
class MethodDemo
{
public static void main(String args[])
{
int r,l,w,b,h;
Scanner sc=new Scanner(System.in);
Area a=new Area();
System.out.println("To calculate area of Circle, enter its radius");
r=sc.nextInt();
System.out.println("To calculate area of Reactangle, enter its length
and width");
l=sc.nextInt();
w=sc.nextInt();
System.out.println("To calculate area of Triangle, enter its base and
height");
b=sc.nextInt();
h=sc.nextInt();
System.out.println("Area of Circle: "+a.circle(r));
System.out.println("Area of Rectangle: "+a.rectangle(l,w));
System.out.println("Area of Triangle: "+a.triangle(b,h));
}
}
Output:
Comments
Post a Comment