Why does 012 % 10 = 0 in Java? [duplicate] - java

This question already has answers here:
Int with leading zeroes - unexpected result
(4 answers)
Closed 7 years ago.
I am trying following program.
int var = 012;
int result = var % 10;
output:
result = 0
I am not able to understand why java is not able to consider 012 as 12.

In Java, integer literals that start with 0 are interpreted as octal numbers.
So, 012 is the number 1 x 8 + 2 = 10 (in decimal), not 12 (decimal).
012 % 10 == 10 % 10 == 0

Related

Why does Java compile this equation: 78 / 10 * 10 + 10 - 78 equal to 2? [duplicate]

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 2 years ago.
I've been trying to understand java operator precedence but I can't wrap my head around this equation someone has given to me. By adding parentheses myself I get (((78 / 10) * 10) + 10) - 78 but this shouldn't equate to 2. Can someone explain this to me? Thanks
78 / 10 = 7
7 * 10 = 70
70 + 10 = 80
80 - 78 = 2
remember an int /int will be an int
a float / int will be a float so you could do the following:
(float)78 / 10 * 10 + 10 - 78 or 78.0 / 10 * 10 + 10 - 78
It's because in integer math 78 / 10 is 7, not 7.8
Then:
7 * 10 = 70
+ 10 = 80
- 78 = 2

