I am trying to make a table from 1 - 5, which displays there power up to 6 values.
so for example, the 2 column would go from, 1,2,4,8,16,32,64 and would stop there.
I am having trouble getting proper table format. Since the numbers don't align where they should be.
for example:
the problem I am facing right now is this
1 2 3 4 5
1 1 1 1 1 1 1 2 4 8 16 and so and so on
any well would be appreciated, my code is down below.
int powNumb=5;
int powValue=6;
for (int i = 1; i <= powValue; i++) {
System.out.printf("%10d",i);
}
System.out.println();
for (int i = 1; i <= powNumb; i++) {
for (int j = 0; j <=powValue; j++) {
System.out.printf("%10.0f",Math.pow(i, j));
}
}
This should help you
for (int i = 1; i <= powNumb; i++) {
System.out.printf("%10d", i); //Print the number (1st col)
for (int j = 0; j <= powValue; j++) { //This loop prints the powers of the curent number 'i'
System.out.printf("%10.0f", Math.pow(i, j));
}
System.out.println(); //To end the current row
}
This prints
num num^0 num^1 num^2 ... num^powValue
where num is from 1 to powNumb
Output
1 1 1 1 1 1 1 1
2 1 2 4 8 16 32 64
3 1 3 9 27 81 243 729
4 1 4 16 64 256 1024 4096
5 1 5 25 125 625 3125 15625
You mean the same base for every element, so there is no need for inner loop:
for (int i = 1; i <= powNumb; i++) {
System.out.printf("%10.0f", Math.pow(powValue, i));
}
This way the base of power is always powValue.
First, you need a println statement somewhere in your inner for loop to separate the rows.
Second, you need to switch the i and j in your call to Math.pow. Because with how it's currently set up, each row is value i = row number to powers 0 through 6. For example, the first row would be 1^0 1^1 1^2 1^3 1^4 1^5 1^6. Then, the second row would be 2^0 2^1 2^2 2^3 2^4 2^5 2^6 However, you want the first row to be 1^0 2^0 3^0 4^0 5^0, second row 1^1 2^1 3^1 4^1 5^1, etc. So your code should be changed to something like this,
int powNumb=5;
int powValue=6;
for (int i = 1; i <= powNumb; i++) {
System.out.printf("%10d",i);
}
for (int i = 0; i <= powValue; i++) {
System.out.println();
for (int j = 1; j <=powNumb; j++) {
System.out.printf("%10.0f",Math.pow(j, i));
}
}
Output:
1 2 3 4 5
1 1 1 1 1
1 2 3 4 5
1 4 9 16 25
1 8 27 64 125
1 16 81 256 625
1 32 243 1024 3125
1 64 729 4096 15625
Also, I had to switch powNumb and powValue in the for loop conditions.
Related
There is a loop that increments the counter 48 times to write certain values to an Excel file.
In the range 1 - 48, 4 blocks from 1 - 12 are to be written.
Expected example:
1 2 3 4 5 6 7 8 9 10 11 12 - 1 2 3 4 5 6 7 ... and so on (4 times).
I have tried different approaches, if/else, switch/case but here I have not come to any result.
My last approach is an if condition with the modolu operator.
for (int i = 1; i <= 48; i++) {
if (i % 12 != 0) {
for (int j = 1; j <= 12; j++) {
workBook.setNumber(HEADLINE_ROW, i + 6, j);
}
} else {
workBook.setNumber(HEADLINE_ROW, i + 6, 12);
}
}
But with this approach I get 12 12 12 12 and so on.
I recognize the error, but currently have no idea how to solve the problem. The part where data is written to the Excel file is rather unimportant. I am concerned with the logic.
I'm stuck in the logic here and can't get any further. Do you have any ideas or suggestions for improvement on how I can generate four 1 - 12 blocks side by side?
do something like that
python
for i in range(48):
index = i % 12 + 1
# do what ever you want here
print(index)
java
for(int i = 0; i < 48; i++) {
int index = i % 12 + 1;
// do something here
}
I think the pseudo code for what you want would be:
for (int i=1; i <= 48; i++) {
int j = i % 12 + 1; // 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 ...
// do something with i and (j + 1)
}
That is, work with the outer loop counter mod 12, which would give you the sequence 1, 2, ..., 12, four times.
public static void applicationB(int A, int B) {
int number = 1;
for (int row = 0; row < A; row++) {
for (int col = 0; col < B; col++) {
int output = number + row++;
System.out.printf("% 4d", output);
}
// does it skip because of this?
System.out.println("");
}
}
It outputs with A = 20, B = 5
1 2 3 4 5
7 8 9 10 11
13 14 15 16 17
19 20 21 22 23
The correct output should be
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
I cannot figure out how to get it to stop skipping 6, 12, and 18.
Am I just doing this a horrible way? or am I on the right track?
You are incrementing row in two places. Also, it would be easier to just have one loop, and output a line break every B elements (you can use i % B to test for this).
I would suggest the following approach. Iterate over the values you want to print out, from 1 to the maximum value (A). Then, print a newline whenever the remainder of value divided by the number of columns (B) is zero.
for (int value = 1; value <= A; value++) {
System.out.printf("% 4d", value);
if (value % B == 0) {
System.out.println("");
}
}
This question already has answers here:
Java - creating a triangle with numbers using nested for-loops [closed]
(2 answers)
Closed 8 years ago.
my output should look like in image 1, but my output looks like in image 2.
I am not suppose to print out ... there I have to print out same thing with 32 then 64. int
That is what i have so far, I got half of the triangle correct. I don't know how to reverse it though.
k = 1;
int j;
int l = 1;
for(int i=1; i <= 8; i++){
for(j=8; j>i; j--){
System.out.print(" ");
}
for(j=1; j<=k; j=j*2){
System.out.print(j + " ");
}
for (j = 1; j<k; j=j*2) {
System.out.print(j + " ");
}
k = k * 2;
System.out.println();
}
}
}
Your problem is, in the 2nd loop, you still go from j=1 -> k. You can simply do a k -> 1 loop to get a reversed sequence.
Also java has printf method, you may want to take a look..
Some example codes:
int rows = 8;
for (int r = 0; r <= rows; r++) {
System.out.print(new String(new char[rows - r]).replace("\0", " "));
int c = 0;
for (int i = 0; i <= r; i++)
System.out.printf("%s%s", 1<<i, r == 0? "\n" : " ");
if (r > 0)
for (int i = r-1; i >= 0; i--)
System.out.printf("%s%s", 1<<i, i == 0? "\n" : " ");
}
just adjust the rows to the value you like.
I did a test with rows=8, it prints:
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 256 128 64 32 16 8 4 2 1
I am studying the Pyramid Numbers in Java which has a code like this.
public class PyramidCharForLup {
public static void main(String[] args) {
int x = 7;
for (int i = 1; i <= x; i++)
{
for (int j = 1; j <= x - i; j++)
System.out.print(" ");
for (int k = i; k >= 1; k--)
System.out.print((k >=10) ?+ k : " " + k);
for (int k = 2; k <=i; k++)
System.out.print((k>= 10) ?+ k : " " + k);
System.out.println();
}
}
}
The output is
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
I chop the code and study it line by line I'm already studying in this line so far.. heheh
So here's the code that I want to ask.
public class PyramidCharForLup {
public static void main(String[] args)
{
int x = 7;
for (int i = 1; i <= x; i++) {
for (int j = 1; j <= x - i; j++)
System.out.print(j);
}
}
}
the output of this is 123456123451234123121 for clarification if you are to arrange
123456 12345 1234 123 12 1
First Question: is i in the code for (int j = 1; j <= x - i; j++) become i= 1234567?
Second Question: from the initialization which is one if increment 1..2..3..5..6..7.. are the increment subtracting the last number from 1234567 like
1234567-1... 123456 - 1 ... 12345 - 1 ... 1234 - 1 ... 123 - 1 ... 12-1...1-0.... till false Am I right?
and that's the reason why i got this output 123456 12345 1234 123 12 1
Your code has two nested loops (one loop runs inside the other). This is what happens:
You set x to 7
You are starting the outer loop, setting i = 1. Since 1 <= 7 the loop is entered.
You are starting the inner loop, setting j = 1. Since 1 <= 6 (7-1 from x - i) the loop is entered.
j (1) is printed.
j is increased with 1 and since 2 is less than 6 the inner loop continues.
j (2) is printed.
This goes on until j is 6.
j (6) is printed.
j is increase by 1 and is now 7 which is not <= 6, so the inner loop exits.
i is increased by one (and is now 2).
The inner loop is entered again, setting j to 1. Since j < 5 (7-2 from x - i) the loop is entered.
11 j (1) is printed.
12 j is increase by one and is now 2. Since 2 < 6 the loop continues.
This goes on until i reaches 7. When i the is increased by 1 it is 8 which is not <= 7 so the outer loop also exists and your program is done.
Your program will count and write the digits from 1..6, then from 1..5 until it reaches 1..1
I need help. My assignment is to write a Java program using nested loops to print out the following output pattern:
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
//pattern1
for(int outer=1;outer<=6;outer++) // outer loop controls number of rows
{
for(int inner=1;inner<=outer; inner++) // another loop to control number of numbers in each row.
{
System.out.print(inner);
}
System.out.println(); // move the cursor from the end of the current line to the beggiing to the next line
}
//pattern 2
for(int outer =1; outer<=6 ; outer++) //outer loop controls number of rows
{
//3-1 create spaces before numbers.
for(int space=1; space<=6-outer; space++ ) //group controls number of spaces
{
System.out.print(" ");
}
//3-2 print out real numbers.
for(int inner=1;inner<=outer; inner++) // another loop to control number of numbers in each row.
{
System.out.print(inner);
}
System.out.println();
}
Those two codes are back to back, but I do not understand how I would make the numbers 2 4 8 16 etc show up, and put them back to back.
What's wrong with my code? Is there a better way of doing this in Java?
A simple version with bit shifting and static column size / padding - could be improved by using Math.getExponent() for dynamically repeating spaces and format %3d ...
public static void f(int n) {
for (int i = 0; i < n; i++) {
for (int l = n - i; l > 0; l--) { // padding for symmetry
System.out.print(" ");
}
for (int j = 0; j <= i; j++) { // "left side" of pyramid
System.out.printf("%3d ", 1 << j);
}
for (int k = i - 1; k >= 0; k--) { // "right side" of pyramid
System.out.printf("%3d ", 1 << k);
}
System.out.println();
}
}
Output:
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
You're going to use a nested loop with an if statement controlling the output.
This code should help you with your formatting. You'll have to figure out how to add the || so that it flips the triangle and how to format your print statements so it looks like that.
int totalWidth = 8;
for (int row = 1; row <= totalWidth; row++) {
for (int col = 1; col <= totalWidth; col++) {
if (col <= totalWidth - row) {
System.out.print(" ");
}else {
System.out.print("*");
}
}
System.out.println();
}
It will output
*
**
***
****
*****
******
*******
********
public class pyramid
public static void f(int n) {
for (int i = 0; i < n; i++) {
for (int l = n - i; l > 0; l--) { // padding for symmetry
System.out.print(" ");
}
for (int j = 0; j <= i; j++) { // "left side" of pyramid
System.out.printf("%3d ", 1 << j);
}
for (int k = i - 1; k >= 0; k--) { // "right side" of pyramid
System.out.printf("%3d ", 1 << k);
}
System.out.println();
}
}