Given $A,B$ are $n \times n$ matrices, $(A+B)^2=A+B,r(A+B)=r(A)+r(B)$. Prove: $A^2=A,B^2=B$ Given $A,B$ are $n \times n$ matrices, $(A+B)^2=A+B,r(A+B)=r(A)+r(B)$.
Prove: $A^2=A,B^2=B$
I don't know how to use $r(A+B)=r(A)+r(B)$

It seems like you are working on your linear algebra worksheet(very similar to mine).@Burno B 's answer is beautiful,but there's also a tricky method just using the properties of the rank of matrix.
We know that the elementary row (or column) transformation doesn't change the rank of a matrix,so we have
$$
\begin{pmatrix}
A&0&0\\
0&B&0\\
0&0&I-A-B
\end{pmatrix}
\sim
\begin{pmatrix}
A&0&0\\
0&B&0\\
A&B&I-A-B
\end{pmatrix}
\sim
\begin{pmatrix}
A&0&A\\
0&B&B\\
A&B&I
\end{pmatrix}
$$
$$
\sim
\begin{pmatrix}
A-A^2&-AB&0\\
-BA&B-B^2&0\\
A&B&I
\end{pmatrix}
\sim
\begin{pmatrix}
A-A^2&-AB&0\\
-BA&B-B^2&0\\
0&0&I
\end{pmatrix}
$$
and
$$
r(A)+r(B)+r(I-A-B)=r\begin{pmatrix}
A-A^2&-AB\\
-BA&B-B^2
\end{pmatrix}+r(I)=r\begin{pmatrix}
A-A^2&-AB\\
-BA&B-B^2
\end{pmatrix}+n
$$
Also,
$$
(A+B)^2=(A+B)\implies r(A+B)+r(I-A-B)=n
$$
$$
r(A+B)=r(A)+r(B)\implies r(A)+r(B)+r(I-A-B)=n
$$
which implies
$$
r\begin{pmatrix}
A-A^2&-AB\\
-BA&B-B^2
\end{pmatrix}=0
$$
so
$$
A^2=A,B^2=B,AB=BA=0
$$
