{"id":18966,"date":"2021-08-29T15:12:58","date_gmt":"2021-08-29T09:42:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=18966"},"modified":"2021-11-22T18:37:10","modified_gmt":"2021-11-22T13:07:10","slug":"python-program-to-compute-1-n","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-compute-1-n\/","title":{"rendered":"Python Program to Compute 1\/N!"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Calculate the Surface Area and Volume of a Hemisphere<\/a> The product of all positive integers less than or equal to n is the factorial of a non-negative integer n, denoted by n! in mathematics:<\/p>\n n! = n * (n – 1) *(n – 2) * . . . . . . . . . . 3 * 2 * 1.<\/strong><\/p>\n 4 != 4 * 3 * 2 *1= 24<\/p>\n Given a number N<\/strong> and the task is to calculate the value of 1\/N!<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to calculate the value of 1\/N! :<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Calculate the Surface Area and Volume of a Hemisphere Factorial: The product of all positive integers less than or equal to n is the factorial of a non-negative integer n, denoted by n! in mathematics: n! = n * (n – 1) *(n – 2) …<\/p>\n
\nFactorial:<\/strong><\/p>\nGiven Number = 4<\/pre>\n
The value of 1\/n! with given n value{ 4 } = 0.041666666666666664<\/pre>\n
Given Number = 6<\/pre>\n
The value of 1\/n! with given n value{ 6 } = 0.001388888888888889<\/pre>\n
Program to Compute 1\/N! in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngven_nval = 4\r\n# Take a variable, initialize it with the value '1', and store it in another variable say\r\n# 'factorl'.\r\nfactorl = 1\r\n# Loop from 1 to the given number using the for loop.\r\nfor itr in range(1, gven_nval+1):\r\n # Inside the loop, calculate the factorial of a given number by multiplying the value of\r\n # the above variable \"factorl\" with the iterator value.\r\n # Store it in the same variable 'factorl'.\r\n factorl *= itr\r\n# Calculate the value of 1\/factorl and store it in another variable.\r\nfnl_reslt = 1.0\/factorl\r\n# Print the value of 1\/N! for the given number.\r\nprint(\"The value of 1\/n! with given n value{\", gven_nval, \"} =\", fnl_reslt)\r\n<\/pre>\n
The value of 1\/n! with given n value{ 4 } = 0.041666666666666664<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and store it in a variable.\r\ngven_nval = int(input(\"Enter some random Number = \"))\r\n# Take a variable, initialize it with the value '1', and store it in another variable say\r\n# 'factorl'.\r\nfactorl = 1\r\n# Loop from 1 to the given number using the for loop.\r\nfor itr in range(1, gven_nval+1):\r\n # Inside the loop, calculate the factorial of a given number by multiplying the value of\r\n # the above variable \"factorl\" with the iterator value.\r\n # Store it in the same variable 'factorl'.\r\n factorl *= itr\r\n# Calculate the value of 1\/factorl and store it in another variable.\r\nfnl_reslt = 1.0\/factorl\r\n# Print the value of 1\/N! for the given number.\r\nprint(\"The value of 1\/n! with given n value{\", gven_nval, \"} =\", fnl_reslt)\r\n<\/pre>\n
Enter some random Number = 6\r\nThe value of 1\/n! with given n value{ 6 } = 0.001388888888888889<\/pre>\n
\n