Programming Exercises · Q5
Q.Write a program to generate the sequence: –5, 10, –15, 20, –25….. upto n, where n is an integer input by the user.
Yanam CbseNCERTSubjective· 3mImportance★★★★★est
87% · 34/39 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Spot the pattern — the i-th term is 5 times i, with a minus sign at odd positions — then loop i from 1 to n printing 5*i negated when i is odd.
The idea: find the term's formula first. Writing the positions against the terms:
| Position i | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Term | -5 | 10 | -15 | 20 | -25 |
The magnitude is always 5 × i; the sign alternates, negative when i is odd, positive when i is even. Once the i-th term is expressible from i alone, a simple counted for loop generates the sequence. (We read "upto n" the standard way: n is the number of terms the user wants.)
Program
n = int(input("Enter the number of terms n: "))
for i in range(1, n + 1):
term = 5 * i
if i % 2 == 1: # odd position -> negative term
term = -term
print(term, end=" ")
print()
Sample run
Enter the number of terms n: 6
-5 10 -15 20 -25 30
Trace for n = 6
| i | 5 * i | i odd? | term printed |
|---|---|---|---|
| 1 | 5 | yes | -5 |
| 2 | 10 | no | 10 |
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.