images arent side by side latex code example

Example 1: latex how to make two images side by side

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{subcaption}	% You need this package for subfigures

\begin{document}

\begin{figure}
\centering
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=.4\linewidth]{image1}
  \caption{A subfigure}
  \label{fig:sub1}
\end{subfigure}%
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=.4\linewidth]{image1}
  \caption{A subfigure}
  \label{fig:sub2}
\end{subfigure}
\caption{A figure with two subfigures}
\label{fig:test}
\end{figure}

Example 2: figures in latex side by side

\documentclass{article}\usepackage{graphicx}\begin{document}\begin{figure}[!tbp]  \centering  \begin{minipage}[b]{0.4\textwidth}    \includegraphics[width=\textwidth]{flower1.jpg}    \caption{Flower one.}  \end{minipage}  \hfill  \begin{minipage}[b]{0.4\textwidth}    \includegraphics[width=\textwidth]{flower2.jpg}    \caption{Flower two.}  \end{minipage}\end{figure}\end{document}