Sample Paper 2 by Teachoo

Computer Science - Class 11
Teachoo Sample Paper

## Write a pseudocode to calculate and display the factorial of a positive integer n entered by the user. Assume that n is less than or equal to 20 and the factorial function is not predefined.

``` n, fact, i: integer ```

``` write "Enter a positive integer n" ```

``` read n ```

``` fact = 1 ```

``` for i = 1 to n ```

```     fact = fact * i ```

``` end for ```

``` write "The factorial of", n, "is", fact ```

• The pseudocode is a way of writing the steps of an algorithm in a simple and human-readable form.
• The pseudocode follows some conventions such as using indentation, keywords, comments, and symbols to make it clear and easy to understand.
• The pseudocode for calculating and displaying the factorial of a positive integer n is explained below:
• First, we declare the variables that we will use in the algorithm. We need three variables: n, fact, and i. We declare them as integers using the colon symbol (:).
• Next, we input the value of n from the user using the write and read statements. We write a message to prompt the user to enter a positive integer n and then we read the value entered by the user and store it in the variable n.
• Then, we initialize the variable fact to 1. This variable will store the factorial of n as we calculate it in the loop. We use the equal sign (=) to assign a value to a variable.
• After that, we use a for loop to iterate from 1 to n and multiply fact by i in each iteration. The for loop has a starting value, an ending value, and an increment value. In this case, we start from 1, end at n, and increment by 1. We use the keyword for to start the loop and the keyword end for to end the loop. We also use indentation to show the body of the loop. In each iteration, we update the value of fact by multiplying it by i using the asterisk symbol (*). This way, we calculate the factorial of n as the product of all integers from 1 to n.
• Finally, we display the factorial of n using the write statement. We write a message that shows the value of n and its factorial separated by commas.

So, the pseudocode is:

``` // Declare variables ```

``` n, fact, i: integer ```

``` // Input n from user ```

``` write "Enter a positive integer n" ```

``` read n ```

``` // Initialize fact to 1 ```

``` fact = 1 ```

``` // Loop from 1 to n and multiply fact by i ```

``` for i = 1 to n ```

```     fact = fact * i ```

``` end for ```

``` // Display the factorial of n ```

``` write "The factorial of", n, "is", fact ```

Learn in your speed, with individual attention - Teachoo Maths 1-on-1 Class 