Write a program to find the side length of a square with given area.
Examples:
Input: Area = 16 square units
Output: Side length = 4 unitsInput: 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;
// Example test case
area = 25.0;
// Calculate side length
sideLength = sqrt (area);
// Output the result
cout << "Side length: " << sideLength << endl;
return 0;
} |
C
#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;
} |
Java
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);
}
} |
Python3
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}" )
|
C#
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);
}
} |
Javascript
// 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 :
Recommended Articles