Skip to content

Latest commit

 

History

History
124 lines (85 loc) · 3.79 KB

assignment-7.md

File metadata and controls

124 lines (85 loc) · 3.79 KB

Code Explanation: Favorite Numbers and Prime Checker

This program interacts with the user to collect their name and favorite numbers, then performs various operations on these numbers. Below are the steps performed in the code:

1. Prompt User for Name and Favorite Numbers

The code begins by prompting the user for their name and three favorite numbers:

name = input("Enter your name: ")
first_number = int(input("Enter your first favorite number: "))
second_number = int(input("Enter your second favorite number: "))
third_number = int(input("Enter your third favorite number: "))
  • Input:
    • The user's name is stored in the variable name.
    • Three favorite numbers are stored as integers in the variables first_number, second_number, and third_number.

2. Store Numbers in a List

The favorite numbers are then stored in a list for easier manipulation:

numbers = [first_number, second_number, third_number]
  • Purpose: Grouping the three favorite numbers into a list called numbers.

3. Create a List to Store Even/Odd Status

A new list status_list is created to store tuples of the favorite numbers and their even/odd status:

status_list = []

4. Determine Even/Odd Status of Each Number

The code loops through the list of numbers to check if each one is even or odd, and appends the results as tuples to status_list:

for number in numbers:
    if number % 2 == 0:
        status_list.append((number, "even"))
    else:
        status_list.append((number, "odd"))
  • Logic:
    • If the number is divisible by 2, it is considered "even".
    • If not, the number is classified as "odd".

5. Greet the User

A greeting message is displayed using the user's name, followed by an introduction to their favorite numbers:

print(f"\nHello, {name}! Let's explore your favorite numbers:")

6. Display Even/Odd Status of Each Number

The program prints out the even/odd status of each favorite number:

for number, status in status_list:
    print(f"The number {number} is {status}.")

7. Print Numbers and Their Squares

The code calculates and prints each number along with its square using a loop:

for number in numbers:
    print(f"The number {number} and its square: ({number}, {number ** 2})")
  • Explanation: For each number in the list, its square is calculated using the ** operator and printed in a tuple format.

8. Calculate the Sum of the Numbers

The sum of the user's favorite numbers is calculated using Python's built-in sum() function:

total_sum = sum(numbers)
print(f"\nAmazing! The sum of your favorite numbers is: {total_sum}")

9. Check if the Sum is a Prime Number

The program checks whether the sum of the numbers is a prime number. A prime number is only divisible by 1 and itself:

is_prime = True
if total_sum < 2:
    is_prime = False
else:
    for i in range(2, int(total_sum ** 0.5) + 1):
        if total_sum % i == 0:
            is_prime = False
            break
  • Logic:
    • Numbers less than 2 are automatically considered non-prime.
    • For numbers greater than or equal to 2, the program checks if any integer from 2 to the square root of the sum divides evenly into the total. If so, the sum is not prime.

10. Notify the User if the Sum is Prime

Finally, the program informs the user whether the sum of their favorite numbers is a prime number:

if is_prime:
    print(f"Wow, {total_sum} is a prime number!")
else:
    print(f"{total_sum} is not a prime number.")

This is a simple yet effective program that interacts with the user and performs mathematical checks on their input, including checking the parity (even/odd) and primality of the sum of their favorite numbers.