Write a function that receives an integer and returns 1 if received integer is an even
number else returns -1.
Write a python function to convert money in
Answers
Answered by
5
The given problem is solved using language - Python.
def f(x):
if x%2==0:
return 1
return -1
x=int(input('Enter a number: '))
print('Value returned by the function:',f(x))
A short approach for this problem:
f=lambda x:[1,-1][x%2]
x=int(input('Enter a number: '))
print('Value returned by the function:',f(x))
- Here, we have to create a function that returns 1 if the integer passed is even and -1 if it is odd.
- So, we will first check if the entered number is even or not. If true, return 1.
- Now, if the condition is false, the if block will not execute and the function will return -1 by default.
Case 1:
Enter a number: 5
Value returned by the function: -1
Case 2:
Enter a number: 6
Value returned by the function: 1
Attachments:
Similar questions
Computer Science,
20 days ago
English,
20 days ago
India Languages,
20 days ago
Math,
1 month ago
Physics,
9 months ago
Math,
9 months ago