When whole numbers divided by 2
leave a remainder of 1
, they are called odd numbers.
Example: 1, 3, 5, 7…
Let n
be any number and initialize sum
and count
values to 0.
An empty list l
is taken to append the values of odd numbers and variable i
is initialized to 1.
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
.
The For
loop is used to iterate the list l
. The elements in list l
are added and stored in the variable sum
.
sum
contains the sum of the first n
odd integers.
n=10sum=0count=0l=[]i=1while(count<n):if(i%2!=0):l.append(i)i=i+2count=count+1for i in l:sum=sum+iprint(sum)