Adding zero to a single digit number, Is it possible? - java

public class MultiplicationTable {
public static void main (String[]a){
int[] x;
x = new int[10];
int i;
int n=0;
for (i=0;i<x.length;i++){
n++;
x[i]=n;
System.out.print(x[i] + " ");
}
System.out.println();
for (i=0;i<x.length;i++)
System.out.print(x[0] * x[i] + " ");
System.out.println();
for (i=0;i<x.length;i++)
System.out.print(x[1] * x[i] + " ");
System.out.println();
for (i=0;i<x.length;i++)
System.out.print(x[2] * x[i] + " ");
System.out.println();
for (i=0;i<x.length;i++)
System.out.print(x[3] * x[i] + " ");
System.out.println();
for (i=0;i<x.length;i++)
System.out.print(x[4] * x[i] + " ");
System.out.println();
for (i=0;i<x.length;i++)
System.out.print(x[5] * x[i] + " ");
System.out.println();
for (i=0;i<x.length;i++)
System.out.print(x[6] * x[i] + " ");
System.out.println();
for (i=0;i<x.length;i++)
System.out.print(x[7] * x[i] + " ");
System.out.println();
for (i=0;i<x.length;i++)
System.out.print(x[8] * x[i] + " ");
System.out.println();
for (i=0;i<x.length;i++)
System.out.print(x[9] * x[i] + " ");
}
}
This is a program that will display a Multiplication Table like this:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
===============================================================
It is running and correct but I want it to look like this one:
01 02 03 04 05 06 07 08 09 10
01 02 03 04 05 06 07 08 09 10
02 04 06 08 10 12 14 16 18 20
03 06 09 12 15 18 21 24 27 30
04 08 12 16 20 24 28 32 36 40
05 10 15 20 25 30 35 40 45 50
06 12 18 24 30 36 42 48 54 60
07 14 21 28 35 42 49 56 63 70
08 16 24 32 40 48 56 64 72 80
09 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
===============================================
Is there any way?

Yes you can! You can use String.format to add zero padding to your output.
Example:
String.format("%05d", 2) would produce 00002.
Some improvement on the current code:
I'm not sure why you intend to store the numbers inside an array (for practice purpose maybe), but that is not necessary as it goes from 1 to 10 anyway. Though if you want to do that, you don't need both i and n.
for (i=0; i<x.length; i++){
x[i] = i+1;
System.out.print(x[i] + " ");
}
Secondly, I'm sure you realize that you have a lot of duplicate code, and it's quite sequential. You can do that using 2 nested for loops, instead of having 10 single loops:
for (int row = 1; row <= 10; row++) {
for (int col = 1; col <= 10; col++)
System.out.print(String.format("%03d", row * col));
System.out.println();
}

Yes:
String.format("%01d", x[i]*x[j]); is what you want.
If you're familiar with printf in C then this will be familiar. If not, read the java reference on String.format format strings.
Also, rather than 10 System.out.println statements, you can use a doubly nested loop with two counters. One to count which row you're in j and one for each column i.

Related

How do i add if else with break and continue in nested for loop, the outcome should be that there are spaces between rows

public class Main {
public static void main(String[] args) {
for (int i = 1;i<14;i++){
for (int j=1;j<=i;j++){
System.out.print(6 *j+" ");
}
System.out.println();
}
}
This is the code that i need to add break and continue on.
6
6 12
6 12 18
6 12 18 24
6 12 18 24 30
6 12 18 24 30 36
6 12 18 24 30 36 42
6 12 18 24 30 36 42 48
6 12 18 24 30 36 42 48 54
6 12 18 24 30 36 42 48 54 60
6 12 18 24 30 36 42 48 54 60 66
6 12 18 24 30 36 42 48 54 60 66 72
6 12 18 24 30 36 42 48 54 60 66 72 78
The outcome should be like this, Im just starting java so simplifiying helps and im still learning
You can simply achieve this by adding a new line to your print statement as follow:
public static void main(String[] args) {
for (int i = 1; i < 14; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(6 * j + " ");
}
System.out.println(System.lineSeparator());
}
}
I've used the System.lineSeparator() to ensure that we will break the line independently on the operation system we're running - so for Linux for example it would be \n while on windows it would be \r\n
Alternatively, you can simply call the System.out.println() twice, and it will do the same...
You can see that running this code will result in the following output as expected:
6
6 12
6 12 18
6 12 18 24
6 12 18 24 30
6 12 18 24 30 36
6 12 18 24 30 36 42
6 12 18 24 30 36 42 48
6 12 18 24 30 36 42 48 54
6 12 18 24 30 36 42 48 54 60
6 12 18 24 30 36 42 48 54 60 66
6 12 18 24 30 36 42 48 54 60 66 72
6 12 18 24 30 36 42 48 54 60 66 72 78
If it's important to you to avoid the last empty line you can add an if-statement to the outer loop and check the index

How do you format and read randomly generated numbers in the console?

