🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Determining whether a number is even or odd is a common task in programming. Typically, this can be done using the modulus operator. However, in this blog post, we're going to take a more conceptual approach by using recursion. Recursion is a method of solving problems where a function calls itself as a subroutine. This approach can make some algorithms easier to implement and understand.
2. Program Steps
1. Define a function that takes a single integer as an argument.
2. Check if the number is 0 (base case for even) or 1 (base case for odd).
3. If neither, recursively call the function with the number decremented by 1.
4. Return the result of evenness or oddness.
3. Code Program
def is_even_or_odd(num):
# Base case for even
if num == 0:
return "Even"
# Base case for odd
elif num == 1:
return "Odd"
# Recursive step
else:
# Use the absolute value to handle negative numbers
return is_even_or_odd(abs(num) - 1)
Output:
# For an even number print(is_even_or_odd(10)) # Output: Even # For an odd number print(is_even_or_odd(11)) # Output: Odd
Explanation:
1. The function is_even_or_odd is defined to take one parameter, num.
2. It checks if num is 0, in which case it returns "Even", the base case for even numbers.
3. If num is 1, it returns "Odd", the base case for odd numbers.
4. If num is neither 0 nor 1, the function calls itself with abs(num) - 1, ensuring that the function can handle negative numbers and will eventually reach the base case.
5. This process repeats until the base case is met, and the function returns the appropriate string indicating whether the original number was even or odd.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment