1
2 1 2
4 2 1 2 4
8 4 2 1 2 4 8
Trying to print the triangle as above, but I'm having hard time printing squares of two (n *= 2). How do I incorporate this? Currently, it's giving me the following output. Appreciate your help.
Current output (incorrect numbers):
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
Here is my code so far (for ex, height = 4):
for(int i=1; i<=height; i++) {
for (int j = 1; j <= height-i; j++)
System.out.print(" ");
for(int k=i; k>=2; k--)
System.out.printf("%-3d",k);
for(int l=1; l<=i; l++)
System.out.printf("%-3d",l);
System.out.println();
}
There you go
for(int i=1; i<=height; i++) {
for (int j = 1; j <= height-i; j++)
System.out.print(" ");
for(int k=1<<i-1; k>=2; k>>=1)
System.out.printf("%-3d",k);
for(int l=1; l<=1<<i-1; l<<=1)
System.out.printf("%-3d",l);
System.out.println();
}
thecode.
Related
I'm trying to print out this pattern but I'm having trouble arranging the numbers and spacing them out to achieve the expected out put.
Expected Output:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Here is my Code. I don't know how to space the numbers and put the correct numbers in the order that the out put has specified.
import java.util.Scanner;
public class patterns{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
for (int i = 6; i >= 1; i--)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = i; j <= 6; j++)
{
System.out.print(j);
}
System.out.println();
}
System.out.println();
for (int i = 1; i <= 6; i++)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = i; j <= 6; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
This is the Output I'm Getting
run:
6
56
456
3456
23456
123456
123456
23456
3456
456
56
6
You should think that each triangle block actually has 6x6 square cells. So it means you need 2 nested loops, each will iterate 6 times.
Then inside you will decide whether you print a number or space, depending on some condition.
for (int i = 6; i >= 1; i--)
{
for (int j = 1; j <= 6; j++)
{
if (j < i)
{
System.out.print(" ");
} else
{
System.out.print(7 - j + " ");
}
}
System.out.println("");
}
System.out.println("");
for (int i = 1; i <= 6; i++)
{
for (int j = 1; j <= 6; j++)
{
if (j < i)
{
System.out.print(" ");
} else
{
System.out.print(j-i+1 + " ");
}
}
System.out.println("");
}
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
int a = 654321;
String str = new Integer(a).toString();
for (int i = str.length() - 1; i >= 0 ; i--) {
String g = String.format("%6s", str.substring(i));
System.out.println(g.replaceAll(".(?=.)", "$0 "));
}
System.out.println();
str = new StringBuffer(str).reverse().toString();
for (int i = str.length() - 1; i >= 0 ; i--) {
String g = String.format("%6s", str.substring(0, i+1));
System.out.println(g.replaceAll(".(?=.)", "$0 "));
}
Here's the modified code for your patterns.
public class Patterns {
public static void main(String[] args) {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < (5 - i) * 2; j++) {
System.out.print(" ");
}
for (int j = i + 1; j >= 1; j--) {
System.out.print(j + " ");
}
System.out.println();
}
System.out.println();
for (int i = 0; i < 6; i++) {
for (int j = 0; j < i * 2; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 6 - i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Output
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Update: fixed bug for spaces
Working=> Consider the first half of the pattern
1 line 0: 10 spaces = (5 - 0) * 2 spaces
2 1 line 1: 8 spaces = (5 - 1) * 2 spaces
3 2 1 line 2: 6 spaces = (5 - 2) * 2 spaces
4 3 2 1 line 3: 4 spaces = (5 - 3) * 2 spaces
5 4 3 2 1 line 4: 2 spaces = (5 - 4) * 2 spaces
6 5 4 3 2 1 line 5: 0 spaces = (5 - 5) * 2 spaces
Consider the second half of the pattern
1 2 3 4 5 6 line 0: 0 spaces = 0 * 2 spaces
1 2 3 4 5 line 1: 2 spaces = 1 * 2 spaces
1 2 3 4 line 2: 4 spaces = 2 * 2 spaces
1 2 3 line 3: 6 spaces = 3 * 2 spaces
1 2 line 4: 8 spaces = 4 * 2 spaces
1 line 5: 10 spaces = 5 * 2 spaces
One way to do this would be to do:
import java.util.Scanner;
public class pattern{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 6; i++)
{
String val = "";
for (int j = i; j >= 1; j--)
{
val += j+ " ";
}
System.out.printf("%12s", val);
System.out.println();
}
System.out.println();
for (int i = 6; i >= 1; i--)
{
String val = "";
int ii=1;
for (int j = i; j >= 1; j--)
{
val += ii + " ";
ii++;
}
System.out.printf("%12s", val);
System.out.println();
}
}
Output:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
One thing to keep in mind is that when you are trying to format text like you are, you should use printf rather than adding print lines.
Best way is to use an array -> you loop less times. http://ideone.com/diYtvM Cheers!
import java.util.Scanner;
public class patterns{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[][] arr = new int[6][6];
for(int i = 0; i<6; i++)
arr[i][5] = 1;
for(int i = 1; i<6; i++)
for (int j = 4; j>=5-i; j--)
{
arr[i][j] = arr[i-1][j+1] + 1;
}
for(int i = 0; i<6; i++)
{
for (int j = 0; j<6; j++)
{
if(arr[i][j] > 0)
System.out.print(arr[i][j] + " ");
else
System.out.print(" ");
}
System.out.println("");
}
System.out.println("");
int z = 0;
for(int i = 5; i>=0; i--)
{
for(int k = 0; k<z; k++)
System.out.print(" ");
for (int j = 5; j>=0; j--)
{
if(arr[i][j] > 0)
System.out.print(arr[i][j] + " ");
}
System.out.println("");
z++;
}
}
}
OUT:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
i need to print out a numeric pyramid using java. that counts in multiples of 2 along with the spaces like how it is below. but the code i have it only bring up multiples of 1 of no spaces.
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
this is my code.
import java.util.Scanner;
public class NumericPyramid {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//Taking noOfRows value from the user
System.out.println("How Many Rows You Want In Your Pyramid?");
int noOfRows = sc.nextInt();
//Initializing rowCount with 1
int rowCount = 1;
System.out.println("Here Is Your Pyramid");
//Implementing the logic
for (int i = noOfRows; i > 0; i--) {
//Printing i*2 spaces at the beginning of each row
for (int j = 1; j <= i*2; j++) {
System.out.print(" ");
}
//Printing j value where j value will be from 1 to rowCount
for (int j = 1; j <= rowCount; j++) {
System.out.print(j+" ");
}
//Printing j value where j value will be from rowCount-1 to 1
for (int j = rowCount-1; j >= 1; j--) {
System.out.print(j+" ");
}
System.out.println();
//Incrementing the rowCount
rowCount++;
}
}
}
Output:
How Many Rows You Want In Your Pyramid?
7
Here Is Your Pyramid
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
For each j from 1 to n you need write 2^j. Currently you are writting only j.
So write function which for given k returns 2^k.
EDIT: For bigger n you need to use BigInteger:
import java.math.BigInteger;
public class NumericPyramid {
private static BigInteger pow(int exponent) {
BigInteger result = new BigInteger("1");
BigInteger two = new BigInteger("2");
for (int i = 0; i < exponent; i++) {
result = result.multiply(two);
}
return result;
}
and use it in both for loops. Replace
System.out.print(j+" ");
with
System.out.print(pow(j)+" ");
the pyramid goes 1 2 4 8 16, that's a sequence of 1 * 2 = 2, 2 * 2 = 4, 4 * 2 = 8 and so on, and then goes inverse, 8 / 2 = 4, 4 / 2 = 2, 2 / 2 = 1, you only need to multiply by 2 and divide by 2 when the center
for (int i = 0; i < noOfRows; i++) {
int cont = 1;
for (int j = 0; j <= i; j++) {
System.out.print(cont + " ");
cont = cont * 2;
}
cont = cont / 2;
for (int j = 0; j < i; j++) {
cont = cont / 2;
System.out.print(cont + " ");
}
System.out.println();
}
I know there is a lot of threads about this easy task, but I still need to put an extra for loop somewhere.
Thus far, I managed to do this:
public static void trikotnik(int n){
for (int i = 1; i <= n; i++ )
{
for (int j = 1; j < n; j++ )
System.out.print(" ");
n--;
for (int k = 1; k <= 2*i - 1; k++ )
System.out.print(k);
System.out.println("");
}
}
which outputs a nice pyramide (ignore format, i dunno how to do it properly).
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9
However, my pyramide needs to look like this:
1
2 3 4
3 4 5 6 7
4 5 6 7 8 9 0
5 6 7 8 9 0 1 2 3
Thank you for your help and suggestions!
just change the value of k in your second loop like below (start with i and border 3*i-2):
for (int i = 1; i <= n; i++ )
{
for (int j = 1; j < n; j++ )
System.out.print(" ");
n--;
for (int k = i; k <=3*i-2; k++ )
System.out.print(k%10);
System.out.println("");
}
OUTPUT:
1
234
34567
4567890
567890123
67890123456
7890123456789
890123456789012
.................
The Simple Solution: use a counter outside of the loop and ignore the inside process. Add one to it on each call and take that value modulo 10.
public static void trikotnik(int n) {
int val = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < n; j++)
System.out.print(" ");
n--;
for (int k = 1; k <= 2 * i - 1; k++)
System.out.print(val++%10);
System.out.println("");
}
}
Output:
1
234
56789
0123456
789012345
The following code:
for (i = 1; i <= 5; i++)
{
System.out.println();
for (j = 1; j <= i; j++) {
System.out.print(" ");
System.out.print(j);
}
}
produces:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Removing in the brace:
int i = 0;
int j = 0;
for (i = 1; i <= 5; i++)
{
System.out.println();
for (j = 1; j <= i; j++)
System.out.print(" ");
System.out.print(j);
}
produces:
2
3
4
5
6
I can understand that the nested for loop is iterating through the " " and printing them, then just printing one j, but I don't understand what happens to j = 1.
THe first time it goes through your for loop
it sets j = 1, checks j <= i
prints out the space
increments j, checks j < = i this fails because i = 1 and j = 2
exits the loop, and prints j and j = 2
For-loop without brace. Code you mentioned below:
for (j = 1; j <= i; j++)
System.out.print(" ");
System.out.print(j);
It is equal to the following code:
for (j = 1; j <= i; j++) {
System.out.print(" ");
}
System.out.print(j);
That's the problem, have a try and go ahead.
You can change it as follows:
for (j = 1; j <= i; j++) {
System.out.print(" ");
System.out.print(j);
}
The output will be expecte, like:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
I worked it out
System.out.println();
for (j = 1; j <= i; j++)
System.out.print(" ");
System.out.print(j);
The j is incremented j++ before being printed, as the loop has gone through an iteration befoe arriving at the System.out.print(j);
Since it only executes one statement, the value of j when it gets printed is one greater than the upper bound of your inner loop (2, 3, 4, 5, 6).
I am having trouble understanding how nested loops and numbers patterns work. I have completed all of the patterns except 3. Can someone please help me with this code and explain how this works?
public class Patterns7{
public Patterns7() {
}
public void displayPatternI(int lines)
{
System.out.println("\n\tPattern I\n");
for (int i = 1; i <= lines; i++)
{
for (int j = 1; j <= i; j++)
System.out.print (j + " " );
System.out.println();
}
}
public void displayPatternII (int lines)
{
System.out.println("\n\tPattern II to be implemented\n");
for (int i = 1;i <= lines; i++)
{
for(int j = i;j >= 1; j--)
System.out.print(j);
System.out.println();
}
System.out.println();
}
public void displayPatternIII (int lines)
{
System.out.println("\n\tPattern III to be implemented\n");
for (int i = 1; i <= lines; i++)
{
for (int space = 1; space <= lines-i; space++)
System.out.print (" ");
for (int j = 1; j <= i; j++)
System.out.print (j + " ");
System.out.println();
}
System.out.println();
}
}
Pattern III is supposed to look like this:
6
56
456
3456
23456
123456
But all I could get it to do was this:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
I am unsure of how to get the out put to start with 6 and decrease then increase.
Pattern V is supposed to look like this:
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
But it comes out like this:
1
1 2 3
1 2 3 4 5
Code:
public void displayPatternVI (int lines)
{
System.out.println("\n\tMy Own Pattern to be implemented\n");
for (int i = 1; i <= lines/2; i++)
{
for (int space = 1; space <= (lines/2)-i; space++)
System.out.print (" ");
for (int j = 1; j <= (i*2)-1; j++)
System.out.print (j + " ");
System.out.println();
}
for (int i = 1; i <= lines/2; i++)
{
for (int space = 1; space <= i-1; space++)
System.out.print (" ");
for (int j = 1; j <= lines-(i*2)+1; j++)
System.out.print (j + " ");
System.out.println();
}
System.out.println();
}
Pattern VI is supposed to look like this:
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
6 5 4 3 2 1 2 3 4 5 6
5 4 3 2 1 2 3 4 5
4 3 2 1 2 3 4
3 2 1 2 3
2 1 2
1
but it looks like this:
1
1 2 3
1 2 3 4 5
1 2 3 4 5
1 2 3
1
Can someone please help and explain to me how to this?
You're really only supposed to ask one question at a time, but here's a solution to your first issue. For SIZE equals 6:
6
56
456
3456
23456
123456
you would want something like:
String temp;
for(int i = 0; i < SIZE; i++)
{
temp = "";
for(int j = SIZE - i; j <= SIZE ; j++)
{
temp += j;
}
System.out.printf("%" + SIZE + "s\n", temp);
}
Try the second one on your own.
public void displayPatternIII (int lines)
{
System.out.println("\n\tPattern III to be implemented\n");
for (int i = lines; i >= 1; i--)
{
int space = 1;
for (; space <= i-1; space++)
System.out.print (" ");
for (int j = space; j <= lines; j++)
System.out.print (j);
System.out.println();
}
}
Output:
Pattern III to be implemented
6
56
456
3456
23456
123456