DEV Community

subash
subash

Posted on

While loop and factorial

Frog is fallen 50 feet borewell, each day that will go up 2 feet and return down 1.25 feet , question is how days needs to frag come out of the borewell, we have to create code for this senario.


feet = 50
up = 2
down = 1.25
day = 0
while feet>0:
    feet = feet - up + down
    day = day + 1
print(day);

Enter fullscreen mode Exit fullscreen mode

OUTPUT;
67

Here what was happened is we create variable and value for feet, up , down and day,
then we create while loop , while loop condition is when feet is greater than 0 then the loop will run,
what inside the loop have is feet = feet - up + down
frag jump 2 feet per day and again down 1.25 feet so we add that,
each time while running day one increase when the while condition false then print day will work because this print is present outside of the loop.

#Additive Identity
0 is additive identity
because all number are return same value while addition with anynumber that same number will return

Multiplicative Identity

1 is multiplicative identity
because all number are return same value while multiple with anynumber that same number will return

Factorial

Factorial means multiplying from 1 to n.

3!=1×2×3=6

3!=3×2×1=6

because multiple order is not change the result

First N Numbers Sum

Standard way:

1+2+3=6

It can also be:

3+2+1=6

Both are correct

Formula for first n number:

n * (n+1)/2

Top comments (0)