This is true if you assume $H_n(X)$ is finitely generated for all $n$ (all coefficients in this post will be $\mathbb{Z}$). In particular, this holds if $X$ has the homotopy type of a CW-complex with finitely many cells in each degree.
To prove this, we invoke the classification of finitely generated abelian groups, which says that $H_n(X)$ is a finite direct sum of cyclic groups. Since the universal coefficient theorem says that $H^n(X)\cong \operatorname{Hom}(H_n(X),\mathbb{Z})\oplus\operatorname{Ext}(H_{n-1}(X),\mathbb{Z})$ and Hom and Ext both preserve (finite) direct sums, to compute $H^n(X)$ we just have to compute $\operatorname{Hom}(A,\mathbb{Z})$ and $\operatorname{Ext}(A,\mathbb{Z})$ when $A$ is a cyclic group. In particular, we can use the following facts:
$$\operatorname{Hom}(\mathbb{Z},\mathbb{Z})\cong \mathbb{Z}$$
$$\operatorname{Hom}(\mathbb{Z}/m,\mathbb{Z})\cong 0$$
$$\operatorname{Ext}(\mathbb{Z},\mathbb{Z})\cong 0$$
$$\operatorname{Ext}(\mathbb{Z}/m,\mathbb{Z})\cong \mathbb{Z}/m$$
It follows that $\operatorname{Hom}(H_n(X),\mathbb{Z})$ is a direct sum of copies of $\mathbb{Z}$, one for each direct summand of $\mathbb{Z}$ in $H_n(X)$, and $\operatorname{Ext}(H_{n-1}(X),\mathbb{Z})$ is a direct sum of finite cyclic groups which are isomorphic to the finite cyclic groups appearing as direct summands in $H_{n-1}(X)$. So, in brief, $H^n(X)$ is isomorphic to the direct sum of the free part of $H_n(X)$ and the torsion part of $H_{n-1}(X)$ (where "free part" means the direct sum of all the $\mathbb{Z}$ summands, and "torsion part" means the direct sum of the finite cyclic summands).
In particular, from the description above, we see that the torsion part of $H^n(X)$ is isomorphic to the torsion part of $H_{n-1}(X)$, so $H^n(X)$ has an element of order $p$ iff $H_{n-1}(X)$ has an element of order $p$. Considering the (co)homology in all degrees at once, we get that $H^*(X)$ has an element of order $p$ iff $H_n(X)$ has an element of order $p$.
Without the assumption that $H_n(X)$ is finitely generated, this need not be true. For instance, it is possible to construct a space $X$ such that $H_0(X)=\mathbb{Z}$, $H_1(X)=\mathbb{Q}/\mathbb{Z}$, and $H_n(X)=0$ for all $n>1$. Then the homology of $X$ has $p$-torsion for any $p$, but the cohomology can be computed to have no $p$-torsion: $H^0(X)=\mathbb{Z}$, $H^1(X)=0$, $H^2(X)=\hat{\mathbb{Z}}$, and $H^n(X)=0$ for $n>2$.