NCERT Computer Science Class #11 – Chapter #6 Solutions – Q #10

Chapter #6 – Flow of Control

NCERT Computer Science Class #11 – Chapter #6 Solutions – Q 10

Class 11 – Chapter 6 Solutions – PROGRAMMING EXERCISES #5

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


Python Program:

def generate_sequence(n):
    sequence = []
    for i in range(1, n + 1):
        # Calculate the term based on the index
        term = (-1) ** i * (5 * i)
        sequence.append(term)
    return sequence

# Get user input
try:
    n = int(input("Enter an integer value for n: "))
    if n <= 0:
        print("Please enter a positive integer.")
    else:
        result = generate_sequence(n)
        print("Generated sequence:", result)
except ValueError:
    print("Invalid input. Please enter an integer.")

You can checkout the code from Github here – Programming Exercises – 5

Explanation of the Code:

1.	Function Definition: The generate_sequence function takes an integer n as 
        input and initializes an empty list sequence to store the terms.
2.	Loop: It iterates from 1 to n, calculating each term using the formula 
        (-1) ** i * (5 * i). This formula alternates the sign and multiplies by 5.
3.	User Input: The program prompts the user to enter an integer value for n. 
        It checks if the input is valid and positive.
4.	Output: If valid, it calls the function and prints the generated sequence.

Example Output:

Enter an integer value for n: 10
Generated sequence: [-5, 10, -15, 20, -25, 30, -35, 40, -45, 50]

NCERT Computer Science Class 11 Chapter 6 Solutions – Q 10

NCERT Computer Science Class 11 Chapter 6 Solutions – Q 10

Posts created 29

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top