Software & Finance





Python - Sum of ODD Numbers





 

I have given here python program to find the sum of ODD numbers.

This code is tested with Python version 2.6

 

        print "Program to find the sum of ODD numbers in the given range"
        
        beg = 0
        end = 0
        sum = 0
        n = 0
        
        print "Enter Starting Number:", 
        beg = input()
        print "Enter Ending Number:", 
        end = input()
        
        n = beg
        if ( (n  % 2) == 0):
           n += 1
        
        print "Adding Numbers : ", 
        while(n <= end):
           print n, 
           sum += n
           n += 2
        
        print "\nThe result is: ", sum
        
        
		#sample output
		#Program to find the sum of ODD numbers in the given range
		#Enter Starting Number: 10
		#Enter Ending Number: 50
		#Adding Numbers :  11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
		#The result is:  600