Challenge: Create Random Numbers and Print a Square and a Cube via Threads
Write a program in which the first thread produces random numbers, the second thread prints the square of the number, and the third thread prints the cube of the random number.
We'll cover the following
Write a program that has three threads in it. The first thread should produce random numbers from 1–20. The second thread should display the square of the number generated by the first thread on the screen. The third thread should write the cube of the number generated by the first thread on the screen:
-
Input: Random numbers generated by the first thread will be used as inputs in the second and third threads.
-
Output: The
generate()
function will generate 10 random numbers. Thesquare()
function will print the square of each random number. Thecube()
function will print the cube of each random number.
Get hands-on with 1200+ tech skills courses.