Tuesday, 6 November 2012

Calculate sum,Average and Number of Even and odds in given length Using For loop


# Get Length from user and calculate the number of even and odds , sum of even and odd and average of even and odds separately.

even_sum=0
even_counter=0
odd_sum=0
odd_counter=0
print 'This Program will calculate the averge of Even Numbers from which length you will give.'
length=input('Enter Number upto which you want to get Average: ')
print'\n'
for i in range(0,length):
    if i%2==0:
        even_sum=even_sum+i
        even_counter=even_counter+1
    if i%2==1:
        odd_sum=odd_sum+i
        odd_counter=odd_counter+1
print 'Number Of Odds is =',odd_counter
print 'Sum is =',odd_sum
odd_avg=odd_sum/odd_counter
print 'Averge of', odd_counter, 'Odd(s) is =',odd_avg
print '\n'
print 'Number Of Even is =',even_counter
print 'Sum is =',even_sum
even_avg=even_sum/even_counter
print 'Averge of', even_counter, 'Even(s) is =',even_avg

No comments:

Post a Comment