write pseudocode in latex code example

Example: latex how to write algorithm pseudocode

% in the header:
% \usepackage{algorithm}
% \usepackage{algpseudocode}

% in the  body:
\begin{algorithm}
  \caption{Euclid’s algorithm}
  \label{euclid}
  \begin{algorithmic}[1]
    \Require $x\ge5$
    \Ensure $x\le-5$
    \Function{Euclid}{$a,b$}
      \Comment{The g.c.d. of a and b}
      \State $r\gets a\bmod b$
      \While{$r \neq 0$}
        \Comment{We have the answer if r is 0}
        \State $a\gets b$
        \State $b\gets r$
        \State $r\gets a\bmod b$
      \EndWhile
      \label{euclidendwhile}
      \State \Return{$b$}
      \Comment{The gcd is b}
    \EndFunction
  \end{algorithmic}
\end{algorithm}

Tags:

Misc Example