Question

In: Computer Science

Exercise: Write a program named factorial.py that contains the following two functions: def while_factorial(num) def for_factorial(num)...

Exercise: Write a program named factorial.py that contains the following two functions:

def while_factorial(num)

def for_factorial(num)

These should calculate the factorial of a given number represented by the argument num using a while loop and a for loop respectively.

Solutions

Expert Solution

#function for calculating factorial using while loop
def while_factorial(num):
   f=1
   c=1
   while(c<=num):
       f=f*c
       c=c+1
  
   print("while_factorial: factorial of {} is {}".format(num,f))

#function for calculating factorial using while loop
def for_factorial(num):
   f=1
   for c in range(1,num+1,1):
       f=f*c
   print("for_factorial: factorial of {} is {}".format(num,f))

num=int(input("Enter number for factorial :"))   #reading number from user
#displaying factorial value using functions
while_factorial(num)
for_factorial(num)

-------------------------------------------------------------------------


Related Solutions

Write a program that contains 2 functions. Program will call a function named calc_commission that prompt...
Write a program that contains 2 functions. Program will call a function named calc_commission that prompt the user to enter the sales amount and computes and prints with a description the commission paid to salesperson as follows: 10% for sales amount less than $2,000.00, 15% for sales amount less than $10,000.00 and 20% for sales amount less than $20,000.00, then function calc_commission calls another function name assign_base_salary() to ask the user to enter each of 5 salesperson’s base salary ,...
Write a program that contains the following Write a function copies with two int parameters, named...
Write a program that contains the following Write a function copies with two int parameters, named n and x. It should dynamically allocate a new array of size n.  The function should then copy the value in x to every position in the array. The function should return a pointer to the new array. In the main function, call the copies function from part 1. with size 5 and value -1.  Output the resulting array to the screen, and deallocate the array....
JAVA program. Write two public classes (named exactly), TextBox and TextBoxTester.  TextBox contains the following overloaded static...
JAVA program. Write two public classes (named exactly), TextBox and TextBoxTester.  TextBox contains the following overloaded static methods called textBoxString. This method returns a String value. public static String textBoxString (int side) The returned String value, when printed, displays as the outline of a square of side characters. The character you use is up to you. Don't forget that '\n' will force a newline character into the returned String. For example, let's assume I want to use * as the character...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program that displays the following menu: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a rectangle 3. Calculate the area of a triangle 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for the radius of the circle and then display the area. Use the following formula to calculate the circle’s area: ?...
Python program please no def, main, functions Given a list of negative integers, write a Python...
Python program please no def, main, functions Given a list of negative integers, write a Python program to display each integer in the list that is evenly divisible by either 5 or 7. Also, print how many of those integers were found. Sample input/output: Enter a negative integer (0 or positive to end): 5 Number of integers evenly divisible by either 5 or 7: 0 Sample input/output: Enter a negative integer (0 or positive to end): -5 -5 is evenly...
Introduction to Inheritance (Exercise 1) Write the class named Horse that contains data fields for the...
Introduction to Inheritance (Exercise 1) Write the class named Horse that contains data fields for the name, color, and birth year. Include get and set methods for these fields. Next, finish the subclass named RaceHorse, which contains an additional field that holds the number of races in which the horse has competed and additional methods to get and set the new field. Run the provided DemoHorses application that demonstrates using objects of each class. DemoHorses.java public class DemoHorses { public...
Create a Python file named num_sum.py that contains: The definition of two functions: volume - accepts...
Create a Python file named num_sum.py that contains: The definition of two functions: volume - accepts three parameters and returns the product of them, but displays nothing sum_of_nums - accepts 1 or more numbers and returns their sum, but displays nothing A print function call that includes a call to volume, passing 2, 3, and 4 A print function call that includes a call to sum_of_nums, passing 1, 2, 3, 4, and 5
Write a C++ program (The Account Class) Design a class named Account that contains (keep the...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate....
Write a program that contains a main method and another method named userName. The main method...
Write a program that contains a main method and another method named userName. The main method should prompt the user to enter a full name. The userName method takes the full name as an argument and prints the name in reverse order and returns the number of characters in the name. See Sample Output (input shown in blue). Sample Output Please enter your FULL name Billy Joe McCallister Here is the name Billy Joe McCallister in reverse: retsillaCcM eoJ ylliB...
Write a program named "cleanDocument.pl" that will do the following: Reads two parameters from the command...
Write a program named "cleanDocument.pl" that will do the following: Reads two parameters from the command line (@ARGV) The first parameter is the name of a file and the second parameter is a word If no command line parameters are present, it asks the user to input the values Opens the text file that is passed as the first command line argument Loops through the lines of the text file and replaces all occurrence of the word (the second command...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT