Computer Science, asked by fahimkhan40226, 2 months ago

Write a program (WAP) that will print following series upto Nth terms.
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 …….
sample input:5
sample output: 1, 3, 5, 7, 9

Answers

Answered by AaronGrey
0

USING PYTHON

last= int(input("Enter number of terms:"))

n=0

pat=1

while n<last:

print( pat)

pat = pat + 2

This should print your desired output on screen.

Answered by mahinderjeetkaur878
0

Here's a Python program that will print the series up to the Nth term based on user input:

n = int(input("Enter the number of terms: "))

# initialize the first term of the series

num = 1

# loop through each term up to n

for i in range(n):

# print the current term

print(num, end=', ')

# add 2 to get the next odd number in the series

num += 2

The program works step by step:

  • The first line of the program asks the user to input the number of terms they want to print in the series.
  • This is done using the input() function, which prompts the user to enter a value and returns it as a string.
  • We then convert this string to an integer using the int() function and store it in the variable n.
  • We then initialize the first term of the series to 1 by assigning it to the variable num.
  • We use a for loop to iterate through each term up to the Nth term. The range(n) function creates a sequence of numbers from 0 up to (but not including) n.
  • Since we want to print n terms, this means we will loop n times.
  • Inside the loop, we print the current term using the print() function. We pass in the value of num as the argument to print(), which prints the value to the console.
  • We also include the end argument with a value of ', '. This tells Python to print a comma and a space after each value.
  • After printing the current term, we add 2 to num to get the next odd number in the series.
  • This is done using the += operator, which adds 2 to the current value of num and assigns the result back to num.
  • The loop continues to iterate until it has printed the desired number of terms, which is n.
  • Once the loop is finished, the program terminates.
  • So, when the user inputs a value of 5, for example, the program will print the first 5 terms of the series: 1, 3, 5, 7, and 9.

To know more :-

https://brainly.in/question/9963200?referrer=searchResults

https://brainly.in/question/17595678?referrer=searchResults

#SPJ3

Similar questions
Physics, 1 month ago