59.4k views
5 votes
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

User Berkelem
by
5.4k points

2 Answers

4 votes

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()


User Haroun SMIDA
by
6.0k points
3 votes

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



Write a program to calculate the following series Sum = 2 + 4 + 6 + 8 + 10 + 12 + 14 + 16.... + N-example-1
Write a program to calculate the following series Sum = 2 + 4 + 6 + 8 + 10 + 12 + 14 + 16.... + N-example-2
Write a program to calculate the following series Sum = 2 + 4 + 6 + 8 + 10 + 12 + 14 + 16.... + N-example-3
User Igoris
by
6.0k points