Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
community creator

How to find sum of first n odd integers

aishwarya chandupatla

When whole numbers divided by 2 leave a remainder of 1, they are called odd numbers.

Example: 1, 3, 5, 7…

Code

  1. Let n be any number and initialize sum and count values to 0.

  2. An empty list l is taken to append the values of odd numbers and variable i is initialized to 1.

  3. The While loop is used to check whether the number is even or odd. If a number is odd, it is appended to the list l.

  4. The For loop is used to iterate the list l. The elements in list l are added and stored in the variable sum.

  5. sum contains the sum of the first n odd integers.

n=10
sum=0
count=0
l=[]
i=1
while(count<n):
     if(i%2!=0):
        l.append(i)
        i=i+2
        count=count+1
for i in l:
    sum=sum+i
print(sum)

RELATED TAGS

python
community creator

CONTRIBUTOR

aishwarya chandupatla
RELATED COURSES

View all Courses

Keep Exploring