Dear @Cresta_jame_Camacho1,
Refer our these two article, you will understand it easily and can able to implement it in a single program.
Just make two function from these two article for our given question