Program to find Perimeter / Circumference of Square and Rectangle - GeeksforGeeks

06 Apr, 2021
The circumference of a figure is the sum of all the side lengths. To calculate the circumference of square, length of one of the side is required as all sides are equal. To calculate the circumference of rectangle, length and breadth of rectangle is required. Circumference of a Square:  
The circumference of a square is given by the formula:  
C = 4 * a
where a is the side length.
Examples :  
input: 4
output: 16

input: 3
output: 12
 
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find// Circumference of a square#include <bits/stdc++.h>using namespace std; int Circumference(int a){    return 4 * a;} // Driver Codeint main(){    int a = 5;    cout << "Circumference of"         <<" a square is "         << Circumference(a);    return 0;} // This code is contributed// by mohitw16
// Java program to find// Circumference of a square import java.io.*;class GFG{    int Circumference(int a)    {        return 4 * a;    }     // Driver code    public static void main(String args[])    {        GFG obj = new GFG();        int a = 5;        System.out.println("Circumference of " +                                "a square is " +                          obj.Circumference(a));    }} // This code is contributed// by Anshika Goyal.
# Python3 Program to find# Circumference of a square def Circumference(a):        return (4 * a) # Driver codea = 5c = Circumference(a)print("Circumference of a " +       "square is % d" % (c))
// C# program to find Circumference// of a squareusing System; class GFG{     static int Circumference(int a)    {        return 4 * a;    }     // Driver Code    public static void Main()    {        int a = 5;         Console.WriteLine("Circumference" +                       " of a square is " +                         Circumference(a));    }} // This code is contributed by vt_m.
<?php// PHP program to find// Circumference of a square function Circumference($a){    return 4 * $a;} // Driver Code$a = 5;echo "Circumference of a ".              "square is ",         Circumference($a); // This code is contributed by ajit?>
<script> // Javascript program to find// Circumference of a squarefunction Circumference(a){    return 4 * a;} // Driver Code    let a = 5;    document.write("Circumference of"        +" a square is "        +Circumference(a)); // This code is contributed by Manoj </script>
Output :  
Circumference Of a square is 20
Circumference of a rectangle:  
The circumference of a rectangle is given by the formula:  
C = 2 * (l + W)
where l is the length and W is the width.
Examples :  
input: 2 4
output: 12

input: 4 6
output: 20
 
C++
Python3
Java
C#
PHP
Javascript
// C++ Program to find// Circumference of a rectangle#include <iostream>using namespace std; int Circumference(int l, int w){    return (2 * (l + w));} // Driver codeint main(){    int l = 8, w = 4;     int c = Circumference(l, w);     cout << "Circumference of a"         << " rectangle is "         << c << endl;     return 0;} // This code is contributed by vt_m.
# Python Program to find# Circumference of a rectangle def Circumference(l, w):        return (2 * (l + w)) # Driver codel = 8w = 4c = Circumference(l, w)print("Circumference of a" +  " rectangle is % d" % (c))
// java Program to find// Circumference of a rectangleimport java.io.*; class GFG{     static int Circumference(int l,                             int w)    {        return (2 * (l + w));    }     // Driver code    static public void main(String[] args)    {        int l = 8, w = 4;         int c = Circumference(l, w);         System.out.println("Circumference of " +                         "a rectangle is " + c);    }} // This code is contributed by vt_m.
// C# Program to find// circumference of a rectangleusing System; class GFG{     static int Circumference(int l,                             int w)    {        return (2 * (l + w));    }     // Driver code    static public void Main()    {        int l = 8, w = 4;         int c = Circumference(l, w);         Console.WriteLine("Circumference of " +                        "a rectangle is " + c);    }} // This code is contributed by vt_m.
<?php// Php Program to find// Circumference of a rectangle function Circumference($l,$w){    return (2 * ($l + $w));} // Driver code$l = 8; $w = 4; $c = Circumference($l, $w); echo "Circumference of a ". "rectangle is " ,$c ,"\n"; // This code is contributed by aj_36.?>
<script>// javascript Program to find// Circumference of a rectangle    function Circumference(l , w)    {        return (2 * (l + w));    }     // Driver code        var l = 8, w = 4;        var c = Circumference(l, w);        document.write("Circumference of " + "a rectangle is " + c);     // This code is contributed by aashish1995</script>
Output :  
Circumference of a rectangle is 24
 
vt_m
jit_t
mank1083
aashish1995
area-volume-programs
square-rectangle
Geometric
Mathematical
School Programming
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Circle and Lattice Points
Program for distance between two points on earth
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Queries on count of points lie inside a circle
Convex Hull | Set 2 (Graham Scan)
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7