Given a number n, we need to count square numbers smaller than or equal to n.

**Examples :**

Input : n = 5 Output : Square Number : 2 Non-square numbers : 3 Explanation : Square numbers are 1 and 4. Non square numbers are 2, 3 and 5. Input : n = 10 Output : Square Number : 3 Non-square numbers : 7 Explanation : Square numbers are 1, 4 and 9. Non square numbers are 2, 3, 5, 6, 7, 8 and 10.

A **simple solution** is to traverse through all numbers from 1 to n and for every number check if n is perfect square or not.

An **efficient solution** is based on below formula.

**Count of square numbers** that are greater than 0 and smaller than or equal to n are floor(sqrt(n)) or **⌊√(n)⌋**

Count of non-square numbers = **n – ⌊√(n)⌋**

## C++

`// CPP program to count squares and ` `// non-squares before a number. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `countSquaresNonSquares(` `int` `n) ` `{ ` ` ` `int` `sc = ` `floor` `(` `sqrt` `(n)); ` ` ` `cout << ` `"Count of squares "` ` ` `<< sc << endl; ` ` ` `cout << ` `"Count of non-squares "` ` ` `<< n - sc << endl; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 10; ` ` ` `countSquaresNonSquares(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count squares and ` `// non-squares before a number. ` `import` `java.io.*; ` `import` `java.math.*; ` ` ` `class` `GFG ` `{ ` ` ` `static` `void` `countSquaresNonSquares(` `int` `n) ` ` ` `{ ` ` ` `int` `sc = (` `int` `)(Math.floor(Math.sqrt(n))); ` ` ` `System.out.println(` `"Count of"` `+ ` ` ` `" squares "` `+ sc); ` ` ` `System.out.println(` `"Count of"` `+ ` ` ` `" non-squares "` `+ ` ` ` `(n - sc) ); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `10` `; ` ` ` `countSquaresNonSquares(n); ` ` ` `} ` `} ` ` ` `// This code is contributed ` `// by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to count ` `# squares and non-squares ` `# before a number. ` `import` `math ` ` ` `def` `countSquaresNonSquares(n) : ` ` ` `sc ` `=` `(math.floor(math.sqrt(n))) ` ` ` `print` `(` `"Count of squares "` `, sc) ` ` ` `print` `(` `"Count of non-squares "` `, (n ` `-` `sc) ) ` ` ` ` ` `# Driver code ` `n ` `=` `10` `countSquaresNonSquares(n) ` ` ` `# This code is contributed ` `# by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count squares and ` `// non-squares before a number. ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `void` `countSquaresNonSquares(` `int` `n) ` `{ ` ` ` `int` `sc = (` `int` `)Math.Sqrt(n); ` ` ` `Console.WriteLine( ` `"Count of "` `+ ` ` ` `"squares "` `+ ` ` ` `sc) ; ` ` ` `Console.WriteLine(` `"Count of "` `+ ` ` ` `"non-squares "` `+ ` ` ` `(n - sc)); ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 10; ` ` ` `countSquaresNonSquares(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count ` `// squares and non-squares ` `// before a number. ` ` ` `// function to count squares ` `// and non-squares before a ` `// number ` `function` `countSquaresNonSquares(` `$n` `) ` `{ ` ` ` `$sc` `= ` `floor` `(sqrt(` `$n` `)); ` ` ` `echo` `(` `"Count of squares "` `. ` ` ` `$sc` `. ` `"\n"` `); ` ` ` `echo` `(` `"Count of non-squares "` `. ` ` ` `(` `$n` `- ` `$sc` `)); ` `} ` ` ` `// Driver code ` `$n` `= 10; ` `countSquaresNonSquares(` `$n` `); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

Count of squares 3 Count of non-squares 7

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of digits before the decimal point in the division of two numbers
- Count of multiples in an Array before every element
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Find an index such that difference between product of elements before and after it is minimum
- Minimum number of operations to move all uppercase characters before all lower case characters
- Check if a number is perfect square without finding square root
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Find smallest perfect square number A such that N + A is also a perfect square number
- Check if a number is a perfect square having all its digits as a perfect square
- Count numbers upto N which are both perfect square and perfect cube
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Count of subarrays having exactly K perfect square numbers
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count quadruplets(A, B, C, D) till N such that sum of square of A and B is equal to that of C and D
- Count of ways to represent N as sum of a prime number and twice of a square
- Sum of square of first n even numbers
- Sum of square of first n odd numbers
- Program to print non square numbers
- Sum of square-sums of first n natural numbers
- Print n numbers such that their sum is a perfect square

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.