Advertisements
Advertisements
प्रश्न
Write a program to generate the sequence: –5, 10, –15, 20, –25….. up to n, where n is an integer input by the user.
थोडक्यात उत्तर
Advertisements
उत्तर
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 the event 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
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
