Advertisement
dimipan80

Sum of All Odd numbers from 1 to N

Aug 31st, 2015
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.44 KB | None | 0 0
  1. # Print the Sum of all odd numbers from 1 to N
  2.  
  3. n = input("Enter a Integer number, bigger from 1: ")
  4. n = int(n)
  5.  
  6. sum = 0
  7.  
  8. # Here using while loop:
  9. #
  10. # counter = 0
  11. # while counter < n:
  12. #     counter += 1
  13. #     if counter % 2 != 0:
  14. #         sum += counter
  15.  
  16. # The better way is with for loop:
  17.  
  18. for i in range(1, n + 1):
  19.     if i % 2 != 0:
  20.         sum += i
  21.  
  22. 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