Encuentra la longitud de la diagonal del hexágono

Dado un hexágono regular de lado a , la tarea es encontrar la longitud de su diagonal.
Ejemplos
 

Input : a = 4
Output : 8

Input : a = 7
Output : 14

Del diagrama, está claro que el triángulo ABC es un triángulo equilátero, entonces 
AB = AC = BC = a .
también es obvio, diagonal = 2*AC o 2*BC 
Entonces la longitud de la diagonal del hexágono = 2*a
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ Program to find the diagonal
// of the hexagon
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the diagonal
// of the hexagon
float hexadiagonal(float a)
{
    // side cannot be negative
    if (a < 0)
        return -1;
 
    // diagonal of the hexagon
    return 2 * a;
}
 
// Driver code
int main()
{
    float a = 4;
 
    cout << hexadiagonal(a) << endl;
 
    return 0;
}

C

// C Program to find the diagonal
// of the hexagon
#include <stdio.h>
 
// Function to find the diagonal
// of the hexagon
float hexadiagonal(float a)
{
    // side cannot be negative
    if (a < 0)
        return -1;
  
    // diagonal of the hexagon
    return 2 * a;
}
  
// Driver code
int main()
{
    float a = 4;
    printf("%f\n",hexadiagonal(a));
    return 0;
}

Java

// Java Program to find the diagonal
// of the hexagon
 
import java.io.*;
 
class GFG {
 
// Function to find the diagonal
// of the hexagon
static float hexadiagonal(float a)
{
    // side cannot be negative
    if (a < 0)
        return -1;
 
    // diagonal of the hexagon
    return 2 * a;
}
 
// Driver code
 
 
    public static void main (String[] args) {
            float a = 4;
 
    System.out.print( hexadiagonal(a));
}
}
 
// This code is contributed
// by shs

Python3

# Python3 Program to find the diagonal
# of the hexagon
 
# Function to find the diagonal
# of the hexagon
def hexadiagonal(a):
 
    # side cannot be negative
    if (a < 0):
        return -1
 
    # diagonal of the hexagon
    return 2 * a
 
 
# Driver code
if __name__=='__main__':
    a = 4
    print(hexadiagonal(a))
 
# This code is contributed by
# Shivi_Aggarwal

C#

// C# Program to find the diagonal
// of the hexagon
using System;
 
class GFG {
 
    // Function to find the diagonal
    // of the hexagon
    static float hexadiagonal(float a)
    {
        // side cannot be negative
        if (a < 0)
            return -1;
         
        // diagonal of the hexagon
        return 2 * a;
    }
 
// Driver code
public static void Main()
{
   float a = 4;
   Console.WriteLine( hexadiagonal(a));
}
}
 
// This code is contributed
// by anuj_67..

PHP

<?php
// PHP Program to find the diagonal
// of the hexagon
 
// Function to find the diagonal
// of the hexagon
function hexadiagonal($a)
{
    // side cannot be negative
    if ($a < 0)
        return -1;
 
    // diagonal of the hexagon
    return 2 * $a;
}
 
// Driver code
    $a = 4;
 
    echo hexadiagonal($a);
    // This code is contributed
// by anuj_67..
 
?>

Javascript

<script>
// javascript Program to find the diagonal
// of the hexagon
 
// Function to find the diagonal
// of the hexagon
function hexadiagonal(a)
{
 
    // side cannot be negative
    if (a < 0)
        return -1;
 
    // diagonal of the hexagon
    return 2 * a;
}
 
// Driver code
 
var a = 4;
document.write( hexadiagonal(a));
 
// This code is contributed by 29AjayKumar
</script>
Producción: 

8

 

Complejidad del tiempo: O(1)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por IshwarGupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *