Instagram
youtube
Facebook
Twitter

Python Program to Find Missing Number from 1 to 100

A Python Program to Find Missing Number from 1 to 100?

Code Explanation:

Function Definition:
A function find_missing_number(numbers) is defined to calculate the missing number.

Expected Sum:
n = 100 sets the fixed range from 1 to 100.
expected_sum = n * (n + 1) // 2 calculates the total sum of numbers from 1 to 100 using the mathematical formula.

Actual Sum:
actual_sum = sum(numbers) computes the total sum of the user-provided numbers.

Missing Number Calculation:
missing number = expected_sum - actual_sum
The number that is not in the list is found by subtracting the actual sum from the expected sum.

User Input:
The program takes numbers between 1 and 100 from the user using input(), with one number missing.

Convert to List:
map(int, input.split()) converts the input string into a list of integers.

Function Call and Output:
The function is called and the result is stored in missing, which is then printed.

 

Program:

def find_missing_number(numbers):

    n = 100

    expected_sum = n * (n + 1) // 2

    actual_sum = sum(numbers)

    return expected_sum - actual_sum

user_input = input("Enter numbers from 1 to 100 with one missing, separated by space:\n")

num_list = list(map(int, user_input.strip().split()))

missing = find_missing_number(num_list)

print("Missing number is:", missing)