Check whether a combination of two numbers has a sum of 10 from the given list

In this python function program, we will create a function to check whether a combination of two numbers has a sum of 10 from the given list.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function check_sum.
  2. Use the def keyword to define the function.
  3. Pass two parameters i.e. list and the sum number to the function.
  4. Use a for loop with the range function to iterate over the elements in the list.
  5. Use a nested for loop with the range function to iterate over the next numbers from the list i.e first loop un the first iteration will use the first element and with the help of the nested loop we will iterate over the number the numbers from the second element.
  6. Now check whether the sum of 1st element and any others elements from the nested loop is equal to 10.
  7. If yes then return True else return False.
  8. Pass the list and the sum number to the function while calling the function.
				
					def check_sum(nums, k):   
    for i in range(len(nums)):
        for j in range(i+1, len(nums)):
            if nums[i] + nums[j] == k:
                return True
    return False
print(check_sum([2, 5, 6, 4, 7, 3, 8, 9, 1], 10))
				
			

Output :

				
					True
				
			

Related Articles

get the Fibonacci series up to the given number.

get unique values from the given list.

Leave a Comment