I'm trying to make a program which checks if a word is a palindrome and I've gotten so far and it works with words that have an even amount of numbers. I know how to make it do something if the amount of letters is odd but I just don't know how to find out if a number is odd. Is there any simple way to find if a number is odd or even?
Just for reference, this is my code:
a = 0 while a == 0: print("\n \n" * 100) print("Please enter a word to check if it is a palindrome: ") word = input("?: ") wordLength = int(len(word)) finalWordLength = int(wordLength / 2) firstHalf = word[:finalWordLength] secondHalf = word[finalWordLength + 1:] secondHalf = secondHalf[::-1] print(firstHalf) print(secondHalf) if firstHalf == secondHalf: print("This is a palindrom") else: print("This is not a palindrom") print("Press enter to restart") input()
In this article, We will see how to write the program to find the given number is Even or Odd Using the Recursion Method. If the number is even returned true else false in Python.
For that, we use the Operator Called Modulus Operator. This operator used in the operation when we need to calculate the remainder of that number when divided by any divisor.
- Even Number: A number Which is completely divisible by 2 Hence remainders is 0
- Odd Number: A number which is not divisible by 2 Hence remainders is 1
Examples:
Input: 2 Output: True Explanation : 2 % 2==0 So True Input: 5 Output: False Explanation : 2 % 2 != 0 So, FalseMethods #1:
Approach :
- We use the concept of getting the remainder without using the modulus operator by subtracting the number by number-2
- If at last, we get any remainder then that number is odd and return the False for that number
- Else the number is even and return True for that number
|
Output:
3 num is oddMethods #2:
We use the modulus operator to find the even or odd Like if num % 2 == 0 then the remainder is 0 so the given number is even and return True
Else if num % 2 != 0 then the remainder is not zero so the given number is odd and return False
print(num ,"num is even") |
Output:
3 num is oddMethods #3: To print all the even number for the range (a, b)
Here We will print all the even number for the Given Range n in the Function evenOdd(n)
|
Output:
2 4 6 8 10
Odd and Even numbers: If you divide a number by 2 and it gives a remainder of 0 then it is known as even number, otherwise an odd number. Even number examples: 2, 4, 6, 8, 10, etc. Odd number examples:1, 3, 5, 7, 9 etc. See this example: num = int(input("Enter a number: ")) if (num % 2) == 0: print("{0} is Even number".format(num)) else: print("{0} is Odd number".format(num)) Output: Next TopicPython Check Leap Year |
- Send your Feedback to [email protected]
def find(num): # code logic here if num%2 == 0: numtype="even" else: numtype = "odd" return numtype num = int(input('Enter the number: ')) # 1. take your input numtype = find(num) # 2. call the find function print('Given number is',numtype). # 3. print if the number is even or odd
coder# python challenge07.py Enter the number: 5 Given number is odd coder# python challenge07.py Enter the number: 8 Given number is even
- input() function is used to take user input
- find() function is called to to check if a number is off/even. This function returns numtype as odd/even
- At last, print if the given number is odd/even
Avoid usage of else block by assigning a default value (odd) to numtype
def find(num): # code logic here numtype = "odd" if num%2 == 0: numtype="even" return numtype num = int(input('Enter the number: ')) # take your input numtype = find(num) # call the find function print('Given number is',numtype) # print if the number is even or odd
Output:
coder# python challenge07.py Enter the number: 5 Given number is odd coder# python challenge07.py Enter the number: 8 Given number is even
Solution #3
directly return numtype
def find(num): # code logic here if num%2 == 0: return "even" return "odd" num = int(input('Enter the number: ')) # take your input numtype = find(num) # call the find function print('Given number is',numtype) # print if the number is even or odd
Output:
coder# python challenge07.py Enter the number: 5 Given number is odd coder# python challenge07.py Enter the number: 8 Given number is even
Bonus
numtype = num%2 == 0 and "even" or "odd"
Find the video explanation also
In this tutorial, we will discuss the Python program to check a number is even or odd using the function
In this program, we are going to learn about how to find the odd or even number from given number using function in the Python language.
First, we must understand what is even or odd?
What is Even or Odd
When the number is divided by 2 and the balance becomes zero and the above number is called as the even number – eg 2,4,6,8,10
and on the other sides when it is divided by 2 and balance becomes 1 they are called odd numbers or uneven numbers
In my previous post, I have explained the various ways to check whether the number is even or odd in Python language
However, in this program, we embed the logic of the check even or odd numbers program in the function
You can embed the logic of the program to check even or odd numbers using any of the following approaches in the function
Once This program received the number it will check the given number either odd or even.
After receiving the input from the user, it is stored in the variable of num and then program divides the value of num by 2 and displays the output
Using modular operator
num=int(input("Enter a number for check odd or even: ")) def find_Evenodd(num): if(num%2==0): print(num," Is an even") else: print(num," is an odd") find_Evenodd(num);//function callWhen the above code is executed, it produces the following results
Case 1
Enter a number for check odd or even: 349 349 is an oddCase 2
Enter a number for check odd or even: 234 234 is an evenUsing Bitwise operator
num=int(input("Enter a number for check odd or even: ")) def find_Evenodd(num)://function definition if(num&1==1): print(num, "is an odd number"); else: print(num, "is an even number"); find_Evenodd(num);//function callWhen the above code is executed, it produces the following results
Case 1
Enter a number for check odd or even: 678 678 is an even numberCase 2
Enter a number for check odd or even: 765 765 is an odd numberUsing the division operator
num=int(input("Enter a number for check odd or even: ")) def find_Evenodd(num)://function definition number=(num/2)*2 if(number==num): print(num, "is a even number"); else: print(num, "is a odd number"); find_Evenodd(num);//call the functionWhen the above code is executed, it produces the following results
Case 1
Enter a number for check odd or even: 678 678 is a even numberCase 2
Enter a number for check odd or even: 987 987 is a odd numberSuggested for you
Python operator
python if else statements
Python function
Similar post
Python program to check whether a number odd or even
Python program to display even and odd number in the given range
Python code to display all even and odd numbers from 1 to n
Python code to display all even and odd numbers without if