Module: M3-R5: Python Programming
Chapter: Ch1 Computer Intro
The Top-down approach is a method of problem solving where a complex problem is broken down into smaller and simpler sub-problems. Each sub-problem is solved individually, and the results are combined to form the complete solution.
Also called Stepwise Refinement, this approach starts from a high-level overview of the problem and gradually refines it into smaller modules until each can be easily solved.
# Example of Top-down Problem Solving (Python)
def get_marks():
marks = []
for i in range(3):
m = int(input(f"Enter marks for subject {i+1}: "))
marks.append(m)
return marks
def calculate_total(marks):
return sum(marks)
def calculate_percentage(total, subjects):
return total / subjects
def display_result(total, percentage):
print(f"Total Marks: {total}")
print(f"Percentage: {percentage:.2f}%")
def main():
marks = get_marks()
total = calculate_total(marks)
percentage = calculate_percentage(total, len(marks))
display_result(total, percentage)
main()
Output (Sample):
Enter marks for subject 1: 80
Enter marks for subject 2: 90
Enter marks for subject 3: 85
Total Marks: 255
Percentage: 85.00%