Computer Science, asked by manjarisingh2121, 1 month ago

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 anindyaadhikari13
5

\texttt{\textsf{\large{\underline{Solution}:}}}

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))

\texttt{\textsf{\large{\underline{Explanation}:}}}

  • 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.

\texttt{\textsf{\large{\underline{Sample I/O}:}}}

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