{"id":17123,"date":"2021-08-17T08:48:30","date_gmt":"2021-08-17T03:18:30","guid":{"rendered":"https:\/\/python-programs.com\/?p=17123"},"modified":"2021-11-22T18:37:22","modified_gmt":"2021-11-22T13:07:22","slug":"python-program-to-print-non-square-numbers","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-non-square-numbers\/","title":{"rendered":"Python Program to Print Non Square Numbers"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Remove any Non-ASCII Characters<\/a> Non-square numbers are those that are not perfect squares of any number. When you multiply any number by itself, you get a square number. A non-square number is the inverse of the same. For example, 16 is a square number because it can be written as 4 x 4, whereas 7 is not. Other non-square numbers include 2, 3, 5, 6, 7, 8, and 10.<\/p>\n math module :<\/strong><\/p>\n The math module is a standard Python module that provides access to various mathematical functions for performing mathematical operations.<\/p>\n The math module in Python provides access to the following mathematical functions: exp(x), pow(x,y), log10(x), sqrt(x), and so on.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to print the non-square numbers.<\/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 Here we printed the nonperfect square numbers.<\/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 Remove any Non-ASCII Characters Non-square numbers : Non-square numbers are those that are not perfect squares of any number. When you multiply any number by itself, you get a square number. A non-square number is the inverse of the same. For example, 16 is a …<\/p>\n
\nNon-square numbers :<\/strong><\/p>\nGiven range = 8<\/pre>\n
The non square numbers in a given range = \r\n2\r\n3\r\n5\r\n6\r\n7<\/pre>\n
Given range = 15<\/pre>\n
The non square numbers in a given range = \r\n2\r\n3\r\n5\r\n6\r\n7\r\n8\r\n10\r\n11\r\n12\r\n13\r\n14<\/pre>\n
Program to Print Non-Square Numbers<\/h2>\n
\n
Method #1: Using math Module (Static input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the range as static input and store it in a variable.\r\ngvn_num = 15\r\n# Loop from 0 to given number using for loop.\r\nprint(\"The non square numbers in a given range = \")\r\nfor itr in range(0, gvn_num):\r\n # Calculate the square root of the iterator value using math.sqrt() function and\r\n # store it in another variable \"sqr_rt\".\r\n sqr_rt = int(math.sqrt(itr))\r\n# Check if the above obtained\u00a0 \"sqr_rt\" multiplied with itself is equal to the iterator\r\n# value using the if conditional statement.\r\n if itr == sqr_rt*sqr_rt:\r\n # If the statement is true, then increase the value of the iterator by '1'.\r\n itr = itr + 1\r\n # Else print the iterator value.\r\n else:\r\n print(itr)\r\n<\/pre>\n
The non square numbers in a given range = \r\n2\r\n3\r\n5\r\n6\r\n7\r\n8\r\n10\r\n11\r\n12\r\n13\r\n14<\/pre>\n
Method #2: Using math Module (User input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the range as user input using the int(input()) function and store it in a variable.\r\ngvn_num = int(input(\"Enter some random number = \"))\r\n# Loop from 0 to given number using for loop.\r\nprint(\"The non square numbers in a given range = \")\r\nfor itr in range(0, gvn_num):\r\n # Calculate the square root of the iterator value using math.sqrt() function and\r\n # store it in another variable \"sqr_rt\".\r\n sqr_rt = int(math.sqrt(itr))\r\n# Check if the above obtained\u00a0 \"sqr_rt\" multiplied with itself is equal to the iterator\r\n# value using the if conditional statement.\r\n if itr == sqr_rt*sqr_rt:\r\n # If the statement is true, then increase the value of the iterator by '1'.\r\n itr = itr + 1\r\n # Else print the iterator value.\r\n else:\r\n print(itr)\r\n<\/pre>\n
Enter some random number = 8\r\nThe non square numbers in a given range = \r\n2\r\n3\r\n5\r\n6\r\n7<\/pre>\n
\n