Program to find sum of first N odd numbers in Python

PythonServer Side ProgrammingProgramming

Suppose we have a number n, we have to find the sum of the first n positive odd integers.

So, if the input is like n = 10, then the output will be 100, as the first 10 odd integers are [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] and its sum is 100.

To solve this, we will follow these steps −

  • There is a nice observation, for first n odd numbers, the sum is always square of n.
  • So to get the result, return n*n

Example

Let us see the following implementation to get better understanding −

def solve(n):
   return n*n
n = 10
print(solve(n))

Input

10

Output

100
raja
Updated on 14-Oct-2021 10:21:01

Advertisements