CPSC150AScientific Computing

Activity 2

Variables

Hello

Write a program that prompts the user for their name. Your program should print a statement that greets that user.

Example

What is your name? Sheila
Hello Sheila

Sum

Write a program that stores two integers in variables called operand_1 and operand_2. Your program should compute and print the sum of the values of the operands.

Example

What is the value of operand 1? 5
What is the value of operand 2? 9
The sum is 14

Voyager

Voyager 1 is a space probe that was launched by NASA in 1977. It is currently the farthest man-made object from Earth. It is very likely that Voyager 1 is (or will be) the first man-made object to exit the solar system. Currently, Voyager 1 is approximately 19,828,976,290 km from Earth. The Voyager 1 communicates with radio waves, which travel at the speed of light, 299,792,458 m/s. At such a great distance, communications take an appreciable amount of time to reach earth. And yet, Voyager 1 is still closer to the Sun than any other star in our galaxy.

Details

Since Voyager 1 is constantly moving away from Earth, the program should prompt the user for Voyager’s current distance from Earth. The program should then print how long it takes a signal from Voyager 1 to reach the Earth. The calculations should be performed using Python’s arithmetic operations and printed nicely labeled and formatted. Be sure to use variables where appropriate.

Example

Enter Voyager 1's distance from Earth in kilometers: 19828976290
It takes 66142.3453487946 seconds for a message from Voyager to reach Earth.

Hint

• You first need to figure out how many meters Voyager 1 is from Earth. Recall that there are 1000 meters in one kilometer.

• The speed of light above is given in meters per second. So if you divide the distance in meters by the speed of light you will get how many seconds a signal takes to reach Earth.

Handshakes

One of the great advantages of a small campus like Roanoke College is that it feels like you know everybody on campus. How long would it take for everybody to meet everybody else on campus. Let’s find out.

Details

Create a Python program that prints the number of handshakes it would take for every student in a classroom to shake hands with every other student. Assume that there are 28 students in the classroom. This program should use arithmetic operations to perform the calculations necessary. Use variables where appropriate, and follow the course’s code conventions.

Example

It would take 45 handshakes for all 10 students to meet each other.

Hint

If you are not sure how to compute the number of handshakes, try some simple examples:

If there are 2 people, Alice and Bob, the following pairs will need to shake hands:

• Alice and Bob

which is 1 handshake.

If there are 3 people, Alice, Bob, and Carol, the following pairs will need to shake hands:

• Alice and Bob
• Alice and Carol
• Bob and Carol

which is 3 handshakes.

If there are 4 people, Alice, Bob, Carol, and Dan the following pairs will need to shake hands:

• Alice and Bob
• Alice and Carol
• Alice and Dan
• Bob and Carol
• Bob and Dan
• Carol and Dan

which is 6 handshakes.

Can you figure out how many handshakes there are if there are 5 people? Can you figure out the pattern to finding the number of handshakes for any number of people?

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect functionality will be accepted as complete.