Write a program to calculate the following series
Sum = 2 + 4 + 6 + 8 + 10 + 12 + 14 + 16.... + N, the user has to enter the value of N and N should be
an even number, if N was entered as an odd number you should print a message "Wrong Input!”,
otherwise find the sum of the series.
The output should be:
Please enter an odd number for n:
12
Sum = 2 + 4 + 6 + 8 + 10 + 12 = 42

Respuesta :

View the following image for how you can do this in R and C++



Ver imagen Аноним
Ver imagen Аноним
Ver imagen Аноним

Here's the program in Python:


def main():

   print("Please enter an even number for n:")

   number = int(input())

   if number % 2:

       print("Wrong input!")

   else:

       answer = sum(range(2, number + 1, 2))

       print(" + ".join( str(x) for x in range(2, number + 1, 2) ), "=", answer)

if __name__ == '__main__':

   main()


ACCESS MORE
EDU ACCESS