CameraIcon
CameraIcon
SearchIcon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

Write a program to generate the sequence: –5, 10,–15, 20, –25..... upto n, where n is an integer input by the user.

Open in App
Solution

The given sequence is: –5, 10, –15, 20, –20
The numbers in the sequence are multiples of 5 and each number at the odd place is negative, while the number at even place is positive. The series can be generated by checking each index for odd-even and if it is odd, it will be multiplied by '-1'.

Program:
num = int(input("Enter the number: "))
for a in range(1,num+1):
if(a%2 == 0):
print(a * 5, end=",")
else:
print(a * 5 * (-1),end=",")

The print(a*5*(-1)**a) statement can be also used inside 'for' loop as for even values of a, (-1)a will return '1' and for odd, it will return (-1).

Program:
num = int(input("Enter the number: "))
for a in range(1,num+1):
print(a*5*(-1)**a, end=",")


OUTPUT:
Enter the number: 6
-5,10,-15,20,-25,30



flag
Suggest Corrections
thumbs-up
9
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
MATHEMATICS
Watch in App
Join BYJU'S Learning Program
CrossIcon