LaTEX Tutorials

Algorithms in LaTeX

How to write algorithms in LaTeX?

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Diam in arcu cursus euismod quis viverra nibh cras pulvinar. Vulputate ut pharetra sit amet aliquam. 

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}
\caption{Sum of Two Numbers}
\begin{algorithmic}[1]
\Procedure{SumofTwo}{$a, b$} 
\State $a \gets 10$ 
\State $b \gets 20$ 
\State $sum \gets a + b$ 
\State $\text{print}\ sum$
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Diam in arcu cursus euismod quis viverra nibh cras pulvinar. Vulputate ut pharetra sit amet aliquam. 

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}
\caption{Sum of Two Numbers}
\hspace*{\algorithmicindent} \textbf{Input:} num1, num2 \\
\hspace*{\algorithmicindent} \textbf{Output:} sum
\begin{algorithmic}[1]
\Procedure{SumOfTwo}{$a, b$} 
\State $a \gets 10$ 
\State $b \gets 20$ 
\State $sum \gets a + b$ 
\State \Return $sum$
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Diam in arcu cursus euismod quis viverra nibh cras pulvinar. Vulputate ut pharetra sit amet aliquam. 

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}
\caption{Sum of Two Numbers}
\hspace*{\algorithmicindent} \textbf{Input:} num1, num2 \\
\hspace*{\algorithmicindent} \textbf{Output:} sum
\begin{algorithmic}[1]
\Procedure{SumOfTwo}{$a, b$} 
\State $a \gets 10$ 
\State $b \gets 20$ 
\If{$a > b$}
\State $\Return\ a$
\Else 
\State $\Return\ b$
\EndIf
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Diam in arcu cursus euismod quis viverra nibh cras pulvinar. Vulputate ut pharetra sit amet aliquam. 

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}
\caption{Display Numbers Between 10 and 20 }
\hspace*{\algorithmicindent} \textbf{Input:} num1, num2 \\
\hspace*{\algorithmicindent} \textbf{Output:} sum
\begin{algorithmic}[1]
\Procedure{PrintNumbers}{$a, b$} 
\State $a \gets 10$ 
\State $b \gets 20$ 
\While{$a \leq b$}
\State $\text{print}\ a$
\State $a \gets a+1$
\EndWhile
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Diam in arcu cursus euismod quis viverra nibh cras pulvinar. Vulputate ut pharetra sit amet aliquam. 

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}
\caption{Display Elements in a List }
\hspace*{\algorithmicindent} \textbf{Input:} num1, num2 \\
\hspace*{\algorithmicindent} \textbf{Output:} sum
\begin{algorithmic}[1]
\Procedure{PrintNumbers}{$a, b$}
\State $list \gets [10, 11, 12, 13, 14, 15]$ 
\For{$i\ \text{in}\ list$}
\State $\text{print}\ i$
\EndFor
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

Need Help?

If you need any further clarification or assistance in LaTeX coding, you are free to contact us.