Java formula to compute discount dynamically [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 days ago.
Improve this question
I need to implement an operation in my java program.
So I have the item, the price of it is 6,00€, but if the user get three of these items I can get a discount of 3,00€. So
int quantity = 3;
double discount = 3.0;
int quantityItem = 3;
double priceItem = 6.0;
double totalPrice = priceItem * quantityItem;
if(quantityItem == quantity){
totalePrice = totalePrice - discount;
}
The previous code is ok. But if I get 5 items the correct totalePrice should be:
priceItem (6€) * quantity (5) = 30€ - 3.0€ (discount) = 27€.
If I get 6 items the correct totalPrice should be:
priceItem (6€) * quantity (6) = 36€ - 6.0€ (discount) = 30€.
How can I write the code to make this dynamically ?

To identify the number of "groups of 3" of something bought, you need to divide the number bought by 3. Specifically, you want to use integer division, which basically means "divide by 3, then ignore the decimal part". So
1 / 3 = 0
2 / 3 = 0
3 / 3 = 1
4 / 3 = 1
5 / 3 = 1
6 / 3 = 2
...
In Java, division between two integers performs integer division by default, so you can calculate the number of "groups of 3" like so.
int discountQuantity = quantity / quantityItem;
Then you apply the discount that many times.
double totalPrice = priceItem * quantityItem - discountQuantity * discount;

Related

Can someone please explain this recursion question the problem is solved? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
Given base and n that are both 1 or more, compute recursively (no
loops) the value of base to the n power, so powerN(3, 2) is 9 (3
squared).
The answer is
public int powerN(int base, int n) {
if(n == 1)
return base;
return base * powerN(base, n - 1);
}
I am confused because when I look at this because is this not saying multiply the base against the returned number of powerN()?
powerN(3,3);
= 3
= 3*powerN(base, n-1) = 6
= 3*powerN(base, n-1) = 9
Which would multiply 9*6*3?
I do not see why we would have to multiply the base by the function?
Should the method not just return the answer as the base never changes and once n==1 the base case executes
Let's calculate powerN(3,3) as you did.
powerN(3,3) = 3 * powerN(3,2)
3 * powerN(3,2) = 3 * 3 * powerN(3,1)
3 * 3 * powerN(3,1) = 3 * 3 * 3 = 27
So it was just wrong the way you calculated.
Let's say we have powerN(2,3)
powerN(2,3):
call#1> return 2*powerN(2,3-1)
call#2> return 2*powerN(2,2-1)
call#3> (n==1)return 2*1
So,
call#3 returns 2 to call#2 [call#2 is now returning 2*2=4]
call#2 returns 4 to call#1 [call#1 is now returning 2*4=8]
That's it.

What's the safe way to calculate 0.01 of an Integer in Java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
For instance, I have integers, i.e. 450. I want to get 1/100 of the number N. In this case it should be 4.5, which will rounded to 5.
int i = 450;
int round = Math.round(i/450)?
When the i varies, is this safe always?
i/450 will do an integer division before the result gets passed into Math.round and you won't get what you expected. Even then you got ~1/450 of the value, not 0.01 of it. You need
(int)Math.round(i/100.0);
However you can do rounding with just integer math like either of these
int round = (i + 50)/100; // i + 99 for ceiling
int round = (i - 1)/100 + 1;
int round = i/100 + (i % 100 < 50 ? 0 : 1);
For more information read Rounding integer division (instead of truncating)
See also
How to round up integer division and have int result in Java?
How to Round Up The Result Of Integer Division
Fast ceiling of an integer division in C / C++
These are about ceiling function but you can get the idea
For it to work no matter numerator or denominator, then
BigDecimal.valueOf(450).divide(BigDecimal.valueOf(100),RoundingMode.HALF_UP);
Another way could be like
BigDecimal.valueOf(450,2).setScale(0, RoundingMode.HALF_UP);
You can convert back to int using .intValue()
That would only make sense of course when you are dealing with exact decimal math most of the time until the very end, like would be the case for financial applications.
I "think" this might be what you are looking for:
float i = 450;
int round = Math.round(i/100);
System.out.println(round);//prints 5
Division with int always rounds down so when i is an int, the expression i/100 returns 4 when i is between 400 and 499.
Alternately, you could cast to float:
int i = 450;
int round = Math.round((float)i/100);
System.out.println(round);//prints 5

Recursive Equations with Time Complexity [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am really struggling with this recursive question. Can anyone help me solve the recurrence T(n) = 5T (n/5)+5 with the base condition T(1) = 0 via closed-form formula? It is given that n = 5^m with the integer m = log5 n.
It will be sufficient to compute T(5n) for n >= 0. For all other values of x, T(x) will equal T(y) where y is the largest power of 5 smaller than x, since the calculations are the same. (I'm assuming that when you write n/5 you mean integer division, i.e. floor(n/5).)
Then:
T(50) = 0
T(51) = 5 * 0 + 5 = 5
T(52) = 5 * 5 + 5 = 52 + 51
T(53) = 5 * (5 * 5 + 5) + 5 = 53 + 52 + 51
... which leads to:
T(5n) = 5n + 5n-1 + ... + 52 + 51
which, using a high-school algebra formula (sum of a geometric series), is
T(5n) = (5n+1 - 5) / 4
If you're thinking about time complexity, notice that T(x) will always be less than or equal to 5x / 4. And since we don't worry about constant factors when expressing things in O-notation, this essentially means T(x) = O(x).
A non constructive way to solve this: looking a bit at the formula one guesses that T(5m) = (5m+1-5)/4. This can be shown by induction:
it is correct for m=0: T(1) = 0
assuming it is correct for m we show it for m+1: T(5m+1) = T(5*5m) = 5T(5m)+5 = 5*((5m+1-5)/4)+5 = (5m+2-25)/4+5 = (5m+2-5)/4.
Therefore it is correct for all m.

Java divider is giving Zero value [duplicate]

This question already has answers here:
Why double width = 50/110000; the output is 0.000000000000000?
(3 answers)
Closed 9 years ago.
I know this is very stupid question but need to ask,
I have values
int i = 12;
int j = 11;
float k = (i*j)/100;
result is giving 0.0 but here i want more that 2 digits decimal points , How can i achieve it,
I am getting wrong data, it is showing 0 which is wrong
Because all the calculation on the right hand side are of in integer, that is why the result 0.
At least one of the operand should be a floating point number like:
float k= (i * j) / 100.0;
In primary school I learnt integer division. We used to calculate things like 13 divides by 5 is 2 remainder 3. The maths you learnt at school still applies in the computing world. 11 * 12 is 132 and 132 / 100 is 1 and 132 % 100 is 32 (the remainder)
I wouldn't use float as double has half a billion times the accuracy.
double k = (i * j) / 100.0;

Java for every 6 remove 1 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm making a system.
What I want is, for every 6 items you have to buy 5 (so when the price is 5 each item, 6 items is not 30 but 25, same with 12, 18, 24 etc...)
How would I do that?
I thought it would be something like this:
if (amount % 6 == 0) {
}</code>
But that would get it one time if I'm correct.
The modulus operator won't work in this situation.
So for an efficient solution.
int numberOfItems = 17; //however many there are
int discount = numberOfItems / 6;
double priceToPay = price * (numOfItems - discount);
By having the discount as an int you won't get any rounding or decimal part after the division.
Using modulus will only give you the discount if you have 6, 12, etc. items. What about if you have 7 items? You won't get any discount (it is not divisible by 6)! So, it would be something like this:
int numOfItems = 6; //this will be different every time
//copy numOfItems because it will be modified
int temp = numOfItems;
double price = 5;
int discount = 0;
//While there are at least 6 items
while (temp >= 6) {
discount++; //discount one item
temp -= 6; //take away 6
}
//you have to pay for the number of items, but not the discounted items
double amountToPay = price * (numOfItems - discount);
This way, every time you take away 6, you don't have to pay for 1 item.

Categories