Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

How to make loops?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to make a program that tells me in how many days do i need to work to get a specific sallary.

Step 1)The program needs to ask how much salary I want.
Step 2)The program will give me as an answer how many days I need to work to get the salary.

For each day the salary doubles with 2.
Day 1 = 1 dollar
Day 2 = 2 dollar
day 3 = 4 dollar

.......I can only work for 30 days

I need to calculate how many days it takes to get the salary im asking for.

For 1000000 dollar then I should get 27 days.

What Problem I have with this question is that I have no idea how to make a loop for this. I dont know how to approach this question. Im a totally beginner in this subject. Please someone explain and show how to do this.

(I need use loops for this question)
 
Saloon Keeper
Posts: 6460
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you write out in English the steps you'd need to take to perform this task?

Can you write the code for prompting for, and getting, input parameters?
 
Marshal
Posts: 66525
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please explain more. On day 2 do you get $2 or do you keep the $1 from day 1 making $3?

Peach Blossom wrote:. . . For 1000000 dollar then I should get 27 days.  . . .

How did you get 27 days? I think 27 is the wrong answer.
Why do you think you need a loop for that calculation? Have you any idea what would happen if you get to day 31?
 
Madelene Rose
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I need to do
EBAC857D-F2BF-4D00-BE3B-5B59183DF35D.jpeg
[Thumbnail for EBAC857D-F2BF-4D00-BE3B-5B59183DF35D.jpeg]
 
Madelene Rose
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is what I have tried. I did it in days, but it need to be money first.I feel like my code is not what its supose to do.
 
Campbell Ritchie
Marshal
Posts: 66525
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will get 27 days if you use ¢/öre rather than dollars. But that would be 100,000,000öre.
Always start with for (int i ; i < something; i++) ... when writing a loop. You may need to change those figures in special cases, but you don't have such circumstances here.

No, that isn't what you want your program to do. You are right there. Please write down on paper how you would work out how many days it would take to reach a particular salary. You don't need a loop because you can calculate the days by other means.
 
Madelene Rose
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My calculations
839B6F33-FF20-4AB3-96FB-43A83FE4EAF3.jpeg
[Thumbnail for 839B6F33-FF20-4AB3-96FB-43A83FE4EAF3.jpeg]
 
Marshal
Posts: 14476
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I got:

So I'm not sure the answer of working 27 days to get a salary of 1,000,000 is correct.
 
Bartender
Posts: 3668
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the earnings after n days are (2^n - 1) * 0.01.
So, it is indeed 27 days to earn 1.000.000.

But we forget one important aspect. It is indicated that survival after 30 days is nil. So, lets say that the chance to live after n days = (30 - n) / 30. How many days would you be willing to work?    
 
Junilu Lacar
Marshal
Posts: 14476
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see now, you are adding up the salary from all the previous days as well. So, yes, to get at least 1,000,000 you will need to work for 27 days.
 
Piet Souris
Bartender
Posts: 3668
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I noticed. But your interpretation is valid, just as OP's (and Campbells and mine). But there is a third interpretation, and more aimed towards the very dangerous labour. That is that the salary per day doubles. So that would give the scheme:

1 day work: earnings 1 * 0.01
2 days work: 2 * 0.02
3 days wotk: 3 * 0.04

et cetera. Now, with this scheme, how many days to work to earn 1.000.000?
 
Junilu Lacar
Marshal
Posts: 14476
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:
1 day work: earnings 1 * 0.01
2 days work: 2 * 0.02
3 days wotk: 3 * 0.04

et cetera. Now, with this scheme, how many days to work to earn 1.000.000?


According to this, 24 days, assuming you worked only full days.
 
Madelene Rose
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I tried to do like this, but I dont know how to make the computer ask for specific number öre.
 
Campbell Ritchie
Marshal
Posts: 66525
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . So I'm not sure the answer of working 27 days to get a salary of 1,000,000 is correct.

No, if you calculate öre/¢ etc., you have to count up to 10⁸ (= 100,000,000). After 27 doublings, we reach 10²⁷öre = 134,217,728 = 1,342,177.28Kr. The first post said $ when it should have read ¢/öre. If you are calculating $/Kr, you pass 1000000 after 20 doublings.
The only explanation that will give 1000000Kr on day 27 is cumulation. The following figures look the same as some Junilu showed us yesterday:-The day's salary is 2⁽⁻¹⁾öre/¢. Because we are using binary arithmetic with the repeated doublings, the cumulative salary is 1öre/¢ less than the next day's salary or (2) − 1öre/¢. The () are redundant () to remind us of the operator precedences. We reach a cumulative salary of ≥ 1000000Kr on day 27. As those who have done more programming have realised, even if the worker survives beyond day 30, their arithmetic won't.

MR: As I told you yesterday, please go back and write down on paper how you would calculate number of days to reach/exceed 1000000Kr (=100000000öre). Don't look at your computer and especially don't look at this old code; you need to start anew. There is a way of calculating that without using a loop.
 
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!