Java Program to Find the Perimeter of a Circle, Rectangle and Triangle

This is a Java Program to Find the Perimeter of a Circle, Rectangle and Triangle.
formula:
Perimeter of circle=2*pi*r
Perimeter of rectangle=2*(l+b)
Perimeter of triangle=(s1+s2+s3)

Enter the radius of the circle, length and breadth of the rectangle and three sides of the triangle as inputs. Calculate the perimeter by using formula and get the desired output.

Here is the source code of the Java Program to Find the Perimeter of a Circle, Rectangle and Triangle. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Perimeter 
  3. {
  4.     int r, l, b, s1, s2, s3;
  5.     double pi = 3.14,perimeter;
  6.     Scanner s = new Scanner(System.in);
  7.     void circle()
  8.     {
  9.         System.out.print("Enter radius of circle:");
  10.         r = s.nextInt();
  11.         perimeter = 2 * pi * r;
  12.         System.out.println("Perimeter of circle:"+perimeter);
  13.     } 
  14.     void rectangle()
  15.     {
  16.         System.out.print("Enter length of rectangle:");
  17.         l = s.nextInt();
  18.         System.out.print("Enter breadth of rectangle:");
  19.         b = s.nextInt();
  20.         perimeter = 2 * (l + b);
  21.         System.out.println("Perimeter of rectangle:"+perimeter);
  22.     }
  23.     void triangle()
  24.     {
  25.         System.out.print("Enter length of first side of triangle:");
  26.         s1 = s.nextInt();
  27.         System.out.print("Enter length of second side of triangle:");
  28.         s2 = s.nextInt();
  29.         System.out.print("Enter length of third side of triangle:");
  30.         s3 = s.nextInt();
  31.         perimeter = s1 + s2 + s3;
  32.         System.out.println("Perimeter of triangle:"+perimeter);
  33.     }
  34.     public static void main(String[] args) 
  35.     {
  36.         Perimeter obj = new Perimeter();
  37.         obj.circle();
  38.         obj.rectangle();
  39.         obj.triangle();
  40.     }
  41. }

Output:

$ javac Perimeter.java
$ java Perimeter
 
Enter radius of circle:4
Perimeter of circle:25.12
Enter length of rectangle:5
Enter breadth of rectangle:6
Perimeter of rectangle:22.0
Enter length of first side of triangle:3
Enter length of second side of triangle:4
Enter length of third side of triangle:5
Perimeter of triangle:12.0

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.