Program to find the side length of a square given its area
Last Updated :
18 Jan, 2024
Write a program to find the side length of a square with given area.
Examples:
Input: Area = 16 square units
Output: Side length = 4 units
Input: Area = 25 square meters
Output: Side length = 5 meters
Approach: To solve the problem, follow the below idea:
The side length of a square can be calculated using the formula: Side length=sqrt(Area)​.
Step-by-step algorithm:
- Calculate the square root of the given area.
- The result is the side length of the square.
Below is the implementation of the algorithm:
C++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double area, sideLength;
area = 25.0;
sideLength = sqrt (area);
cout << "Side length: " << sideLength << endl;
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
int main()
{
double area, sideLength;
area = 25.0;
sideLength = sqrt (area);
printf ( "Side length: %lf\n" , sideLength);
return 0;
}
|
Java
import java.util.Scanner;
public class SquareSideLength {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
double area = 25.0 ;
double sideLength = Math.sqrt(area);
System.out.println( "Side length: " + sideLength);
}
}
|
Python3
import math
area = 25.0
side_length = math.sqrt(area)
print (f "Side length: {side_length}" )
|
C#
using System;
class Program
{
static void Main()
{
double area = 25.0;
double sideLength = Math.Sqrt(area);
Console.WriteLine( "Side length: " + sideLength);
}
}
|
Javascript
let area = 25.0;
let sideLength = Math.sqrt(area);
console.log( "Side length: " + sideLength);
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...