Question

Exercise: Finding the Factorial Write a program so a user enters a positive integer N and outputs N1 (meaning N factorial, gi
***answer using pythin***
***include indentation in the program if necessary***
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Pre-requisite:

1) Code is been run in python 3

2) You have "sys" and "re" modules installed in your python version

How to run the program:

Save the provided code in some file and execute the saved file using python interpreter. The script will accept a single input on every run thus execute the script for every different input.

below is the snip when you execute the script:

$ ./factorial.py Enter a positive integer: 3

You can see above that on executing the script it prompts for the input, and I have provided "3" as input, as soon as you provide the input, press enter and you would see the result on you console.

CODE PASTED AS BELOW

fron sys import exit import re def checkInt(iten): # check if the value is an integer or not # return True if an positive int

OUTPUT OF THE PROGRAM:

$ ./factorial.py Enter a positive integer: 1 1! = 1 $ ./factorial.py Enter a positive integer: 2 2! = 2 $ ./factorial.py Ente

CODE TO COPY:

from sys import exit

import re

def checkInt(item):

        # check if the value is an integer or not

        # return True if an positive integer

        # else return False if not an integer

        # we are also excluding '0' from the allowed inputs

        if item.isdigit() and int(item) != 0:

                return True

        elif re.match(r"\d+\.*\d*", item) or re.match(r"-\d+\.*\d*", item):

                return False

        else:

                return False

# Accept an value as input from the user

N = input("Enter a positive integer: ")

# check if the provided input is a positive non-zero integer

if not checkInt(N):

        # if not a valid input then print the error message and exit the script

        print("Please enter a positive integer")

        exit(1)

# store the value into a temp variable to be used later in printing the output

num = N

# initialize the factorial variable to be equal to the value been input

# also doing an int assignment to "fact" variable so that later on there is no type error while

# performing mathematical operations

fact = int(N)

# looping until the input value is not zero, as we would be subtracting the

# input integer by 1 on every iteration

while N != 0:

        # in case the value is 1 then we break from the loop, as we do not want to further

        # decrement the number as if done then the resultant will come to zero

        if int(N) == 1:

                break

        # fact variable was assigned the value of the input integer before the start of this while block

        # lets suppose 6 was entered as input then 'fact' variable got an assignment to 6 before this while block

        # now below line will just go on multiplying the decremented input integer value on each iteration

        # thus this will further go on storing as 5 * 4 * 3 * 2 ( assuming 6 was the input string )

        fact = fact * (int(N) - 1)

        # decrement the value of 'N' by 1 on each iteration

        N = int(N) - 1

# print the factorial result

print("%s! = %d" %(num,fact))

Add a comment
Know the answer?
Add Answer to:
***answer using pythin*** ***include indentation in the program if necessary*** Exercise: Finding the Factorial Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4.25 Lab9d: Factorials This program will output the factorial of numbers 1 through 10. Recall that...

    4.25 Lab9d: Factorials This program will output the factorial of numbers 1 through 10. Recall that 10! = 10 * 9 * 8 * 7 * … * 2 * 1. Calculating the factorial will require two loops. The outer loop will iterate from the number 1 up to and including the number n. n will be based on a number the user entered (an int value). The inner loop will calculate the factorial of the number the first loop...

  • // Group Names: // Date: // Program Description: //   Import required packages //--> //   Declare class...

    // Group Names: // Date: // Program Description: //   Import required packages //--> //   Declare class (SwitchDoLab) //--> {    //   Declare the main method    //-->    {        //   Declare Constant integers SUM = 1, FACTORIAL = 2, QUIT = 3.        //-->        //-->        //-->        //   Create an integer variable named choice to store user's option.        //-->        //   Create a Scanner object        //   Create...

  • Please use python to write the simple program. Exercise 2 - Summation Write a program to...

    Please use python to write the simple program. Exercise 2 - Summation Write a program to accept integer inputs until the user inputs a non-integer. Then, the program prints the summation of the inputted numbers. Hint: Use the function input() to get the user input. Use a variable total for calculating the summation. Need to use while loop for the repeated inputs. Use if statement with isdigit() function to check whether the user input is an integer or not. if...

  • USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by...

    USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by the user. Remember that x! =x* (x-1)* (x-2)*... *3+ 2* 1. Your program should check the value input by the user to ensure it is valid (i.e., that it is a number > =1). To do this, consider looking at the is digit() function available in Python. If the user enters an incorrect input, your program should continue to ask them to enter a...

  • Factorial.java This is an exercise in using repetition structures to calculate the mathematical values of the...

    Factorial.java This is an exercise in using repetition structures to calculate the mathematical values of the factorial function. Since methods have not yet been covered in detail, you may do the calculations in a class that contains just a main method. The looping structures require the use of count-controlled loops. This program computes the factorial function of a non-negative integer, which is written mathematically as n! (the parameter n followed by an exclamation mark). For values of n equal to...

  • Finish the following program which adds up all integers from 0 to the user's given number inclusively using a While Loop

    // Finish the following program which adds up all integers from 0 to// the user's given number inclusively using a While Loop. The total should be// assigned to the variable 'total'.#includeusing namespace std;int main() {int number;int total = 0;int counter = 0; //initialize the variable// user enters a numbercout << "Enter a positive integer to find the summation of ";cout << "all numbers from 0 to the given number up to 100." << endl;cin >> number;// check for invalid user...

  • Write a program that receives a positive integer n from the user and then calculates the...

    Write a program that receives a positive integer n from the user and then calculates the sum below by using a loop. Your program needs to ask the user to re-enter a different number if the user enters a non-positive one. Display the result. 1 SUM 1 2 3 ... n x x n = = = + + + +

  • C++ coding answer 5. Write a program that asks the user for a positive integer value....

    C++ coding answer 5. Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. Input Validation: Do not accept a negative starting number.

  • Write a working C program that will do the following: 1. Include your name, Lab Test...

    Write a working C program that will do the following: 1. Include your name, Lab Test number, and student id as comments in the first lines of the program 2. User is first prompted to enter an integer number between 1 and 20, to be stored as variable named number. If the number is outside the range 1-20, the program will end. 3. If the number is within the allowed range, variable number is passed to function multF(). The multF()...

  • Write a working C program that will do the following: 1. Include your name, Lab Test...

    Write a working C program that will do the following: 1. Include your name, Lab Test number, and studentId as comments in the first lines of the program 2. User is first prompted to enter an integer number between 1 and 20, to be stored as variable named number. If the number is outside the range 1-20, the program will end. 3. If the number is within the allowed range, variable number is passed to function multF(). The multF() function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT