# Print n numbers such that their sum is a perfect square

Given an integer **n**, the task is to print **n** numbers such that their sum is a perfect square.

**Examples:**

Input:n = 3

Output:1 3 5

1 + 3 + 5 = 9 = 3^{2}

Input:n = 4

Output:1 3 5 7

1 + 3 + 5 + 7 = 16 = 4^{2}

**Approach:** The sum of first **n** odd numbers is always a perfect square. So, we will print the first **n** odd numbers as the output.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print n numbers such that ` `// their sum is a perfect square ` `void` `findNumbers(` `int` `n) ` `{ ` ` ` `int` `i = 1; ` ` ` `while` `(i <= n) { ` ` ` ` ` `// Print ith odd number ` ` ` `cout << ((2 * i) - 1) << ` `" "` `; ` ` ` `i++; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` `findNumbers(n); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Function to print n numbers such that ` ` ` `// their sum is a perfect square ` ` ` `static` `void` `findNumbers(` `int` `n) ` ` ` `{ ` ` ` `int` `i = ` `1` `; ` ` ` `while` `(i <= n) { ` ` ` ` ` `// Print ith odd number ` ` ` `System.out.print(((` `2` `* i) - ` `1` `) + ` `" "` `); ` ` ` `i++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` `findNumbers(n); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to print n numbers such that ` `# their sum is a perfect square ` `def` `findNumber(n): ` ` ` `i ` `=` `1` ` ` `while` `i <` `=` `n: ` ` ` ` ` `# Print ith odd number ` ` ` `print` `((` `2` `*` `i) ` `-` `1` `, end ` `=` `" "` `) ` ` ` `i ` `+` `=` `1` ` ` `# Driver code ` `n ` `=` `3` `findNumber(n) ` ` ` `# This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `public` `class` `GFG { ` ` ` ` ` `// Function to print n numbers such that ` ` ` `// their sum is a perfect square ` ` ` `public` `static` `void` `findNumbers(` `int` `n) ` ` ` `{ ` ` ` `int` `i = 1; ` ` ` `while` `(i <= n) { ` ` ` ` ` `// Print ith odd number ` ` ` `Console.Write(((2 * i) - 1) + ` `" "` `); ` ` ` `i++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(` `string` `[] args) ` ` ` `{ ` ` ` `int` `n = 3; ` ` ` `findNumbers(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to prn numbers such that ` `// their sum is a perfect square ` `function` `findNumbers(` `$n` `) ` `{ ` ` ` `$i` `= 1; ` ` ` `while` `(` `$i` `<= ` `$n` `) ` ` ` `{ ` ` ` ` ` `// Print ith odd number ` ` ` `echo` `((2 * ` `$i` `) - 1) . ` `" "` `; ` ` ` `$i` `++; ` ` ` `} ` `} ` ` ` `// Driver code ` `$n` `= 3; ` `findNumbers(` `$n` `); ` ` ` `// This code contributed by PrinciRaj1992 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1 3 5

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- 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
- Sum of all perfect square divisors of numbers from 1 to N
- Count of subarrays having exactly K perfect square numbers
- Check if product of array containing prime numbers is a perfect square
- Print N numbers such that their sum is a Perfect Cube
- Print N numbers such that their product is a Perfect Cube
- Find smallest perfect square number A such that N + A is also a perfect square number
- Program to print non square numbers
- 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
- Perfect Square String
- Check if given number is perfect square
- Largest number that is not a perfect square
- Count of subarrays whose sum is a perfect square
- Closest perfect square and its distance
- Find the Next perfect square greater than a given number
- Count of pairs in an array whose sum is a perfect square
- Largest factor of a given number which is a perfect square
- Largest perfect square number in an Array

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.