I want to ask a question or a probable favor on how am I going to make my program coding "do-while loop" in creating a Triangular Multiplication. Is there a probable way on to create such thing without using any other statement?
public class Main {
static void ssbr(int n) {
int i = 1;
do{
System.out.printf("%4d", n * i);
i = i + 1;
} while(i <= 7);
System.out.println("");
}
public static void main(String[] args) {
int i = 1;
do{
ssbr(i);
i = i + 1;
} while (i <= 7);
}
}
Output it gave:
1 2 3 4 5 6 7
2 4 6 8 10 11 12
3 6 9 12 15 18 21
4 8 12 16 20 24 30
5 10 15 20 25 30 35
6 12 18 24 30 36 42
7 14 21 28 35 42 49
Output I wanted:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
You can do it with the following algorithm:
You have to do it 7 times and therefore you can use a loop that should run 7 times.
Each row starts with the row number, and run for row number * row number times with a step-value equal to the row number.
Given below is the implementation of this algorithm using for loop and I leave it to you to implement it using the do-while loop (as it seems to be your homework 😀)
public class Main {
public static void main(String[] args) {
int n = 7;
for (int row = 1; row <= n; row++) {
for (int col = row; col <= row * row; col += row) {
System.out.printf("%-4d", col);
}
System.out.println();
}
}
}
Output:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
Related
This question already has answers here:
Pascal's Triangle Format [duplicate]
(8 answers)
Closed 3 years ago.
I have to print Pascal's Triangle, so it should have number 1 on each side, and format of triangle should be even on each side (now it's longer on the right side). My code:
public static void main(String[] args) {
printPascalTriangle(10);
}
public static void printPascalTriangle(int size) {
for (int i = 0; i <= size; i++) {
System.out.println();
for (int j = 0; j <= (size - i); j++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print(" " + (i + j));
}
}
}
And the output is:
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10
6 7 8 9 10 11 12
7 8 9 10 11 12 13 14
8 9 10 11 12 13 14 15 16
9 10 11 12 13 14 15 16 17 18
10 11 12 13 14 15 16 17 18 19 20
Why it doesn't sum up? And why loop doesn't format spaces properly?
You should use printf method instead of print with proper arguments to format your output:
public static void main(String[] args) {
printPascalTriangle(10);
}
public static void printPascalTriangle(int rows) {
for (int i = 0; i < rows; i++) {
int number = 1;
System.out.printf("%" + (rows - i) * 2 + "s", "");
for (int j = 0; j <= i; j++) {
System.out.printf("%4d", number);
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
In the below code block:
public static void main(String[] args) {
int i;
int x = 0;
int count;
int sum = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter The Table Operator (+, -, *, /, %, or R)");
String operator = scan.nextLine();
System.out.println("Enter The Smallest Operand For the Table:");
int small = scan.nextInt();
System.out.println("Enter The Largest Operand For the Table");
int large = scan.nextInt();
for(i = 0; i < 12; i++) {
for(x = 0; x <= large; x ++)
System.out.printf("%4d", x + small);
System.out.printf("\n");
x++;
large++;
}
}
The code outputs:
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
but I want it to look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
2 3 4 5 6 7 8 9 10 11 12 13 14
3 4 5 6 7 8 9 10 11 12 13 14 15
4 5 6 7 8 9 10 11 12 13 14 15 16
5 6 7 8 9 10 11 12 13 14 15 16 17
6 7 8 9 10 11 12 13 14 15 16 17 18
7 8 9 10 11 12 13 14 15 16 17 18 19
8 9 10 11 12 13 14 15 16 17 18 19 20
9 10 11 12 13 14 15 16 17 18 19 20 21
10 11 12 13 14 15 16 17 18 19 20 21 22
11 12 13 14 15 16 17 18 19 20 21 22 23
12 13 14 15 16 17 18 19 20 21 22 23 24
It's basically an addition table. I'm trying to get the x value and the value of "large" to implement by one on each loop. I'm pretty sure I'm using the for loop incorrectly but I haven't been able to find out how.
for (int row = 1; row <= 12; row++) {
for (int col = row; col < row + 13; col++)
System.out.print(col != row ? " " + col : col);
System.out.println();
}
This pretty simple trianlge. Just inline all temporary variables like row and 'col' into the loops. Be simple.
int large = 10;
int small = 0;
int x,i;
for( i = 0; i < large; i++) {
for( x = 1; x <= large; x ++)
System.out.printf("%d ", x + small);
System.out.printf("\n");
x++;
small++;
}
Change "%4d" to "%d ". %4d makes every number 4 spaces long no matter what but you wanted the number and one space.
The last line in your outer for-loop should be small++ instead of large++ to offset the start value each time.
Your loop looks right. You are incrementing the wrong variables.
for(i = 0; i < large; i++) {
for(x = 0; x <= large; x ++){
System.out.printf("%4d", x + small);
}
System.out.printf("\n");
small++;
}
output:
1 2 3 4 5 6 7 8 9 10 11 12 13
2 3 4 5 6 7 8 9 10 11 12 13 14
3 4 5 6 7 8 9 10 11 12 13 14 15
4 5 6 7 8 9 10 11 12 13 14 15 16
5 6 7 8 9 10 11 12 13 14 15 16 17
6 7 8 9 10 11 12 13 14 15 16 17 18
7 8 9 10 11 12 13 14 15 16 17 18 19
8 9 10 11 12 13 14 15 16 17 18 19 20
9 10 11 12 13 14 15 16 17 18 19 20 21
10 11 12 13 14 15 16 17 18 19 20 21 22
11 12 13 14 15 16 17 18 19 20 21 22 23
12 13 14 15 16 17 18 19 20 21 22 23 24
int small = 1;
int large = 12;
for(int i = 0; i < 12; i++) {
for(int x = i; x <= i + large; x ++)
{
System.out.printf("%4d", x + small);
}
System.out.printf("\n");
}
checkout this simple change by changing the x ranges from i -> i + large which will avoid increment large ( because it is not need as in every iteration i also increase)
what you are trying to is in every iteration of i and x you are increment it at the end of the print operation that wont effect exactly as you want because for every iteration the value you print should iterate and print so change the x value to i, which will get incremented in every iteration and use it till i + large (where large is the number you obtain as maximum limit)
Note this can be achieved since small, and large are constant through out the loop
The code below is the answer I wrote for a question that asks to rotate an n x n 2D matrix by 90 degrees (clockwise), without creating a new 2D array. So for example,
Given input matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],
rotate the input matrix:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
I tried to do it row by row, but the problem I have to deal with is what to do if the pair of index if already altered. So if I try to assign index pair [1, 2] to [0, 1], but then [0,1] is already changed before. The solution I came up with is to use a HashMap, put the index pair in an array as key, and the original number as value.
Here is my code
public void rotate(int[][] matrix) {
int n = matrix.length;
HashMap<int[], Integer> map = new HashMap<>();
for(int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if(map.containsKey(new int[]{n-j,i})){
matrix[i][j] = map.get(new int[]{n-j, i});
}
else{
int temp = matrix[i][j];
matrix[i][j] = matrix[n-j][i];
map.put(new int[]{n-j,i}, temp);
}
}
}
}
However, the result shows that
if(map.containsKey(new int[]{n-j,i})){
matrix[i][j] = map.get(new int[]{n-j, i});
}
this line of code isn't searching for the array I put in before. I know that I am creating a new array every time, but how does it make containsKey not know if the array contains same numbers(the same array)? Can anyone help me understand why using an array here to mark the pair of index isn't working in a HashMap?
You don't need a Map to rotate a matrix. You only need one temp variable.
To rotate a 3x3:
1 2 3
4 5 6
7 8 9
temp = 1, copy corner values around, then save value to next corner:
1 2 3 7 2 3 7 2 3 7 2 3 7 2 1
4 5 6 → 4 5 6 → 4 5 6 → 4 5 6 → 4 5 6
7 8 9 7 8 9 9 8 9 9 8 3 9 8 3
repeat for border values, temp = 2:
7 2 1 7 4 1 7 4 1 7 4 1 7 4 1
4 5 6 → 4 5 6 → 8 5 6 → 8 5 6 → 8 5 2
9 8 3 9 8 3 9 8 3 9 6 3 9 6 3
And you're done, in-place rotation with only 1 value in temp storage, i.e. O(1) memory footprint.
Now I'll let you actually code that, for any size matrix.
UPDATE
For the fun of it, I decided to try writing it, so here it is, with test code. I'm not going to explain the logic though, that's for you to figure out yourself.
public static void main(String... args) {
for (int size : new int[] {2,3,4,5,10}) {
int[][] matrix = createMatrix(size);
printMatrix(matrix);
System.out.println();
rotateMatrix(matrix);
printMatrix(matrix);
printSeparatorLine(matrix);
}
}
private static int[][] createMatrix(int size) {
int[][] matrix = new int[size][size];
for (int y = 0, i = 0; y < size; y++)
for (int x = 0; x < size; x++)
matrix[y][x] = ++i;
return matrix;
}
private static void rotateMatrix(int[][] matrix) {
for (int y1 = 0; y1 < matrix.length / 2; y1++) {
for (int y2 = matrix.length - y1 - 1, x1 = y1; x1 < y2; x1++) {
int x2 = matrix.length - x1 - 1, temp = matrix[y1][x1];
matrix[y1][x1] = matrix[x2][y1];
matrix[x2][y1] = matrix[y2][x2];
matrix[y2][x2] = matrix[x1][y2];
matrix[x1][y2] = temp;
}
}
}
private static void printMatrix(int[][] matrix) {
int w = maxValueWidth(matrix);
for (int[] row : matrix) {
for (int i = 0; i < row.length; i++)
System.out.printf("%" + (w + (i == 0 ? 0 : 1)) + "d", row[i]);
System.out.println();
}
}
private static void printSeparatorLine(int[][] matrix) {
char[] buf = new char[(maxValueWidth(matrix) + 1) * matrix.length - 1];
Arrays.fill(buf, '-');
System.out.println(new String(buf));
}
private static int maxValueWidth(int[][] matrix) {
return Arrays.stream(matrix).flatMapToInt(Arrays::stream).map(i -> String.valueOf(i).length()).max().getAsInt();
}
Output
1 2
3 4
3 1
4 2
---
1 2 3
4 5 6
7 8 9
7 4 1
8 5 2
9 6 3
-----
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4
-----------
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
21 16 11 6 1
22 17 12 7 2
23 18 13 8 3
24 19 14 9 4
25 20 15 10 5
--------------
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
91 81 71 61 51 41 31 21 11 1
92 82 72 62 52 42 32 22 12 2
93 83 73 63 53 43 33 23 13 3
94 84 74 64 54 44 34 24 14 4
95 85 75 65 55 45 35 25 15 5
96 86 76 66 56 46 36 26 16 6
97 87 77 67 57 47 37 27 17 7
98 88 78 68 58 48 38 28 18 8
99 89 79 69 59 49 39 29 19 9
100 90 80 70 60 50 40 30 20 10
---------------------------------------
You said "this line of code isn't searching for the array I put in before". But you also acknowledge that you were creating a new object each time. That won't work:
Since arrays extend Object, but don't override hashCode() or equals(), you get the default implementations defined by Object. These require that the array is actually the exact same one as is being compared to - so it can't just be 'equivalent'. That is, another array of the same type, with the same elements in the same order, won't work.
Source: https://coderanch.com/t/399422/java/array-HashMap-Key
Instead, you should use a Pair object to store your coordinates. You can write your own implementation or use a pre-existing one, such as javafx.util.Pair
I need a for loop that its limit could be exceeded after one ends(one of the limits), I like to declare the limit 9 and start traversing an array to index of 8 then start from 9 and take 9 more steps and so on,until I reach the end of the array, my tries reached to this point but I wonder if it works correctly:
int [] i={9,18,27,36,45,54,63,72,81};
for(int x:i){
for(int j=0;j<x;j++)
{}
}
does the nested for loop going to change the x value after each complete cycle of the inner for loop or not?
then start from 9 and take 9 more steps
Your code doesn't behave as you want, since the inner loop always starts at 0.
There's no need to declare the i array. You can do it like this :
int start = 0;
for (int i = 9; i <= 81; i+=9) {
for (int j = start; j < i; j++) {
}
start = i;
}
Or as phflack suggested :
for (int i = 9; i <= 81; i+=9) {
for (int j = i - 9; j < i; j++) {
}
}
you can use this code:
int start = 0;
for (int i = 9; i <= 81; i+=9) {
for (int j = start; j < i; j++) {
System.out.print(j+" ");
}
System.out.println();
start = i;
//System.out.print(start+" ");
}
}
and you see:
0 1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16 17
18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35
36 37 38 39 40 41 42 43 44
45 46 47 48 49 50 51 52 53
54 55 56 57 58 59 60 61 62
63 64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79 80
Another training for you:
int start = 0;
for (int i = 1; i <= 10; i++) {
for (int j = 1; j < i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
and you can see:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
You can use two loop to print like matrix.
I'm new to Java. I am trying to create a multiplication table with 12 on each side of the table, so 12 going to the right and 12 going down. On each line, we will see the two values multiple. So my plan is to use 12 very similar for statements to print each of the twelve lines. One value will increment within a loop. The issue is, the first line isn't increment my y value. So it just prints out spaced out 1s.
If you have any suggestions on my latter part of the assignment, it'd be helpful. Once I get the first line to print 12 digits, I can make 11 other for statements. But I feel like there may be a simpler way to get the rest of the statements.
public class Sixthree
{
public static void main (String[] args)
{
int x = 1;
int y = 1;
System.out.print(" ");
for ( int c= x*y; y<= 12; y++)
{
System.out.print(c + " ");
}
}
}
I want the out put to look like this to start off with.:
1 2 3 4 5 6 7 8 9 10 11 12
But the current output looks like this:
1 1 1 1 1 1 1 1 1 1 1 1 1
But I want it to eventually like this: http://math.about.com/blgrid.htm
But without the blue lines.
You are getting all 1s because the loop initialization statement int c= x*y will be executed only once for a for loop. That is it is executed the first time when x=1 and y=1 and since, it is given as the loop initialisation statement and not in the loop body, it is never reevaluated. The for loop works as :
The loop initialisation statement is executed only once at the beginning of the loop. After each iteration the loop update expression is executed and the loop condition is reevaluated. for(loop_initialisation;loop_condition;loop_update) { ... }
So you should update c inside the loop, something like :
for ( int c= x*y; y<= 12; y++)
{
c = x*y;
System.out.print(c + " ");
}
for (int i = 1; i <= 12; i++) {
for (int j = 1; j <= 12; j++){
System.out.printf ("%3d ", j * i);
}
System.out.print ("\n");
}
The above code will give you output similar to what is shown below:
1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8 10 12 14 16 18 20 22 24
3 6 9 12 15 18 21 24 27 30 33 36
4 8 12 16 20 24 28 32 36 40 44 48
5 10 15 20 25 30 35 40 45 50 55 60
6 12 18 24 30 36 42 48 54 60 66 72
7 14 21 28 35 42 49 56 63 70 77 84
8 16 24 32 40 48 56 64 72 80 88 96
9 18 27 36 45 54 63 72 81 90 99 108
10 20 30 40 50 60 70 80 90 100 110 120
11 22 33 44 55 66 77 88 99 110 121 132
12 24 36 48 60 72 84 96 108 120 132 144
You should be using two nested for loops, one to iterate over the values of x, another to iterate over the values of y with each inner loop printing the value of x * y and each outer loop printing a new line character for formatting.
// Pseudo-code //
for(each x) {
for(each y) {
print(product);
}
print(newline);
}
Why it just prints out spaced out 1s ?
It is because you just assign c only once in for-loop. for ( int c= x*y; y<= 12; y++)
When value y is incvreasing, value c is not changing. The values is 1*1=1 (x=1, y=1).
As a result, you see it just prints out spaced out 1s.
You can use nested for loop to implement it.
public class Sixteen {
public static void main(String[] args) {
int x = 12;
int y = 12;
for (int i = 1; i <= x; i++) {
for (int j = 1; j <= y; j++) {
System.out.printf("%d ", i * j);
}
System.out.println();
}
}
}
for (int x = 1; x <= 12; x++)
{
for (int y = 1; y <= 12; y++)
{
int multiply = x * y;
System.out.print(multiply + "\t");
}
System.out.println();
}