Computer Science, asked by george0096, 3 months ago

Rewrite the following code of programs using the for loop.​

Attachments:

Answers

Answered by ItzAditt007
5

Answer:-

Using for loop it can be written as:-

a = int(input("Enter a number"))

for i in range (a, -1, -2):

print(a*i)

Explanation:-

Explanation of the given while loop:-

Let us first understand the code written in the while loop.

Lets say user gives input 4.

So the program will run like that:-

↦ a = 4

↦ i = a = 4.

It will check if i > or = 0 or not, so yes i is > or = 0.

The program will print,

↦ 4×4 = 16.

↦ then i will be equal to 4 - 2 = 2.

So again it will check the condition and yes the condition is true.

↦ 4 × 2 = 8.

↦ Then i will be equal to 2 - 2 = 0.

↦ 0 × 4 = 0.

↦ Then i will be equal to 0-2 = 2.

So this time condition is false and hence the program will come out of the loop.

Final Result:-

The output of the program if the input of user would 4 will be:-

16

8

0

Explanation Of the for loop:-

Syntax:-

For 'variable' in range (Start value, stop value, step value):

statements.

So we have done the same thing,

for i in range (a, -1, -2):

print (i*a)

↦ a = 4.

↦ Values of i will be,

  • 4
  • 4 - 2 =2.
  • 2-2 =0.

The value of i will not go less than 0 as the stop value is -1, and -1+1 = 0.

The program will print:-

↦ 4 × 4 = 16.

↦ 4 × 2 = 8.

↦ 4 × 0 = 0.

Final output of the program:-

16

8

0

Answered by Nylucy
7

 \mathcal { a=int(input("Enter \: a \: number")) \: }

 \mathcal {  for \: i \: in \: range(a,-1,-2): \: }

 \mathcal { print(a*i) \: }

i am also in 11th..bio with IP

 \mathbb { Nice \: to \: meet \: uh \: }

Similar questions