Precedence in expression including parentheses and int cast in java [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 3 years ago.
I have the following expression in my code
int n = ((int) Math.sqrt(4 * 4 + 5) - 1) / 2;
Can someone tell me the precedence in which the expression is evaluated?
Logically I would evaluate the expression in the following way:
4 * 4 + 5 = 16 + 5 = 21
Math.sqrt(21) ~ 4.58
4.58 - 1 = 3.58
(int) 3.58 = 3
3 / 2 = 1.5
However the code evaluates to 1.
You almost got it. The only difference (which doesn't matter for the result) is that the cast is evaluated
before the subtraction, and you're using integer division:
4 * 4 + 5 = 16 + 5 = 21
Math.sqrt(21) ~ 4.58
(int) 4.58 = 4 (cast first)
4 - 1 = 3
3 / 2 = 1 (integer division)
The order you suggest is correct.
The keypoint is the last operation: the result of an int divided by an int is an int as well.
To fix this, one of the two number should be a float (or a double):
float n = ((float)(int) (Math.sqrt(4 * 4 + 5) - 1)) / 2;
In this way you divide a float by an int, and the result will be a float.
Or better:
double n = (Math.sqrt(4 * 4 + 5) - 1) / 2;
Because the cast to int of Math.sqrt() isn't useful.
Please note that the first operation does exactly what you ask with the round of the Math.sqrt(), while the second one doesn't.

How to Print Random number in a Math Table?

I am implementing a math table using two integers (a and tableSize). I have built a random operation named R. I am going to calculate a random number between the row and column range and print the random number. For those instances where the row value is larger than the column value, the output is a dash ("-").
Here is my code,
int a = 4;
int tableSize = 10;
System.out.print(" R ");
for(int i = a; i <= tableSize;i++ )
{
System.out.format("%4d",i);
}
System.out.println();
for(int i = a ;i <= tableSize;i++)
{
System.out.format("%4d ",i);
for(int j=a;j <= tableSize;j++)
{
int randomNum = rand.nextInt (j) + i;
if(!(i > j))
{
System.out.format("%4d", randomNum);
} else
{
System.out.format("%4s", "-");
}
}
System.out.println();
}
The output I need is like this,
R 4 5 6 7 8 9 10
4 4 4 5 5 4 9 8
5 - 5 5 6 5 9 8
6 - - 6 6 7 9 6
7 - - - 7 7 7 7
8 - - - - 8 9 9
9 - - - - - 9 10
10 - - - - - - 10
But the problem is I didn't get output like that. output I receive is,
R 4 5 6 7 8 9 10
4 5 7 6 8 8 10 13
5 - 5 9 8 8 10 12
6 - - 9 8 11 10 11
7 - - - 8 14 9 16
8 - - - - 14 12 11
9 - - - - - 13 18
10 - - - - - - 19
And the row value is larger than the column value, Please anyone can help me? Thanks in advance.
The problem is that you are computing the cell value as the sum of a random number between 1 and the column number plus the row number. The logic I think you want is that a given cell in the matrix can be no larger than the max of the row or column number. If so, then you need to change this line:
int randomNum = rand.nextInt(j) + i;
To this:
int randomNum = rand.nextInt((Math.max(i, j) - a) + 1) + a;
Demo
Change your random number like this.
int randomNum = rand.nextInt((tableSize - a) +1)+a;
Output:
R 4 5 6 7 8 9 10
4 4 6 7 6 6 7 7
5 - 6 5 4 6 8 8
6 - - 7 8 7 7 8
7 - - - 10 7 5 5
8 - - - - 9 5 8
9 - - - - - 8 8
10 - - - - - - 4
You want a number that can be up to the higher limit (inclusive), but Random.nextInt(int) excludes the higher limit, so you need to add 1 to the argument. To get a random number from zero to 10 (inclusive), you can use rand.nextInt(10+1).
But you also have a lower bound. It's correct that you need to add the lower bound to the result as you did, but you need to subtract it from the range first.
You need to change this line:
int randomNum = rand.nextInt (j) + i;
To this:
int randomNum = rand.nextInt(j + 1 - i) + i;
But you need to move this line within your check that i <= j, or else your range becomes negative:
if (i <= j) {
int randomNum = rand.nextInt(j + 1 - i) + i;
System.out.format("%4d", randomNum);
} else {
System.out.format("%4s", "-");
}
Output:
R 4 5 6 7 8 9 10
4 4 5 5 7 4 5 8
5 - 5 6 6 5 5 5
6 - - 6 6 8 8 9
7 - - - 7 7 9 9
8 - - - - 8 9 9
9 - - - - - 9 9
10 - - - - - - 10
First, you should know how to get random between two integers, and then code the rest (check code comments)
Here is an implementation using ternary operator ?:, which is good to know of
PrintR.java:
import java.util.Random;
public class PrintR {
public static void main(String[] args) {
int a = 4;
int end = 10;
printRTable(a, end);
}
public static void printRTable(int init, int end) {
Random rand = new Random();
// first print the top header row
System.out.format(" R ");
for(int i = init; i<=end;i++ ) {
System.out.format("%4d",i);
}
System.out.println();
System.out.println("------------------------------------------");
for(int i = init ;i<=end;i++) {
// print left most column first
System.out.format("%4d |",i);
for(int j=init;j<=end;j++) {
//Ternary operator
//r.nextInt(High-Low + 1) + Low; gives you a random number in between Low (inclusive) and High (inclusive)
System.out.format("%4s", i > j ? "-" : (rand.nextInt(j-i+1)) + i);
}
System.out.println();
}
}
}
Example output:
R 4 5 6 7 8 9 10
------------------------------------------
4 | 4 4 5 7 5 5 4
5 | - 5 6 6 5 8 10
6 | - - 6 7 7 6 8
7 | - - - 7 7 7 10
8 | - - - - 8 8 10
9 | - - - - - 9 10
10 | - - - - - - 10

How to calculate Base64 encoded string length?

I've read following topic and found this formula there:
length = 4*(n/3)
I started to test it:
1 symbol:
Base64.getEncoder().encodeToString("1".getBytes()) =>MQ==(4 symbols)
2 symbols:
Base64.getEncoder().encodeToString("12".getBytes()) =>MTI= (4 symbols)
5 symbols:
Base64.getEncoder().encodeToString("12345".getBytes()) =>MTIzNDU=(8 symbols)
8 symbols:
Base64.getEncoder().encodeToString("12345678".getBytes()) =>MTIzNDU2Nzg=(12 symbols)
21symbols:
Base64.getEncoder().encodeToString("123456789012345678901".getBytes()) => MTIzNDU2Nzg5MDEyMzQ1Njc4OTAx (28 symbols)
Looks like this formula doesn't work.
Can you please explain mu results?
With 64 digits (26) one digit can represent 6 bits.
Hence 4 digits can represent exactly 4*6 bits = 3 bytes.
(Using ÷ for explicit integer division:)
With n bytes, 4*(n÷3) digits are needed plus for the remainder n%3 (0 < 3 bytes) there is a need for 0 upto 4 digits:
0 bytes (0 bits) 0 digits
1 byte (8 bits) 2 digits (12 bits) + "=="
2 bytes (16 bits) 3 digits (18 bits) + "="
Often there is a padding upto 4 digits/padding chars, using =.
This cannot be 0 as one then would add a byte 0x0.
Then the formula is 4 * Math.ceil(n / 3.0).
Without padding: Math.ceil(n * 8 / 6.0) = Math.ceil(n * 4 / 3.0) = (n * 4 + (3 - 1)) ÷ 3.
In java one should use int division only:
int base64Length(byte[] b) {
int n = b.length;
return (n * 4 + 2)/3;
}
Try length = 4*((n/3) + 1), where "/" is integer division.
Edit: Lexicore is correct, my formula fails when the remainder is zero.
int length = 4 * (n / 3);
if (n % 3 > 0) length++;

Java Loop to go Through Numbers and Adding 5

I am writing Java code that needs to print these numbers: "0 5 10 3 8 1 6 11 4 9 2 7" in that order. I am new to Java, and am not very good at Loops yet. I am finding the points of a 12 point star, starting at 0, and trying to find the points that need to be touched by a line to make the star..
How do I do a loop that starts at 0, and adds 5 to each number.. so 0 + 5 = 5, 5+5=10, 10+5=3 (this is where my problem is.. How do I make it go back from 11 to 0?
I know this might seem confusing... or it might be extremely easy.. but any help would be greatly appreciated.
Increment by 5 until you pass 60, display the result of modulo 12. Something like,
for (int i = 0; i < 60; i += 5) {
System.out.println(i % 12);
}
This is called modulus, and java has the modulo operator % which gives the remainder of integer division.
15 / 12 == 1
15 % 12 == 3
as
(15 / 12) * 12 + (15 % 12) == 15
See the wikipedia.
Of course in your case you could also do
n += 5;
if (n >= 12) {
n -= 12;
}
instead of using modulo:
n = (n + 5) % 12;
Just iterate from 0 to 12, multiplying your number by 5, and applying modulus 12:
for (int i = 0; i < 12; i++) {
System.out.println(i * 5 % 12);
}
I believe the operator you are looking for is the modulus operator. The modulus gives you the remainder from a division problem. In this case you are using 12 as the denominator.
0 + 5 % 12 = 5 (0, remainder 5)
5 + 5 % 12 = 10 (0, remainder 10)
10 + 5 % 12 = 3 (1, remainder 3)
15 + 5 % 12 = 8 (1, remainder 8)
20 + 5 % 12 = 1 (2, remainder 1)
try this :
for(int i=0; ; i = (i+5)%12){
System.out.println(i);
if(i==7)break;
}

Categories