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)
Trainings :
Data Science Training in Indore | Data Analytics Training in Indore | Python Training in Indore | Blockchain Training in Indore | React JS Training in Indore | Web Development Training in Indore | Full Stack Development Training in Indore |Free Courses and Resource :
Verbal Aptitude | Matplotlib Tutorials | Examples | Interview Questions | Pandas Tutorials | Projects | Interview Questions | OpenCV Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Dart | Python Data Structures and Algorithms | Rust Tutorials | Projects | Interview Questions | Aptitude Tests | HackerRank Python | Django | MongoDB Tutorials | Examples | Interview Questions | NumPy Practice Questions | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | React.js Tutorials | Verbal Ability Tutorial | MS Sql Server Tutorials | Examples | Interview Questions | Power BI Tutorials | Projects | Interview Questions | Example Dashboards | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial | HackerRank C++ Solutions | Tensor Flow | Pandas Practice Questions | Python Practice Questions | C++ Tutorials | Quantitative Ability Tutorial | Javascript | Node.js Tutorials | HackerRank SQL Solutions | HackerRank DSA Solutions | Verbal Aptitude 2 | HackerRank Java Solutions | HTML Tutorial | TCS NQT Mock Test Series | Reasoning Ability Tutorial | CodeChef Python Solutions | HackerRank C Program Solutions | Leetcode Python Solutions | SQL Practice Question |Interview Questions :
Pandas Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Django | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial |Top Colleges in India :
Indian Institute of Technology Bombay | Jaypee University of Engineering and Technology - Guna |