Program to find Perimeter / Circumference of Square and Rectangle
Last Updated :
07 Jan, 2024
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++
#include <bits/stdc++.h>
using namespace std;
int Circumference( int a)
{
return 4 * a;
}
int main()
{
int a = 5;
cout << "Circumference of"
<< " a square is "
<< Circumference(a);
return 0;
}
|
Java
import java.io.*;
class GFG
{
int Circumference( int a)
{
return 4 * a;
}
public static void main(String args[])
{
GFG obj = new GFG();
int a = 5 ;
System.out.println( "Circumference of " +
"a square is " +
obj.Circumference(a));
}
}
|
Python3
def Circumference(a):
return ( 4 * a)
a = 5
c = Circumference(a)
print ( "Circumference of a " +
"square is % d" % (c))
|
C#
using System;
class GFG
{
static int Circumference( int a)
{
return 4 * a;
}
public static void Main()
{
int a = 5;
Console.WriteLine( "Circumference" +
" of a square is " +
Circumference(a));
}
}
|
Javascript
<script>
function Circumference(a)
{
return 4 * a;
}
let a = 5;
document.write( "Circumference of"
+ " a square is "
+Circumference(a));
</script>
|
PHP
<?php
function Circumference( $a )
{
return 4 * $a ;
}
$a = 5;
echo "Circumference of a " .
"square is " ,
Circumference( $a );
?>
|
Output :Â
Â
Circumference Of a square is 20
Time complexity : O(1)Â
Auxiliary Space : O(1)
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++
#include <iostream>
using namespace std;
int Circumference( int l, int w)
{
return (2 * (l + w));
}
int main()
{
int l = 8, w = 4;
int c = Circumference(l, w);
cout << "Circumference of a"
<< " rectangle is "
<< c << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int Circumference( int l,
int w)
{
return ( 2 * (l + w));
}
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);
}
}
|
Python3
def Circumference(l, w):
return ( 2 * (l + w))
l = 8
w = 4
c = Circumference(l, w)
print ( "Circumference of a" +
" rectangle is % d" % (c))
|
C#
using System;
class GFG
{
static int Circumference( int l,
int w)
{
return (2 * (l + w));
}
static public void Main()
{
int l = 8, w = 4;
int c = Circumference(l, w);
Console.WriteLine( "Circumference of " +
"a rectangle is " + c);
}
}
|
Javascript
<script>
function Circumference(l , w)
{
return (2 * (l + w));
}
var l = 8, w = 4;
var c = Circumference(l, w);
document.write( "Circumference of " + "a rectangle is " + c);
</script>
|
PHP
<?php
function Circumference( $l , $w )
{
return (2 * ( $l + $w ));
}
$l = 8; $w = 4;
$c = Circumference( $l , $w );
echo "Circumference of a " .
"rectangle is " , $c , "\n" ;
?>
|
Output :Â
Â
Circumference of a rectangle is 24
Time complexity : O(1)Â
Auxiliary Space : O(1)Â
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...