Computer Science, asked by BrainlyProgrammer, 3 months ago

New Question!!!

Without using any Conditional contruct and Strings , print the following series:-
0,5,0,5...Nth term

#ShortestProgramPossible​

Answers

Answered by anindyaadhikari13
5

Required Answer:-

Question:

Without using conditional construct and strings, print the following series upto nth term.

0 5 0 5...

Solution:

Here comes the program. This might be the shortest program possible.

n,a,b=int(input("Enter limit - ")),0,5

for i in range(n):

print(a&5,end=" ")

a,b=b,a

If you don't know about bitwise 'and', then you can use this approach.

n=int(input("Enter the range - "))

a,b=0,5

for i in range(n):

print(a,end=" ")

a+=b

b*=-1

Algorithm:

  1. Accept the limit.
  2. Initialise a=0 and b=5
  3. Display the value of a.
  4. Add value of b to a.
  5. Negate the value of b.
  6. Continue this steps n times.

Refer to the attachment for output ☑

Attachments:
Answered by Oreki
4

\textsf{\large \textbf{Program}}

  \texttt{N = int(i \hspace{-1em} nput(`Enter N - '))}\\\texttt{print(*([0, 5] * N)[:N])}

\textsf{\large \textbf{Algorithm}}

   \textsf{\textemdash \: Getting the number of terms (N)}\\\textsf{\textemdash \: Coping the pattern (0, 5) \textit{N} times}\\\textsf{\textemdash \: Slicing the list up to the Nth term}\\\textsf{\textemdash \: Unpacking the list hence, displaying the series}

Attachments:
Similar questions