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();
}
Related
I'm a newbie, but I'm willing to learn how to code.
I tried using this code:
int n = 50;
int counter = 0;
System.out.print("Even Numbers from 1 to "+n+" are: ");
for (int i = 1; i <= n; i++) {
counter++;
if (counter == 2) {
System.out.println(i + " ");
counter = 0;
%10== 0
to find all even numbers between 1 to 50 and make a new line at multiples of 10 just follow these steps -
Make one loop which will go 1 to 50
Check if the number is even by checking the remainder after diving it with 2, if YES print that number.
Check if the number is a multiple of 10 by checking the remainder after dividing it by 10, if YES make a new line
The code will look something like this -
int i = 1;
while(i<=50){
if(i%2 == 0) System.out.print(i + " ");
if(i%10 == 0) System.out.println();
i++;
}
Output -
2 4 6 8 10
12 14 16 18 20
22 24 26 28 30
32 34 36 38 40
42 44 46 48 50
It's up to you which looping method you want to use for me While loop looks cleaner.
I hope this solves all your queries.
PFB Snippet:
public class Main
{
public static void main(String[] args) {
for(int i=1;i<=50;i++){
if (i%2 == 0) //Check whether number is even
{
System.out.print(i+" ");
if (i%10 == 0) // Check if it is multiple of 10
{
System.out.print("\n");
}
}
}
}
}
Output:
2 4 6 8 10
12 14 16 18 20
22 24 26 28 30
32 34 36 38 40
42 44 46 48 50
"\n" is a Escape Sequence which means new line
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
I have a Matrix with different line size , i read it from a text file
private int set_data[][];
I have created another copy
private int set_number=set_data.length;
private int[][] set_cluster = new int[set_number][];
What i want to do is to fill set_cluster from the third row of each line
For example we have :
line one 1 3 30 49 48
line two 2 3 22 36 11 40
line three 3 5 51 44 47 15 38 40
and my goal is to store in set_cluster these numbers
line one 30 49 48
line two 22 36 11 40
line three 51 44 47 15 38 40
I have tried this code
private void fill_id_cluster() {
for (int i = 0; i < set_data.length; i++) {
set_cluster[i] = new int[set_data[i].length - 2];
for (int j = 0; j < set_data[i].length - 2; j++) {
set_cluster[i][j] = set_data[i][j + 2] ;
System.out.print(set_cluster[i][j] + " ");
}
}
}
Thanks for your help!
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 was wondering if someone could help me with a small problem I'm having.
I am trying to print out an array of 100 items, every row needs 10 items, after which it starts a new row. I have done this successfully, however, every row in every column displays the same number. For example. My output would be:
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
When it should be appearing as something along the lines of:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
Here is the following method I am using to print the table:
public static void printTable(int[] emirps) {
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 10; j++) {
// After 10 go to a new line
System.out.printf("%d\t", emirps[i]);
}
System.out.println("");
}
}
If anyone can help me pinpoint where I have goofed I'd greatly appreciate it.
You are printing the same emirps[i] each time through the inner loop. There are a couple of ways around this. Here's one:
public static void printTable(int[] emirps) {
for (int i = 0; i < 100; i += 10) {
for (int j = 0; j < 10; j++) {
// After 10 go to a new line
System.out.printf("%d\t", emirps[i + j]);
}
System.out.println();
}
}
Here's another (not very elegant):
public static void printTable(int[] emirps) {
for (int i = 0; i < 100; ++i) {
System.out.printf("%d\t", emirps[i]);
if (i % 10 == 9) {
System.out.println();
}
}
}
This looks like homework, so instead of giving you the answer, here's a hint:
For the first iteration of the first loop, i = 0. When this is the case, you increment j from 0 to 9. Note that i stays the same throughout. This is when you print your first line.
Once you're through with the second for loop, i increases by 1 to, and now i = 1. Again, you increment j from 0 to 9, and i stays at 1. This is when you print your second line.
This repeats 100 times, as i goes from 0 to 99.
There are 2 ways of approaching this:
Use one for loop, and figure out when to print an end-of-line character.
Use two for loops, each going up to 10. Figure out your index by simple multiplication and addition.
You should try and implement both methods. It'll help you understand loops better.
You are not using the j index anywhere in this code.
Here is how you should print:
System.out.printf("%d\t", emirps[i+j]);
Notice how in the print statement you are now using both the i and j indices.
Also, to get your row offset working correctly, you should be incrementing i by 10 at each step of the loop.
Alternatively, declare emirps as a 2-dimensional array. Then the signature of printTable() would be:
public static void printTable(int[][] emirps)
In that setup, here is how you would print:
System.out.printf("%d\t", emirps[i][j]);
So in the first setup, i+j together index into a 1-D array. In the second setup, i is the row and j is the column, indexing together into a 2-D array. It looks like you're doing a 1-D array but this other example is for your future reference.
Use the modulo operator. Every time your number can be divided by 10 without a remainder, you can make a new line because you have filled up the row. You obtain the remainder using the 'modulo operator' which is the percent (%) sign. It can be used like /, except that rather than the quotient being the result, the remainder of the division operation is the result.
public static void main(String[] args)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 100; i++)
{
if (i != 0 && i % 10 == 0)
{
builder.append('\n');
}
builder.append(i + " ");
}
System.out.println(builder.toString());
}
Prints out:
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 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99