A number is even if it is perfectly divisible by 2. When the number is divided by 2, we use the remainder operator % to compute the remainder. If the remainder is not zero, the number is odd.
Source Code
# Python program to check if the input number is odd or even. # A number is even if division by 2 gives a remainder of 0. # If the remainder is 1, it is an odd number. num = int(input("Enter a number: ")) if (num % 2) == 0: print("{0} is Even".format(num)) else: print("{0} is Odd".format(num))Output 1
Enter a number: 43 43 is OddOutput 2
Enter a number: 18 18 is EvenIn this program, we ask the user for the input and check if the number is odd or even. Please note that { } is a replacement field for num.
#Here I define the user and cpu function
cpu_num = random.randint(1,6) user_selection = ["Odd", "Even"] def cpu_choice(): choice = random.randint(1,6) print("The computer's choice is",choice) return choice def user_choice(): while(True): choice = input("Odd or Even? ") if choice in user_selection: print("Your choice is",choice) return choice else: print("You did not write your answer correctly, please try again.")#In this function I define the result of each round based on previous formulas and two new variables (num_rounds and user_lives) and everything works well
def round_result(): num_rounds = 0 user_lives = 10 while num_rounds < 8 and user_lives > 0: user_pick = user_choice() cpu_pick = cpu_choice() if (cpu_pick % 2 == 0) and (user_pick == "Even"): print (f'You have {user_lives + cpu_pick} lives left') num_rounds += 1 user_lives = user_lives + cpu_pick if (cpu_pick % 2 == 0) and (user_pick == "Odd"): print (f'You have {user_lives - cpu_pick} lives left') num_rounds += 1 user_lives = user_lives - cpu_pick if (cpu_pick % 2 != 0) and (user_pick == "Even"): print (f'You have {user_lives - cpu_pick} lives left') num_rounds += 1 user_lives = user_lives - cpu_pick if (cpu_pick % 2 != 0) and (user_pick == "Odd"): print (f'You have {user_lives + cpu_pick} lives left') num_rounds += 1 user_lives = user_lives + cpu_pick#Everything works well until here, I don't know what am I doing wrong in the winner function
def winner(): user_won = user_lives > 0 and num_rounds == 8 cpu_won = user_lives < 0 game = round_result() while game: if user_won is True: print ('You won') if cpu_won is True: print ('Cpu won')Given three positive integers X, Y and P. Here P denotes the number of turns. Whenever the turn is odd X is multiplied by 2 and in every even turn Y is multiplied by 2. The task is to find the value of max(X, Y) ÷ min(X, Y) after the complete P turns.
Examples :
Lets play the above game for 8 turns :
Here we can easily spot a pattern :
Here z is actually the power of 2. So, we can
simply say –
Below is the implementation :
C++
#include <bits/stdc++.h>
using namespace std;
int findValue(int X, int Y, int P)
{
if (P % 2 == 0)
return (max(X, Y) / min(X, Y));
else
return (max(2 * X, Y) / min(2 * X, Y));
}
int main()
{
int X = 1, Y = 2, P = 1;
cout << findValue(X, Y, P) << endl;
X = 3, Y = 7, P = 2;
cout << findValue(X, Y, P) << endl;
}
C
#include <stdio.h>
int findValue(int X, int Y, int P)
{
int Max = X,Min = X;
if(Max < Y)
Max = Y;
if(Min > Y)
Min = Y;
int Max2 = 2*X,Min2 = 2*X;
if(Max2 < Y)
Max2 = Y;
if(Min2 > Y)
Min2 = Y ;
if (P % 2 == 0)
return (Max / Min);
else
return (Max2 / Min2);
}
int main()
{
int X = 1, Y = 2, P = 1;
printf("%d\n",findValue(X, Y, P));
X = 3, Y = 7, P = 2;
printf("%d\n",findValue(X, Y, P));
}
Java
import java.util.*;
class Even_odd{
public static int findValue(int X, int Y,
int P)
{
if (P % 2 == 0)
return (Math.max(X, Y) /
Math.min(X, Y));
else
return (Math.max(2 * X, Y) /
Math.min(2 * X, Y));
}
public static void main(String[] args)
{
int X = 1, Y = 2, P = 1;
System.out.println(findValue(X, Y, P));
X = 3;
Y = 7;
P = 2;
System.out.print(findValue(X, Y, P));
}
}
Python3
def findValue( X , Y , P ):
if P % 2 == 0:
return int(max(X, Y) / min(X, Y))
else:
return int(max(2 * X, Y) / min(2 * X, Y))
X = 1
Y = 2
P = 1
print(findValue(X, Y, P))
X = 3
Y = 7
P = 2
print((findValue(X, Y, P)))
C#
using System;
class GFG
{
public static int findValue(int X, int Y,
int P)
{
if (P % 2 == 0)
return (Math.Max(X, Y) /
Math.Min(X, Y));
else
return (Math.Max(2 * X, Y) /
Math.Min(2 * X, Y));
}
public static void Main()
{
int X = 1, Y = 2, P = 1;
Console.WriteLine(findValue(X, Y, P));
X = 3;
Y = 7;
P = 2;
Console.WriteLine(findValue(X, Y, P));
}
}
PHP
<?php
function findValue($X, $Y, $P)
{
if ($P % 2 == 0)
return (int)(max($X, $Y) /
min($X, $Y));
else
return (int)(max(2 * $X, $Y) /
min(2 * $X, $Y));
}
$X = 1;
$Y = 2;
$P = 1;
echo findValue($X, $Y, $P), "\n";
$X = 3; $Y = 7; $P = 2;
echo findValue($X, $Y, $P), "\n";
?>
Javascript
<script>
function findValue(X, Y, P)
{
if (P % 2 == 0)
return parseInt((Math.max(X, Y) / Math.min(X, Y)), 10);
else
return parseInt((Math.max(2 * X, Y) / Math.min(2 * X, Y)), 10);
}
let X = 1, Y = 2, P = 1;
document.write(findValue(X, Y, P) + "</br>");
X = 3, Y = 7, P = 2;
document.write(findValue(X, Y, P));
</script>
Output:
Time Complexity:O(1)