Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Print the Sum of all odd numbers from 1 to N
- n = input("Enter a Integer number, bigger from 1: ")
- n = int(n)
- sum = 0
- # Here using while loop:
- #
- # counter = 0
- # while counter < n:
- # counter += 1
- # if counter % 2 != 0:
- # sum += counter
- # The better way is with for loop:
- for i in range(1, n + 1):
- if i % 2 != 0:
- sum += i
- print('The Sum of all odd numbers from 1 to {0} is: {1}' .format(n, sum))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement