I'm going through the Java Tutorial on HackerRank using Java 8. The goal is to print out a multiplication table of 2 from 1 - 10.
Here is what I came up with
public static void main(String[] args) {
int x = 2;
int y = 0;
int z;
while (y < 10) {
z = x * y;
y++;
System.out.println(x + " x " + y + " = " + z);
}
Here is the output I get from the code above
2 x 1 = 0
2 x 2 = 2
2 x 3 = 4
2 x 4 = 6
2 x 5 = 8
2 x 6 = 10
2 x 7 = 12
2 x 8 = 14
2 x 9 = 16
2 x 10 = 18
I've also tried while <= 10 instead of while < 10 as shown in my code above and for that my result was:
2 x 1 = 0
2 x 2 = 2
2 x 3 = 4
2 x 4 = 6
2 x 5 = 8
2 x 6 = 10
2 x 7 = 12
2 x 8 = 14
2 x 9 = 16
2 x 10 = 18
2 x 11 = 20
Neither of this outputs is what I'm looking for. Logically I am confident my code makes sense and should work so I'm looking for someone to give me tips as to something I may have missed or maybe I've made a mistake and I'm not aware of it. I am not looking for the code to the right answer, but rather advice and/or pointers which will allow to come up with a working solution on my own.
Start your y value at 1
Don't increment your y value until after the print statement
public static void main(String[] args) {
int x = 2;
int y = 1; //starts at 1
int z;
while (y < 10) {
z = x * y;
System.out.println(x + " x " + y + " = " + z);
y++; // increment y after the print statement
}
}
Assign value of y = 1 and increment it after your system.out.println();
Related
I need an explanation of how the output prints 9(S), 7(S), 5(S) and 3(S).
10 > 3 is correct and goes to y 1 <= 2 which is correct so 2 x 10 - 2 = 18 but the output prints 9. I don't understand the logic here. Why does it print 9(s) instead of 18(s)?
public class Q2{
public static void main(String args[]) {
int x,y;
for(x= 10; x > 3; x = x - 2) {
for(y = 1; y <= 2 * x - 2; y = y + 2)
System.out.print("S");
System.out.print("\n");
}
}
}
Its correct Y <= 18 , but you are incrementing Y by 2, so it gets printed 9 times.
To understand, write down on a piece of paper what the values of your variables will be.
First, write down the values of x:
x: 10 8 6 4
Next, write down the calculated upper boundary value for y, i.e. the result of expression 2 * x - 2:
x : 10 8 6 4
yMax: 18 14 10 6
Last, write down the values of y:
x : 10 8 6 4
yMax: 18 14 10 6
y : 1 1 1 1
3 3 3 3
5 5 5 5
7 7 7
9 9 9
11 11
13 13
15
17
Finally, count the number of y values for each x value, i.e. the number of times S is printed:
x : 10 8 6 4
count: 9 7 5 3
Then realize that the code would have been much easier to understand if it had just been written like this:
for (int count = 9; count >= 3; count -= 2) {
for (int i = 0; i < count; i++) {
System.out.println("S");
}
}
Of course, that wouldn't have taught you what they were trying to teach you, which is:
Conclusion: If you don't understand what the code is doing, follow the logic step by step, and write down what it is doing.
I'm making a java program that shows the multiplication table that looks like this:
1
But I can only get the results from 1 to 5th column. How do I make the rest appear below ?
The program must only contain one for() nested loop.
This is my code so far:
import java.util.Scanner;
public class Table{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.print("Enter a number: ");
int inputi = s.nextInt();
for(int i = 1 ;i<=10;i++) {
for(int j=1;j<=inputi && j <= 5;j++) {
System.out.print(j + " x " + i + " = " +(i*j) + "\t");
}
System.out.println();
if(i >= 5)
for(int j = 6; j <= inputi && j <= 10; j++){
System.out.print(j + " x " + i + " = " +(i*j) + "\t");
}
}
System.out.println();
System.out.println();
}
}
Can anyone help ?
Thanks.
Edit: Sample input added.
Inputi = 7
Expected output:
Actual output:
I would suggest to
Create separate variable with number of required columns
Calculate how many rows will be printed (see iterations)
Print rows one by one
Please, see the code snippet below:
int inputi = 12;
int columns = 5;
int iterations = inputi / columns + (inputi % columns > 0 ? 1 : 0);
for (int iter = 0; iter < iterations; iter++) {
for (int i = 1; i <= 10; i++) {
for (int j = iter * columns + 1; j <= Math.min(inputi, (iter + 1) * columns); j++) {
System.out.print(j + " x " + i + " = " +(i * j) + "\t\t");
}
System.out.println();
}
System.out.println();
}
Note, in case inputi is huge, you may have to fill outputs with additional spaces, to avoid layout issues, when you have statements like 1 x 1 = 1 and 1 x 10000000 = 1000000
This algorithm with only 2 layers of for loops will give you the correct output.
import java.util.Scanner;
public class Table {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.print("Enter a number: ");
int inputi = s.nextInt();
for(int i = 0; i < Math.ceil(inputi / 5.0) * 10; i++) {
if ( i > 0 && i % 10 == 0 ) System.out.println();
int t = inputi - 5 * (i / 10);
for(int j = 0; j < (t > 5 ? 5 : t); j++) {
int a = 5 * (i / 10) + j + 1;
int b = i % 10 + 1;
System.out.print(a + " x " + b + " = " + (a * b) + "\t");
}
System.out.println();
}
}
}
Here is the sample output for input=11:
1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 4 x 1 = 4 5 x 1 = 5
1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 4 x 2 = 8 5 x 2 = 10
1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 4 x 3 = 12 5 x 3 = 15
1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16 5 x 4 = 20
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30
1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35
1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40
1 x 9 = 9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45
1 x 10 = 10 2 x 10 = 20 3 x 10 = 30 4 x 10 = 40 5 x 10 = 50
6 x 1 = 6 7 x 1 = 7 8 x 1 = 8 9 x 1 = 9 10 x 1 = 10
6 x 2 = 12 7 x 2 = 14 8 x 2 = 16 9 x 2 = 18 10 x 2 = 20
6 x 3 = 18 7 x 3 = 21 8 x 3 = 24 9 x 3 = 27 10 x 3 = 30
6 x 4 = 24 7 x 4 = 28 8 x 4 = 32 9 x 4 = 36 10 x 4 = 40
6 x 5 = 30 7 x 5 = 35 8 x 5 = 40 9 x 5 = 45 10 x 5 = 50
6 x 6 = 36 7 x 6 = 42 8 x 6 = 48 9 x 6 = 54 10 x 6 = 60
6 x 7 = 42 7 x 7 = 49 8 x 7 = 56 9 x 7 = 63 10 x 7 = 70
6 x 8 = 48 7 x 8 = 56 8 x 8 = 64 9 x 8 = 72 10 x 8 = 80
6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81 10 x 9 = 90
6 x 10 = 60 7 x 10 = 70 8 x 10 = 80 9 x 10 = 90 10 x 10 = 100
11 x 1 = 11
11 x 2 = 22
11 x 3 = 33
11 x 4 = 44
11 x 5 = 55
11 x 6 = 66
11 x 7 = 77
11 x 8 = 88
11 x 9 = 99
11 x 10 = 110
This will give you the desired output:
import java.util.Scanner;
public class Table{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.print("Enter a number: ");
int inputi = s.nextInt();
for(int i = 1 ;i<=10;i++) {
for(int j=1;j<=inputi && j <= 5;j++) {
System.out.print(j + " x " + i + " = " +(i*j) + "\t");
}
System.out.println();
}
System.out.println();
System.out.println();
for(int i = 1 ;i<=10;i++) {
for(int j=6;j<=inputi && j <= 10;j++) {
System.out.print(j + " x " + i + " = " +(i*j) + "\t");
}
System.out.println();
}
}
}
I am trying to solve a Sudoku program that I created.
This is the Objective Function
IloNumExpr numExpr = cplex.linearNumExpr();
cplex.addMaximize(numExpr);
What I am trying to do is to add to this either a constraint or new objective function that will allow for the four corner points in the 9x9 Sudoku matrix have special preference in the order of 5 > 7 > 4 > 6 > 8 > 2 > 3 > 9 > 1
Any ideas on what mathematical formulation is needed to complete this?
You'd impose the preference order by adding elements to your Objective Function.
Let's say you have variables of the type
choose[row][col][digit]
If choose[2][3][8] = 1 it means that the square (2,3) has the value 8.
The four corners are:
1,1; 1,9;
9,1; 9,9
Essentially, you need to add the following to your existing objective function.
9 x choose[1][1][5] + 8 x choose[1][1][7] + 7 x choose[1][1][4] + ... + 2 x choose[1][1][9] + 1 x choose[1][1][1]
9 x choose[1][9][5] + 8 x choose[1][9][7] + 7 x choose[1][9][4] + ... + 2 x choose[1][9][9] + 1 x choose[1][9][1]
9 x choose[9][1][5] + 8 x choose[9][1][7] + 7 x choose[9][1][4] + ... + 2 x choose[9][1][9] + 1 x choose[9][1][1]
9 x choose[9][9][5] + 8 x choose[9][9][7] + 7 x choose[9][9][4] + ... + 2 x choose[9][1][9] + 1 x choose[9][9][1]
In CPLEX
// Preference order: 5 > 7 > 4 > 6 > 8 > 2 > 3 > 9 > 1
int[] preferenceOrder;
preferenceOrder[1] = 9;
preferenceOrder[2] = 8;
preferenceOrder[8] = 2;
preferenceOrder[9] = 1;
cplex.addMaximize(cplex.scalProd(preferenceOrder, choose));
Why this works?
The CPLEX Solver tries to maximize the value of the obj function. All else being equal, it will first try to make choose[1][1][5] to be 1, and will next try to make it 7 and so on.
int x = 13;
while(x >= 4) {
if (x % 2 == 1) {
System.out.println(x);
}
x = x - 3;
}
I know the output of this, it is 13 and 7, would someone care to explain as how it came to be 13 and 7.
13 % 2 = 1 therefore, you print 13.
Now x = 10.
10 % 2 = 0, so you dont print out 10.
Now x = 7.
7 % 2 = 1, so you print 7.
Now x = 4.
4 % 2 = 0;
Now x = 1 and the loop stops.
The % operator is the modulo operator. This prints the remainder when dividing two numbers. For example 14/3 = 4 remainder 2, so 13 % 4 = 2.
First x is 13, is it >= then 4? Yes. Enter the while loop. Is 13%2==1. Yes. Print x (print 13). Then x = x-13, x becomes 10. Is 10 >=4? Yes. .... So on.
What don't you understand?
At the first iteration, x=13, 13%2=1 so it prints 13. The seconds iteration, x=10 (x=x-3) 10%2=0, nothing is printed. The third iteration x=7 (10-3), 7%2=1 so 7 is printed.
After that, x=4 so nothing is printed and x=1 quits the loop.
case 1:
---> x = 13;
while(true) // 13 >= 4
if(true) // 13%2 = 1 which is 1==1 is true
then print x
reduce x by 3 // now x ==10
case 2 :
---> x = 10;
while(true) // 10 > =4
if(false) // 10 % 2 = 0, 0 == 1 is false
skip
reduce x by 3// now x == 7
case 3:
---> x =7;
while(true) // 7 > = 4
if(true) //7 % 2 ,1==1 is true
print x;
reduce x by 3 // x == 4
case 4:
---> x =4;
while(true) // 4 > = 4
if(false) //4 % 2 ,0==1 is false
skip
reduce x by 3 // x == 4
case 5:
---> x =1;
while(false) // 7 > = 4
skip
operator summary :
**%** finds remainder // result is undefined if RHS operand is 0
**>=** greater than or equals
Say I have an index 12 (12th element) going from left to right, top to bottom.
I have an array[4][4].
What would be the fastest way to compute the index [3][2] given the 1D index 12? (1D index starts at 1).
Thanks
Don't know if this is fastest, but it's definitely simple:
Assuming array[x][y]
ix = floor(index / y)
iy = index % y
Example:
01
23
45
x = 3
y = 2
index = 3
ix = floor(3 / 2) = 1
iy = 3 % 2 = 1
index = 5
ix = floor(5 / 2) = 2
iy = 5 % 2 = 1
given a[x][y] is the array
use this formula
[index of 1d array]= (rnum * colsize) + (colnum + 1)
so for a[3][2] with colsize=4
= (3 * 4) + (2 + 1)
= 15