# 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 = 32

Input: n = 4
Output: 1 3 5 7
1 + 3 + 5 + 7 = 16 = 42

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `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); ` `} `

## 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); ` `    ``} ` `} `

## 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 `

## 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 `

## PHP

 ` `

Output:

```1 3 5
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : shrikanth13, princiraj1992

Article Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.