{"id":24103,"date":"2021-10-14T08:32:40","date_gmt":"2021-10-14T03:02:40","guid":{"rendered":"https:\/\/python-programs.com\/?p=24103"},"modified":"2021-11-05T20:53:29","modified_gmt":"2021-11-05T15:23:29","slug":"python-program-for-fmod-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-fmod-function\/","title":{"rendered":"Python Program for fmod() Function"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Dictionary setdefault() Method<\/a> The remainder (modulo) of x\/y is returned by the math.fmod() method.<\/p>\n The Python fmod() math function is used to compute the Modulo of the given arguments.<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n x:<\/strong> This is required. It is a positive or a negative number to divide.<\/p>\n y:<\/strong> This is required. It is a positive or negative number with which to divide x<\/p>\n Note:<\/strong><\/p>\n If both x and y are zero, a ValueError is returned. Return Value:<\/strong><\/p>\n It returns a float value which indicates the remainder of x\/y.<\/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 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 Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples<\/a>.<\/p>\n In the previous article, we have discussed Python Program for Dictionary setdefault() Method fmod() Function in Python: The remainder (modulo) of x\/y is returned by the math.fmod() method. The Python fmod() math function is used to compute the Modulo of the given arguments. Syntax: math.fmod(x, y) Parameters: x: This is required. It is a positive …<\/p>\n
\nfmod() Function in Python:<\/strong><\/p>\nmath.fmod(x, y)<\/pre>\n
\nIf y = 0, a ValueError is returned.
\nIf x or y is not a number, it returns a TypeError.<\/p>\nGiven list = [1, -2, -3, 4]\r\nGiven first number = 18 \r\nGiven second number = 2\r\nGiven number = 3<\/pre>\n
The remainder when given first number\/second number { 18 \/ 2 } = 0.0\r\nThe remainder when gvn_lst[2]\/given number { -3 \/ 5 } = -3.0\r\nThe remainder of -16\/5 = -1.0<\/pre>\n
Given list = [2, 4, 6, 8, 10]\r\nGiven first number = 8\r\nGiven second number = 5\r\nGiven number = 4<\/pre>\n
The remainder when given first number\/second number { 8 \/ 5 } = 3.0\r\nThe remainder when gvn_lst[3]\/given number { 8 \/ 4 } = 0.0\r\nThe remainder of -25\/-4 = -1.0<\/pre>\n
Program for fmod() Function in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [1, -2, -3, 4]\r\n# Give the first number as static input and store it in another variable.\r\ngvn_numb1 = 18\r\n# Give the second\u00a0number as static input and store it in another variable.\r\ngvn_numb2 = 2\r\n# Apply math.fmod() function to the given first and second number to get the\r\n# remainder of given first number\/given second number.\r\n# store it in another variable.\r\nremaindr = math.fmod(gvn_numb1, gvn_numb2)\r\n# Print the above result which is the result after applying fmod() function.\r\nprint(\r\n \"The remainder when given first number\/second number {\", gvn_numb1, \"\/\", gvn_numb2, \"} = \", remaindr)\r\n# Give the number as static input and store it in another variable.\r\ngvn_numb3 = 5\r\n# Apply math.fmod() function to the given list element and above given number and print it.\r\nprint(\r\n \"The remainder when gvn_lst[2]\/given number {\", gvn_lst[2], \"\/\", gvn_numb3, \"} = \", math.fmod(gvn_lst[2], gvn_numb3))\r\n# similarly do the same for other numbers and print it.\r\nprint(\"The remainder of -16\/5 = \", math.fmod(-16, 5))\r\n<\/pre>\n
The remainder when given first number\/second number { 18 \/ 2 } = 0.0\r\nThe remainder when gvn_lst[2]\/given number { -3 \/ 5 } = -3.0\r\nThe remainder of -16\/5 = -1.0<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
\nfunctions.<\/li>\n# Import math module using the import keyword.\r\nimport math\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give first and second numbers respectively as user input using int(),map(),input(),and split()\r\n# functions and store them in two separate variables.\r\ngvn_numb1, gvn_numb2 = map(int, input(\r\n \"Enter two random numbers separated by spaces = \").split())\r\n# Apply math.fmod() function to the given first and second number to get the\r\n# remainder of given first number\/given second number.\r\n# store it in another variable.\r\nremaindr = math.fmod(gvn_numb1, gvn_numb2)\r\n# Print the above result which is the result after applying fmod() function.\r\nprint(\r\n \"The remainder when given first number\/second number {\", gvn_numb1, \"\/\", gvn_numb2, \"} = \", remaindr)\r\n# Give the number as user input using the int(input()) function and store it in another variable.\r\ngvn_numb3 = int(input(\"Enter some random number = \"))\r\n# Apply math.fmod() function to the given list element and above given number and print it.\r\nprint(\r\n \"The remainder when gvn_lst[3]\/given number {\", gvn_lst[3], \"\/\", gvn_numb3, \"} = \", math.fmod(gvn_lst[3], gvn_numb3))\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 2 4 6 8 10\r\nEnter two random numbers separated by spaces = 8 5\r\nThe remainder when given first number\/second number { 8 \/ 5 } = 3.0\r\nEnter some random number = 4 \r\nThe remainder when gvn_lst[3]\/given number { 8 \/ 4 } = 0.0<\/pre>\n
\n