Algorithm (Step by Step):
- Start
- Define a recursive function
add_positive_numbers(a, b)
- Check if
b
is 0 (base case)- If true, return
a
- If false, proceed to recursive case
- If true, return
- For recursive case, return
add_positive_numbers(a + 1, b - 1)
- Prompt the user to enter two positive numbers
- Validate that both numbers are positive
- Call
add_positive_numbers
with the input numbers - Display the result
- End
Hello, dear reader! 👋
Thanks for visiting my blog! I’m a student just like you, sharing what I learn to help others with Python programming. I hope my posts are useful for your studies! 😊
If you find this post helpful, please leave a comment—even just a few emojis will make my day! 🐍✨ Your feedback keeps me motivated to create more content for everyone. 🚀
Happy programming!
— Abhin Krishna, S01, EB Department, MEC
Pseudocode:
FUNCTION add_positive_numbers(a: INTEGER, b: INTEGER) -> INTEGER
IF b = 0 THEN
RETURN a
ELSE
RETURN add_positive_numbers(a + 1, b - 1)
END IF
END FUNCTION
ALGORITHM AddPositiveNumbers
BEGIN
INPUT num1, num2
IF num1 < 0 OR num2 < 0 THEN
DISPLAY "Both numbers must be positive."
ELSE
result ← add_positive_numbers(num1, num2)
DISPLAY "The sum of ", num1, " and ", num2, " is ", result
END IF
END
Program:
# Recursive function to add two positive numbers
def add_positive_numbers(a, b):
# Base case: if one of the numbers is zero
if b == 0:
return a
# Recursive case: increment a and decrement b
return add_positive_numbers(a + 1, b - 1)
# Input two positive numbers
num1 = int(input("Enter the first positive number: "))
num2 = int(input("Enter the second positive number: "))
# Ensure both numbers are positive
if num1 < 0 or num2 < 0:
print("Both numbers must be positive.")
else:
# Calculate the sum using recursion
result = add_positive_numbers(num1, num2)
# Print the result
print(f"The sum of {num1} and {num2} is {result}")
Flowchart:
flowchart TD
A([Start]) --> B[/Enter 2 positive numbers/]
B --> C{Are both
numbers positive?}
C -->|No| D[/Display: Both numbers must be positive/] --> E([End])
C -->|Yes| F["Call add_positive_numbers(num1, num2)"]
F --> G{Is b = 0?}
G -->|Yes| H[Return a]
G -->|No| I["Return add_positive_numbers(a + 1, b - 1)"]
I --> G
H --> J[Calculate final result]
J --> K[/Display: The sum is result/]
K --> E
%% Styling
style A fill:#f9f,stroke:#333,stroke-width:2px
style E fill:#f9f,stroke:#333,stroke-width:2px
style B fill:#90caf9,stroke:#333,stroke-width:2px
style D fill:#90caf9,stroke:#333,stroke-width:2px
style K fill:#90caf9,stroke:#333,stroke-width:2px
style C fill:#ffd700,stroke:#333,stroke-width:2px
style G fill:#ffd700,stroke:#333,stroke-width:2px
style F fill:#a5d6a7,stroke:#333,stroke-width:2px
style H fill:#a5d6a7,stroke:#333,stroke-width:2px
style I fill:#a5d6a7,stroke:#333,stroke-width:2px
style J fill:#a5d6a7,stroke:#333,stroke-width:2px
Important!
If you find any mistakes in my code or flowchart, please comment below this post. I will be happy to correct them and clear up any doubts you may have.