+ 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!

17th Aug 2020, 4:50 AM
Shuarma
Shuarma - avatar
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 😅
17th Aug 2020, 4:58 AM
Nilesh
Nilesh - avatar
+ 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
17th Aug 2020, 5:06 AM
BroFar
BroFar - avatar
+ 3
Why dont people realize that 0*2 = 0?
17th Aug 2020, 2:18 PM
Zachiah sawyer
Zachiah sawyer - avatar
17th Aug 2020, 5:41 AM
Rishi
17th Aug 2020, 6:41 AM
Tri Satria [NEW]
Tri Satria [NEW] - avatar
+ 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)
17th Aug 2020, 7:21 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 1
1 Use modulus fn to get even or odd 2 Return 0 if zero 😶
18th Aug 2020, 7:30 AM
Sanjay Kamath
Sanjay Kamath - avatar
0
x = int(input()) if x % 2 == 0: print(x*2) else: print(x*3)
17th Aug 2020, 5:09 AM
Zachiah sawyer
Zachiah sawyer - avatar