{"id":24310,"date":"2021-10-14T09:18:11","date_gmt":"2021-10-14T03:48:11","guid":{"rendered":"https:\/\/python-programs.com\/?p=24310"},"modified":"2021-11-05T21:06:05","modified_gmt":"2021-11-05T15:36:05","slug":"python-program-for-cos-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-cos-function\/","title":{"rendered":"Python Program for cos() Function"},"content":{"rendered":"
In the previous article, we have discussed Python Program for atan2() Function<\/a> The cosine of a number is returned by the math.cos() method.<\/p>\n The Trigonometry Cosine function’s mathematical formula is<\/p>\n cos(A)= adjacent side of A\/ Hypotenuse<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n x:<\/strong> This is required. It is a number. A number for which the cosine must be calculated. If the value is not a number, a TypeError is returned.<\/p>\n Return Value:<\/strong><\/p>\n Returns a float value ranging from -1 to 1 that represents the cosine of an angle.<\/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 Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples<\/a> well explained step by step.<\/p>\n In the previous article, we have discussed Python Program for atan2() Function cos() Function in Python: The cosine of a number is returned by the math.cos() method. The Trigonometry Cosine function’s mathematical formula is cos(A)= adjacent side of A\/ Hypotenuse Syntax: math.cos(x) Parameters: x: This is required. It is a number. A number for which …<\/p>\n
\ncos() Function in Python:<\/strong><\/p>\nmath.cos(x)<\/pre>\n
\n
Given first number = 1\r\nGiven second number = 45<\/pre>\n
The result after applying cos() function on above given first number 1 = 0.5403023058681398\r\nThe result after applying cos() function on above given second number 45 = 0.5253219888177297<\/pre>\n
Given first number = 60\r\nGiven second number = 10<\/pre>\n
The result after applying cos() function on above given first number 60 = -0.9524129804151563\r\nThe result after applying cos() function on above given second number 10 = -0.8390715290764524<\/pre>\n
Program for cos() 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 = [10, 15, 20, 30, 35]\r\n# Give the first number as static input and store it in another variable.\r\ngvn_numb1 = 1\r\n# Apply math.cos() function to the given first number to get the cosine value\r\n# of a given number.\r\n# Store it in another variable.\r\nfnl_rslt = math.cos(gvn_numb1)\r\nprint(\"The result after applying cos() function on above given first number\",\r\n gvn_numb1, \" = \", fnl_rslt)\r\n# similarly do the same for the other number.\r\ngvn_numb2 = 45\r\nprint(\"The result after applying cos() function on above given second number\",\r\n gvn_numb2, \" = \", math.cos(gvn_numb2))\r\n# Apply math.cos() function to the given list element and print it.\r\nprint(\r\n \"The result after applying cos() function on given list element gvnlst[1] = \", math.cos(gvn_lst[1]))\r\n<\/pre>\n
The result after applying cos() function on above given first number 1 = 0.5403023058681398\r\nThe result after applying cos() function on above given second number 45 = 0.5253219888177297\r\nThe result after applying cos() function on given list element gvnlst[1] = -0.7596879128588213<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\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(float, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the first number as user input using the float(input()) function\r\n# and store it in a variable.\r\ngvn_numb1 = float(input(\"Enter some random number = \"))\r\n# Apply math.cos() function to the given first number to get the cosine value\r\n# of a given number.\r\n# Store it in another variable.\r\nfnl_rslt = math.cos(gvn_numb1)\r\nprint(\"The result after applying cos() function on above given first number\",\r\n gvn_numb1, \" = \", fnl_rslt)\r\n# similarly do the same for the other number.\r\ngvn_numb2 = float(input(\"Enter some random number = \"))\r\nprint(\"The result after applying cos() function on above given second number\",\r\n gvn_numb2, \" = \", math.cos(gvn_numb2))\r\n# Apply math.cos() function to the given list element and print it.\r\nprint(\r\n \"The result after applying cos() function on given list element gvnlst[2] = \", math.cos(gvn_lst[2]))\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 10 15 5 6 0\r\nEnter some random number = 60\r\nThe result after applying cos() function on above given first number 60.0 = -0.9524129804151563\r\nEnter some random number = 20.5\r\nThe result after applying cos() function on above given second number 20.5 = -0.07956356727854007\r\nThe result after applying cos() function on given list element gvnlst[2] = 0.28366218546322625<\/pre>\n
\n