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
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.
I need to fix my output as shown at the bottom, i have code that outputs the correct math values
but it does not output the correct angle that is expected of the example output that i have provided below. ( I know this is pretty simple for most of you stack users but im a beginner in java and this is something that confuses me ). I have not come up with any ideas on what i can do to fix this issue and put in the correct angle.
Instructions:
-Write a program using a Scanner that asks the user for a number
n between 1 and 9 (inclusive).
-The program prints a triangle with n rows.
-The first row contains only the square of 1, and it is right-justified.
-The second row contains the square of 2 followed by the square of 1,
and is right justified.
-Subsequent rows include the squares of 3, 2, and 1, and then 4, 3, 2
and 1, and so forth until n rows are printed.
Assuming the user enters 4, the program prints the following triangle to the console
1
4 1
9 4 1
16 9 4 1
Code:
import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a number between 1 and 9 inclusive:");
int n = scan.nextInt();
for (int i = 1 ; i <=n; i++) {
for (int j = n-i; j >=1; j--) {
System.out.print("");
}
for (int k = i; k <= n; k++ ) {
System.out.print(" " + i * i);
}
System.out.println(" ");
}
scan.close();
}
}
My output:
Please enter a number between 1 and 9 inclusive: 4
1 1 1 1
4 4 4
9 9
16
Here is a short and simple variant.
for (int row = 1; row <= n; row ++) {
for (int col = n; col >= 1; col--) {
if (col <= row) {
System.out.print(String.format("%2d ", col * col));
} else {
System.out.print(" ");
}
}
System.out.println();
}
Here is the output generated for input 9:
1
4 1
9 4 1
16 9 4 1
25 16 9 4 1
36 25 16 9 4 1
49 36 25 16 9 4 1
64 49 36 25 16 9 4 1
81 64 49 36 25 16 9 4 1
TL;DR; Use a pen and paper for annoying logic problems.
The easiest way to do a problem like this is to write out the locations you're printing in terms of the variables you are iterating through. An extra variable, col helps us keep track of the current column opposite of the direction we are iterating from.
We can notice that the number just corresponds to the column position, squared, if we were counting columns from right to left.
We can also notice that the number of blank spaces is equal to n - one less than the current row iteration (since that starts at 0).
1
4 1
9 4 1
16 9 4 1
All of that nonsense aside, we can use String.format() to make the output even for double digit numbers.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a number between 1 and 9 inclusive:");
int n = scan.nextInt();
for (int i = n; i > 0; i--) {
int col = 4;
// Print spaces
for (int j = 0; j < (i-1); j++) {
System.out.print(" ");
col--;
}
// Print numbers
for (int j = (i-1); j < n; j++) {
System.out.print(String.format("%2d", col*col) + " ");
col--;
}
System.out.println();
}
scan.close();
}
I have used the code you guys provided and it had some logical issues when inputting a number past 4 it would not square the triangle correctly and would only create the shape. I have solved the issue by adding a while loop and editing the for loops and not it outputs correctly.
Incorrect output:
Please enter a number between 1 and 9 inclusive: 8
9
4 9
1 4 9
0 1 4 9
1 0 1 4 9
4 1 0 1 4 9
9 4 1 0 1 4 9
16 9 4 1 0 1 4 9
Correct Code Given:
import java.util.Scanner;
public class AverageGrades {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a number between 1 and 9 inclusive:");
int n = scan.nextInt();
for (int row = 1; row <= n; row ++) {
for (int col = n; col >= 1; col--) {
if (col <= row) {
System.out.print(String.format("%2d ", col * col));
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
My Code:
import java.util.Scanner;
public class TriangleTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter Number between 1 and 9 : ");
int n = scan.nextInt();
while(n < 0 || n > 9)
{
System.out.print("Please enter a valid value between 1 and 9 :");
n = scan.nextInt();
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= n*2-(i + i); j++)
{
System.out.print(" ");
}
for(int l = i + 1; l > 0; l--)
{
int r = l*l;
System.out.print(r + " ");
}
System.out.println("\n");
}
scan.close();
}
}
Correct Output:
Enter Number between 1 and 9 : 10
Please enter a valid value between 1 and 9 :9
1
4 1
9 4 1
16 9 4 1
25 16 9 4 1
36 25 16 9 4 1
49 36 25 16 9 4 1
64 49 36 25 16 9 4 1
81 64 49 36 25 16 9 4 1
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.
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();
}
}