+ 2
can someone solve this and explain in depth?
Write a program that takes a number as input and - returns its double, if the number is even - returns its triple, if the number is odd - returns 0, if number is 0 I can't solve it yet :(( edit: I solved it a couple minutes ago but thank you everyone! I expected that I was gonna get called dumb cause I couldn't solve something simple, thanks a lot everyone!
8 Réponses
+ 7
Frankly speaking I suck at python but just telling you the logic,
num = int(input())
Take an input integer
Than take a if statement,
if(num%2==0)
print(num * 2)
Since it's written that if the number is even ( numbers which have remainder 0) it returns the double of the input number.
return num
Than for the second condition, take another if statement,
if(num%2==1)
print (num * 3)
Since it's written that if the number is odd ( numbers which have remainder 1) it returns the triple of the input number.
return num
Last condition states that if the input no. is 0
if(num == 0)
print("0")
There are many other methods to solve this but I think this might be easy (according to me)
Hope it clears
I really suck at python 😅
+ 4
Joshua Quitoy here is the code simplistically
# input a number
x = int(input())
# double it
y = (x*x)
print(y)
# check rather even or drop through for odd
if ( y%2 == 0):
print(y*x)
else:
print(0)
input a number // x = int(input())
- returns its double ( x*x )
if the number is even ( x*x)%2 == 0
- returns its triple, ( x * x * x )
if the number is odd
- returns 0, if number is 0
+ 3
Why dont people realize that 0*2 = 0?
+ 2
https://code.sololearn.com/cgxH0kL0u3k4/?ref=app
Maybe this can help
+ 1
That is rather smart Zachiah sawyer 👌👌
One-liner-ish solution based on Sawyers'
x = int(input())
print(x*2 if x%2==0 else x*3)
+ 1
1 Use modulus fn to get even or odd
2 Return 0 if zero 😶
0
x = int(input())
if x % 2 == 0:
print(x*2)
else:
print(x*3)