[Type B] Working with Functions – Chapter 3 Sumita Arora

|
Table of Contents

16. Write a program with a function that takes an integer and prints the number that follows after it: Call the function with these arguments: 4, 6, 8, 2+1, 4-3*2, 3-2.

Solution:

Python
def next_num(num):
    next_num = num + 1
    print("Next number after", num, "is", next_num)

# Call the function with the provided arguments
next_num(4)
next_num(6)
next_num(8)
next_num(2 + 1)
next_num(4 - 3 * 2)
next_num(3 - 2)
Output
Next number after 4 is 5
Next number after 6 is 7
Next number after 8 is 9
Next number after 3 is 4
Next number after -2 is -1
Next number after 1 is 2

Short Answer: The program defines a function print_next_number that takes an integer number as input and prints the number that follows after it. The function is called with the provided arguments: 4, 6, 8, 2+1, 4-3*2, and 3-2. The output displays the next number for each input.

Leave a Reply

Your email address will not be published. Required fields are marked *