Program to find the perimeter of a square given its area
Last Updated :
18 Jan, 2024
Write a program to find the perimeter of a square given its area.
Examples:
Input: Area = 36 square units
Output: Perimeter = 24 units
Explanation: Since the area of the square is 36 square units, so the side of square will be 6 units. Therefore, the perimeter of the square will be 4 * 6 = 24 units.
Input: Area = 25 square units
Output: Perimeter = 20 units
Explanation: Since the area of the square is 25 square units, so the side of square will be 5 units. Therefore, the perimeter of the square will be 4 * 5 = 20 units.
Approach: To solve the problem, follow the below idea:
The area of a square is given by Area=side×side. We can find the side length by taking the square root of the area. Once the side length is known, the perimeter can be calculated as Perimeter=4×side.
Step-by-step algorithm:
- Calculate the side length using the square root of the area.
- Calculate the perimeter using the formula Perimeter=4 × side.
- Print the calculated perimeter.
Below is the implementation of the algorithm:
C++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double area, side, perimeter;
area = 16;
side = sqrt (area);
perimeter = 4 * side;
cout << "Perimeter: " << perimeter << endl;
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
int main()
{
double area, side, perimeter;
area = 16;
side = sqrt (area);
perimeter = 4 * side;
printf ( "Perimeter: %.2f\n" , perimeter);
return 0;
}
|
Java
import java.util.Scanner;
public class SquarePerimeter {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
double area, side, perimeter;
area = 16 ;
side = Math.sqrt(area);
perimeter = 4 * side;
System.out.println( "Perimeter: " + perimeter);
}
}
|
Python3
import math
area = 16
side = math.sqrt(area)
perimeter = 4 * side
print (f "Perimeter: {perimeter}" )
|
C#
using System;
class Program {
static void Main()
{
double area, side, perimeter;
area = 16;
side = Math.Sqrt(area);
perimeter = 4 * side;
Console.WriteLine( "Perimeter: " + perimeter);
}
}
|
Javascript
let area = 16;
let side = Math.sqrt(area);
let perimeter = 4 * side;
console.log(`Perimeter: ${perimeter}`);
|
Time Complexity: O(log N), because it takes O(logN) time to calculate the square root of N.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...