🎓 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
In this post, we will explore how to determine if a number is positive or negative in Python. While this might seem like a straightforward task, doing it recursively is a great way to understand the concept of recursion, which is particularly useful in solving more complex problems.
Recursion is a programming technique where a function calls itself. To avoid infinite recursion, a base case is defined. For checking if a number is positive or negative, the base case can be the number itself, since any non-zero number can be classified as positive or negative.
2. Program Steps
1. Define a recursive function that takes an integer as an argument.
2. Set the base case for the function to stop calling itself.
3. If the number is positive or negative, return the corresponding result.
4. If the number is zero, it is neither positive nor negative, return that result.
3. Code Program
def check_positive_negative(num):
# Base case: if the number is greater than 0, it is positive
if num > 0:
return "Positive"
# Base case: if the number is less than 0, it is negative
elif num < 0:
return "Negative"
# Base case: if the number is 0, it is neither positive nor negative
else:
return "Neither Positive Nor Negative"
Output:
print(check_positive_negative(10)) # Output: Positive print(check_positive_negative(-5)) # Output: Negative print(check_positive_negative(0)) # Output: Neither Positive Nor Negative
Explanation:
1. The function check_positive_negative is defined to accept one parameter, num.
2. It checks if num is greater than 0, in which case it returns "Positive".
3. If num is less than 0, it returns "Negative".
4. If num is exactly 0, it returns "Neither Positive Nor Negative", as zero is neither positive nor negative.
5. There is no recursive call in this function because the nature of the problem doesn't require breaking down the problem into smaller subproblems.
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