I've created a loop and random number generator that generates 100 numbers within the range of 1-100. I need to format these numbers so that it is 10 per line. I tried using printf and had a hard time. In addition, I have to find the average of all these numbers. The issue is I am unsure of how to do this because all the numbers are under the int variable 'randoms'. I can't add a single variable together and divide by 100.
public static void main(String[] args) {
Random rand = new Random();
int n = 100;
for (int i=1; i<=n; i++) {
int randoms = rand.nextInt(101);
}
}
You may print each number without a new line, and with spaces before to pad at 4-length string, and each 10 values, print a new line. For the average, use math : sum/count
Random rand = new Random();
int n = 100;
int total = 0;
for (int i = 1; i <= n; i++) {
int randoms = rand.nextInt(101);
total += randoms;
System.out.format("%4d", randoms);
if (i % 10 == 0) {
System.out.println();
}
}
System.out.println("AVG " + total / (double) n);
49 55 89 26 88 58 80 98 62 8
34 65 9 3 28 71 30 11 50 50
18 90 61 62 18 93 83 83 57 14
9 54 49 6 24 28 60 8 86 83
60 6 17 67 49 89 66 13 65 50
70 24 3 90 89 4 47 49 48 7
16 38 79 59 51 9 22 81 8 84
52 30 64 97 42 100 30 26 66 44
22 46 16 100 73 100 56 63 8 48
50 88 55 93 6 82 65 46 44 7
AVG 49.29

Printf for a Magic Square

so this is the code I have below:
public void printSquare() {
for (int row = 0; row < square.length; row++) {
for (int col = 0; col < square.length; col++) {
System.out.printf(square[row][col] + "%-3c", ' ');
}
System.out.println();
}
}
I'm trying to print them to look like this:
But my output is:
***** Square 2 *****
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
I've been messing around with printf for awhile now and I can't seem to figure out how to print it neatly. I'm barely hitting Java for my second semester in school, so I'm not that adept at coding yet. Any advice would help.
And if my coding is unorthodox or looks bad, please call me out on it so I can fix it.
Thank you.
You can try adding a tab space instead of manually adding space. Tab space will take care of issue of digit with space
public static void printSquare(int[][] square) {
for (int row = 0; row < square.length; row++) {
for (int col = 0; col < square.length; col++) {
System.out.print(square[row][col]+"\t");
}
System.out.println();
}
}
OUTPUT
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
To print a number with exactly four characters (padded by spaces) use this format:
System.out.printf("%4d", square[row][col]);

Align multiplication tables in nice columns [duplicate]

This question already has answers here:
Java output formatting for Strings
(6 answers)
Closed 5 years ago.
I am trying to print out the multiplication tables up to 10. I have the logic down, but I am not sure how to make it all aligned with nice columns.
Here is what I printing at the moment:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
I would like this:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Here is my current code:
public static void main(String[] args) {
multiplicationTables(10);
}
public static void multiplicationTables(int max) {
for(int x = 1; x <= max; x++) {
for (int y = 1; y <= max; y++) {
System.out.print(x*y + " ");
}
System.out.println();
}
}
Thanks for helping me.
You can use format to pad left each number with spaces:
public static void main(String[] args) {
multiplicationTables(10);
}
public static void multiplicationTables(int max) {
for(int x = 1; x <= max; x++) {
for(int y = 1; y <= max; y++) {
System.out.print(String.format("%4d", x * y));
}
System.out.println();
}
}
Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

How to print numbers in right-aligned manner? [duplicate]

This question already has answers here:
Align printf output in Java
(5 answers)
Closed 8 years ago.
So I am trying once of the codeeval's easy problems for multiplication tables
One of the requirement is
(The numbers are right-aligned and strip out leading/trailing spaces
on each line)
I am not sure about how to do that, my current code looks like
private static void printTable(final int numberOfTables, final int numberOfTimes) {
for (int i = 1; i <= NUMBER_OF_TABLES; i++) {
final StringBuilder sb = new StringBuilder();
for (int j = 1; j <= NUMBER_OF_TIMES; j++) {
sb.append(i * j).append(" ");
}
System.out.println(sb.substring(0, sb.length() - 4));
}
}
and what I get back is
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
How do I right-align numbers?
As you said in your question title, you use printf(). Something like,
for (int i = 1; i <= NUMBER_OF_TABLES; i++) {
for (int j = 1; j <= NUMBER_OF_TIMES; j++) {
System.out.printf("%6d", i * j);
}
System.out.println();
}
Where 6 is the width and the Javadoc for Formatter describes width as
Width
The width is the minimum number of characters to be written to the output. For the line separator conversion, width is not applicable; if it is provided, an exception will be thrown.
And
'd' ... Formats the argument as a decimal integer.
To print a number as right aligned you could use
System.out.printf("%nd", x);
where n is minimum width and x is the integer to be print.
Now for your questions answer you could use below code
for(int i=1; i<= NUMBER_OF_TABLES; i++)
{
int j=1;
System.out.print(i*j++);
for(; j<= NUMBER_OF_TIMES; j++)
{
System.out.printf("%4d",j*i);
}
System.out.println("");
}

Categories