Find the Nth_Non_Square_Number using C++


We all know about the numbers that are not square of any number like 2, 3, 5, 7, 8, etc. There are Nth numbers of non-square numbers, and it is impossible to know every number. So In this article, we will explain everything about the square-free number or non-square number and what are the ways to find Nth non-square number in C++.

Nth Non-Square Number

A number is said to be a perfect square if it is a square of an integer. Some example of perfect square numbers are −

1 is square of 1
4 is square of 2
9 is square of 3
16 is square of 4
25 is square of 5

A number is said to be a non-square number if it is not a square of any integer. For example, the first 15 non-square numbers are −

2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19

How to find Nth non square number?

So here is the example to find the Nth non-square number −

Input : 2
Output : 3
Explanation : 2nd Non square number is 3 (after 2 which is first non square number)

Input : 5
Output : 7
Explanation : 7th Non square number is 7 ( after 2,3,5,6 which are first four non square

After looking at the above example, we can come up with a solution that For finding Nth non-square number, we need to start counting for nth number and check each integer whether it is a perfect square or not and do not count number which is a perfect square, i.e., do counting if the number is a perfect square.

Creating a C + + Program to Find Nth Non-Square Number

We have created a complete syntax for finding an Nth non-square number in C++.

Example

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin >> n; // Taking input from the user.
    int i = 2; // as 0 and 1 are squares of themselves so we start calculating from 2.
    int cnt = 0; // declaring counter variable;
    while(cnt != n){// the loop will terminate when out counter will have the same value as n.
        int a = sqrt(i);
        if(i != a*a)
            cnt++;
        if(cnt != n)
            i++;
    }
    cout << i << "\n"; // printing the nth non square number.
}

Output

5

(When we provide 3 as an input, then we get 5 as an output)

Let us have a brief explanation of the above codes.

Step 1 − Taking input from user and setting count to 0.

cin >> n; // Taking input from the user.
int i = 2; // as 0 and 1 are squares of themselves so we start calculating from 2.
int cnt = 0; // declaring counter variable;

Step 2 − Counting non-square numbers and skipping square numbers.

while(cnt != n) // the loop will terminate when out counter will have the same value as n.{
   int a = sqrt(i); // finding square root using sqrt() function.
   if(i != a*a) // check whether the number is a perfect square or not.
      cnt++; // incrementing counter if found non perfect number.
      if(cnt != n)
   i++;
}

Step 3 − Printing Nth square number.

cout << i << "\n"; // printing the nth non square number.

Conclusion

In this article, we explained about a non-square number and ways to find Nth non-square numbers in C++. Apart from C++, we can use this program in different programming languages such as Java, Python, C, or any other. We hope you find this article helpful and informative as we have described everything in the simplest way possible.

Updated on: 23-Nov-2021

142 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements