# Saving \$1 Each Day of the Week but Each Week the Amount Goes up by \$1 [Assignment]

Let’s say you are saving up to buy a new phone. For the first week, you save \$1 each day. For the second week, you save \$2 each day and so on each consecutive week you add \$1 more. Meaning that for week 3 you would be saving \$3 per day. For example, after 16 days you would have saved \$27 (\$7 from the first week, \$14 for the second, and \$6 for those two days of the third week). See the image below for a clearer explanation: I want to write a program that calculates the total amount of money that has been saved after N days. Where N is a number of days that was inputted by the user. Below is my code but I am unsure of how to optimize it. It works assuming the maximum number of days can be entered is 14 but of course I want to write the program so that it can take any number of days. During my research I came across the formula n*(n+1)/2 but that only works if the money was saved on a weekly basis (eg \$1 for the first week, \$2 for the second, etc). As explained before, in my problem the money is saved on a daily basis. My code below: (I’m new to Java so the syntax might not be perfect)

```import java.util.Scanner;

public class App {
public static void main(String[] args) throws Exception {

int days;
int savings = 0;

Scanner input = new Scanner(System.in);

System.out.print("Enter the number of days: ");
days = input.nextInt();

for(int i = 0; i < days; i++){
if(i < 7){
savings++;
}
if(7 <= i && i <=14){
savings++;
savings++;
}
}

System.out.println("Total Frank's savings (after " + days +" days): " + savings);
}

}

```

```public static void main(String[] args) {
int days = 16;
int weeks = days / 7; // '/' means get the divider
int savings = 0;
int ans = 0;
if (days % 7 == 0) {  // '%' means get the divider, when there's no reminder for day / 7
for (int i = 1; i <= weeks; i++) {
savings += 7 * i;
}
}
else { // when there is reminder for day / 7
for (int i = 1; i <= weeks; i++) {
int left = days % 7;
ans += 7 * i;
savings = ans + (weeks + 1) * left;
}
}

System.out.println("saving is " + savings);
}
```