
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find sum of first N odd numbers in Python
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
- Related Articles
- Program to find the sum of first n odd numbers in Python
- Find the sum of first $n$ odd natural numbers.
- Swift Program to calculate the sum of first N odd numbers
- Sum of square of first n odd numbers
- Program to find sum of first n natural numbers in C++
- 8085 program to find the sum of first n natural numbers
- Python Program for cube sum of first n natural numbers
- Python Program for Sum of squares of first n natural numbers
- Sum of first n natural numbers in C Program
- Average of first n odd naturals numbers?
- Java Program to cube sum of first n natural numbers
- Java Program to Display Numbers and Sum of First N Natural Numbers
- PHP program to find the sum of cubes of the first n natural numbers
- Swift Program to calculate the sum of all odd numbers up to N
- Haskell Program to calculate the sum of all odd numbers up to N

Advertisements