0% found this document useful (0 votes)
8 views1 page

Exercise 4.3 - Using Functions

The document outlines an algorithm for a main program that utilizes functions to calculate factorials and identify prime numbers. It includes the creation of a factorial function, user input validation, and a loop to allow repeated calculations. Additionally, it describes a function to check for prime numbers and the implementation of user input features to enhance the program's functionality.

Uploaded by

1015014
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Exercise 4.3 - Using Functions

The document outlines an algorithm for a main program that utilizes functions to calculate factorials and identify prime numbers. It includes the creation of a factorial function, user input validation, and a loop to allow repeated calculations. Additionally, it describes a function to check for prime numbers and the implementation of user input features to enhance the program's functionality.

Uploaded by

1015014
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Using Functions

Your main program should follow the following algorithm:

while yes:
Code get and validate input
required for Input
perform task(s)
looping of Process
print results
program check if user wants to run program again Output
yes – continue looping
no – end program

1. a) There is a concept in mathematics called "factorial", which is the product


of all positive integers from the selected number down to 1. This
operation is denoted in mathematics with the exclamation point (!).
For example, 5! = 5 x 4 x 3 x 2 x 1 = 120
Create a function called factorial(n) that returns the factorial of a number.

b) Add a function called get_n() that asks the user for the value of n. This
function should also perform two error checks, looping until the user enters valid
input. The first error check is ensuring that the user enters an integer and the
second is to ensure that the number entered is greater than 0. The value of n
should be returned to the main program.

c) Write a main program with the following features:


o Loops until the user indicates that they are done
o Gets and validates user input
o Calculates the factorial
o Outputs the results

2. a) Create a boolean function called is_prime(num) that will return True if num
is prime and otherwise it will return False.

b) Write a main program that asks the user to specify a number, and display all of
the prime numbers between 1 and the user's number. This program will use your
function developed in part (a).

c) Add the following features:


• Write a function that gets and validates user input
• Loop the program

You might also like