PROBLEM 43 : Missing Number
A Program to find out the missing number from a range of 0-N numbers in a series.
Author - Ajay Zad
Date - 10/03/2022
import sys
n = int(input("Enter the value for n : "))
l = []
for i in range(1,n):
n1 = int(input("Enter the value less than or equal to 'n' without repeating the values :"))
l.append(n1)
l.sort()
j = 1
for i in range(0,n-1):
if j != l[i]:
print("The missing number is ",j)
sys.exit()
j = j + 1
print("The missing number is ",n)
OR
n = int(input("Enter the value for n : "))
l = []
sum = 0
for i in range(1,n):
n1 = int(input("Enter the value less than or equal to 'n' without repeating the values :"))
l.append(n1)
sum = sum + n1
sum1 = 0
for i in range(1,n+1):
sum1 = sum1 + i
print("The missing number is ",(sum1-sum))
The above code is in Python language.
Input :
Enter the value for n : 5
Enter the value less than or equal to 'n' without repeating the values : 3
Enter the value less than or equal to 'n' without repeating the values : 1
Enter the value less than or equal to 'n' without repeating the values : 5
Enter the value less than or equal to 'n' without repeating the values : 2
Output :
The missing number is 4
No comments:
Post a Comment