Python Program to Print Non Square Numbers

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 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.

math module :

The math module is a standard Python module that provides access to various mathematical functions for performing mathematical operations.

The math module in Python provides access to the following mathematical functions: exp(x), pow(x,y), log10(x), sqrt(x), and so on.

Examples:

Example1:

Input:

Given range = 8

Output:

The non square numbers in a given range = 
2
3
5
6
7

Example 2:

Input:

Given range = 15

Output:

The non square numbers in a given range = 
2
3
5
6
7
8
10
11
12
13
14

Program to Print Non-Square Numbers

Below are the ways to print the non-square numbers.

Method #1: Using math Module (Static input)

Approach:

  • Import math module using the import keyword.
  • Give the range as static input and store it in a variable.
  • Loop from 0 to given number using for loop.
  • Calculate the square root of the iterator value using math.sqrt() function and store it in another variable “sqr_rt”.
  • Check if the above obtained  “sqr_rt” multiplied with itself is equal to the iterator value using the if conditional statement.
  • If the statement is true, then increase the value of the iterator by ‘1’.
  • Else print the iterator value.
  • The Exit of the program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the range as static input and store it in a variable.
gvn_num = 15
# Loop from 0 to given number using for loop.
print("The non square numbers in a given range = ")
for itr in range(0, gvn_num):
  # Calculate the square root of the iterator value using math.sqrt() function and
  # store it in another variable "sqr_rt".
    sqr_rt = int(math.sqrt(itr))
# Check if the above obtained  "sqr_rt" multiplied with itself is equal to the iterator
# value using the if conditional statement.
    if itr == sqr_rt*sqr_rt:
   # If the statement is true, then increase the value of the iterator by '1'.
        itr = itr + 1
 # Else print the iterator value.
    else:
        print(itr)

Output:

The non square numbers in a given range = 
2
3
5
6
7
8
10
11
12
13
14

Method #2: Using math Module (User input)

Approach:

  • Import math module using the import keyword.
  • Give the range as user input using the int(input()) function and store it in a variable.
  • Loop from 0 to given number using for loop.
  • Calculate the square root of the iterator value using math.sqrt() function and store it in another variable “sqr_rt”.
  • Check if the above obtained  “sqr_rt” multiplied with itself is equal to the iterator value using the if conditional statement.
  • If the statement is true, then increase the value of the iterator by ‘1’.
  • Else print the iterator value.
  • The Exit of the program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the range as user input using the int(input()) function and store it in a variable.
gvn_num = int(input("Enter some random number = "))
# Loop from 0 to given number using for loop.
print("The non square numbers in a given range = ")
for itr in range(0, gvn_num):
  # Calculate the square root of the iterator value using math.sqrt() function and
  # store it in another variable "sqr_rt".
    sqr_rt = int(math.sqrt(itr))
# Check if the above obtained  "sqr_rt" multiplied with itself is equal to the iterator
# value using the if conditional statement.
    if itr == sqr_rt*sqr_rt:
       # If the statement is true, then increase the value of the iterator by '1'.
        itr = itr + 1
 # Else print the iterator value.
    else:
        print(itr)

Output:

Enter some random number = 8
The non square numbers in a given range = 
2
3
5
6
7

Here we printed the nonperfect square numbers.

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.