بخشی از مجموعه

Several example codes

~1 دقیقه مطالعه • بروزرسانی ۲۰ مهر ۱۴۰۴

Program Overview

This Python program uses a recursive method named l1 to display all numbers less than n that are divisible by 3.
The recursion starts from n-1 and checks each number downward.


Python Code:


def l1(n: int):
    if n <= 0:
        return
    if n % 3 == 0:
        print(n)
    l1(n - 1)

# Read input from user
n = int(input("Enter value for n: "))
print(f"Multiples of 3 less than {n}:")
l1(n - 1)

Sample Output (input: n = 11):


Multiples of 3 less than 11:
9  
6  
3

نوشته و پژوهش شده توسط دکتر شاهین صیامی