Program to find total amount of money we have in bank in Python


Suppose you put 1Rs in a bank on first day say Monday. And every day from next day, Tuesday to Sunday, you put in 1Rs more than the day before. And on every subsequent Monday, you will put in 1Rs more than the previous Monday. If we have a number n, we have to find the total amount of money you will have in the bank at the end of the nth day.

So, if the input is like n = 17, then the output will be 75 because, put 1Rs on Monday, 2Rs on Tuesday and so on, so 7Rs on Sunday, then on next Monday put 2Rs, on second Tuesday put 3Rs, so on Sunday put 8Rs. Then on third Monday put 3Rs, Tuesday put 4Rs and Wednesday (last day) put 5Rs, so total sum is (1+2+3+4+5+6+7)+(2+3+4+5+6+7+8)+(3+4+5) = 75Rs

To solve this, we will follow these steps −

  • s := 28

  • res := 0

  • if n > 7, then

    • res := s

    • div := floor of n/7

    • for i in range 1 to div - 1, do

      • res := res + s+7*i

    • rem := n mod 7

    • for i in range 1 to rem, do

      • res := res + i+div

  • otherwise,

    • for i in range 1 to n, do

      • res := res + i

  • return res

Example (Python)

Let us see the following implementation to get better understanding −

 Live Demo

def solve(n):
   s = 28
   res = 0
   if n>7:
      res = s
      div = n//7
      for i in range(1,div):
         res += s+7*i
      rem = n % 7
      for i in range(1,rem+1):
         res += i+div
   else:
      for i in range(1,n+1):
         res+=i
   return res

n = 17
print(solve(n))

Input

17

Output

75

Updated on: 18-May-2021

425 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements