1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Finding Total

  1. Oct 10, 2009 #1
    1. The problem statement, all variables and given/known data

    In this problem, we are asked to write a program which displays the salary of a person for one month. Given that his salary for the first day is 1, second day 2 $ , third day 4$ , and contiue to double . And, then we have to display the total salary of the whole month.





    3. The attempt at a solution
    Her is the code I made

    public class Ass3 {

    public static void main (String args [])
    {

    int salary = 1;

    System.out.println(salary);

    for ( int d = 0 ; d < 29 ; d++)

    {
    salary *=2;
    System.out.println(salary);

    }


    }

    }


    I tired with making total = 0 and then total = total + salary,
    But it did Not work, even when putting inside the loop itself ?

    What do you think ???
     
  2. jcsd
  3. Oct 10, 2009 #2

    vk6kro

    User Avatar
    Science Advisor

    Go through the days of the month and each day work out the salary for that day and a running total up to the end of that day.

    At the end of the month you have a month's salary in the total.
     
  4. Oct 10, 2009 #3
    what do you mean by : a running total up to the end of that day ?

    What I thought about is initlizing a variable total = 0

    and then add the salaries each day.

    Right ??
     
  5. Oct 10, 2009 #4

    vk6kro

    User Avatar
    Science Advisor

    Yes, that is right.
    Each day double the previous day's salary and add it to the total.

    Before you start the loop, make the total = 0, the first day's salary = 1.
    Then FDS= FDS * 2
    Total = total + FDS

    Be ready for a very big number.
     
  6. Oct 11, 2009 #5
    alright, thanks alot!!

    I am done with one right now,

    Thanks alot once again =)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook