Open In App

Program to find the side length of a square given its area

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:

Below is the implementation of the algorithm:




#include <cmath>
#include <iostream>
 
using namespace std;
 
int main()
{
    double area, sideLength;
 
    // Example test case
    area = 25.0;
 
    // Calculate side length
    sideLength = sqrt(area);
 
    // Output the result
    cout << "Side length: " << sideLength << endl;
 
    return 0;
}




#include <math.h>
#include <stdio.h>
 
int main()
{
    double area, sideLength;
 
    // Example test case
    area = 25.0;
 
    // Calculate side length
    sideLength = sqrt(area);
 
    // Output the result
    printf("Side length: %lf\n", sideLength);
 
    return 0;
}




import java.util.Scanner;
 
public class SquareSideLength {
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
 
        // Example test case
        double area = 25.0;
 
        // Calculate side length
        double sideLength = Math.sqrt(area);
 
        // Output the result
        System.out.println("Side length: " + sideLength);
    }
}




import math
 
# Example test case
area = 25.0
 
# Calculate side length
side_length = math.sqrt(area)
 
# Output the result
print(f"Side length: {side_length}")




using System;
 
class Program
{
    static void Main()
    {
        // Example test case
        double area = 25.0;
 
        // Calculate side length
        double sideLength = Math.Sqrt(area);
 
        // Output the result
        Console.WriteLine("Side length: " + sideLength);
    }
}




// Example test case
let area = 25.0;
 
// Calculate side length
let sideLength = Math.sqrt(area);
 
// Output the result
console.log("Side length: " + sideLength);

Output
Side length: 5

Time Complexity: O(1)
Auxiliary Space: O(1)


Article Tags :