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();
}
Related
for (int i = 1; i <= 4; i++)
{
int n = 4;
for (int j = 1; j <= n - i; j++)
{
System.out.print(" ");
}
for (int k = i; k >= 1; k--)
{
System.out.print(k);
}
for (int l = 2; l <= i; l++)
{
System.out.print(l);
}
System.out.println();
}
for (int i = 3; i >= 1; i--)
{
int n = 3;
for (int j = 0; j <= n - i; j++)
{
System.out.print(" ");
}
for (int k = i; k >= 1; k--)
{
System.out.print(k);
}
for (int l = 2; l <= i; l++)
{
System.out.print(l);
}
System.out.println();
}
My output:
Enter height:
12
1
212
32123
4321234
543212345
65432123456
7654321234567
876543212345678
98765432123456789
109876543212345678910
1110987654321234567891011
12111098765432123456789101112
It prints correctly, but with no spacing...
Would I need another for loop with a println that just prints a space under the first for loop?
Also, if I did that would it still work for double digit heights such as 12?
I would greatly appreciate help, thank you.
You just need to play around with spaces.
for (int i = 1; i <= height; i++) {
int n = height;
int n2 = height + 1;
for (int j = 1; j <= n - i; j++) {
System.out.print("" + String.format("%3s", " ") + " ");
}
for (int k = i; k >= 1; k--) {
System.out.print("" + String.format("%3s", k) + " ");
}
for (int l = 2; l <= i; l++) {
System.out.print("" + String.format("%3s", l) + " ");
}
System.out.println();
System.out.println();
}
OUTPUT: For height 10
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
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
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
Hi all I am trying to create a pyramid program that asks a user for int between 1-15. When I enter 1-9 it creates a perfect pyramid. But if I enter 10-15 the line down the middle goes to right with each number pass 10 no longer making a pyramid.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a number between 1-15");
int dig = input.nextInt();
for (int i = 1; i <= dig; i++) {
for (int j = 1; j <= dig - i; j++) {
System.out.print(" ");
}
for (int k = i; k >= 1; k--) {
System.out.print(" " + k);
}
for (int k = 2; k <= i; k++) {
System.out.print(" " + k);
}
System.out.println();
}
}
}
Thank you for your help!
You can still achieve the same result with your code but rather than use the print method, use the printf method that takes a format specifier as the first parameter.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a number between 1-15");
int dig = input.nextInt();
for (int i = 1; i <= dig; i++) {
for (int j = 1; j <= dig - i; j++) {
System.out.printf("%4s", " ");
}
for (int k = i; k >= 1; k--) {
System.out.printf("%4d", k);
}
for (int k = 2; k <= i; k++) {
System.out.printf("%4d", k);
}
System.out.println();
}
}
Here each integer that is printed out is padded with additional spaces, by specifying a fixed width of 4 characters, therefore compensating for integers that can either be one or two characters long.
Here you go :).
You have to count the number of digits of the max number and then create bigger spaces at beginning, but start to make them smaller when two-digit number comes.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a number between 1-15");
int dig = input.nextInt();
int length = ("" + dig).length();
for (int i = 1; i <= dig; i++) {
for (int j = 1; j <= dig - i; j++) {
for (int f = 0; f < length; f++) {
System.out.print(" ");
}
System.out.print(" ");
}
for (int k = i; k >= 1; k--) {
for (int j = 0; j < length; j++) {
System.out.print(" ");
}
for (int j = 0; j < length - (""+k).length() + 1; j++) {
System.out.print(" ");
}
System.out.print(k);
}
for (int k = 2; k <= i; k++) {
for (int j = 0; j < length; j++) {
System.out.print(" ");
}
for (int j = 0; j < length - (""+k).length() + 1; j++) {
System.out.print(" ");
}
System.out.print(k);
}
System.out.println();
}
}
Output for 15 :
Enter a number between 1-15
15
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
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13
14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Number of rows to print Pyramid: ");
int rows= scanner.nextInt();
scanner.close();
String star="*";
System.out.println("========================================");
for (int i = 0; i < rows; i++) {
for (int j =0; j<=rows-i ; j++) {
System.out.print(" ");
}
for(int k=0; k<=i; k++) {
System.out.print(star + " ");
}
System.out.println();
}
}
Output for 5:
*
* *
* * *
* * * *
* * * * *
How do I print out a table of numbers when the last number is given by the user, and the variables are added together to get new variables? For example, if the user gives the value 3 for numberOfRows, an example output would be;
1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
Here's what I have so far:
int numberOfRows;
int numberOfVariables = numberOfRows*numberOfRows;
int i = 0;
boolean cycle = false;
if(i < numberOfVariables){
cycle = true;
i++;
} else if(i >= numberOfVariables){
cycle = false;
i++;
}
System.out.println(cycle);
for(cycle = true;;){
System.out.println(i + "\t");
i++;
}
I'm not sure how to continue.
public static void main(String args[]) throws NumberFormatException,
IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number of rows : ");
int n = Integer.parseInt(reader.readLine());
for (int i = 0; i < n; i++) {
for (int j = i; j < n + i; j++) {
System.out.print(j + "\t");
}
System.out.println();
}
}
Output :
Enter number of rows : 6
0 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
5 6 7 8 9 10
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Wnter row count");
int numberOfRows = scanner.nextInt();
for (int i = 0 ; i <= numberOfRows ; i++) {
for (int j = 0 ; j <= numberOfRows ; j++) {
System.out.print(i + j + " ");
}
System.out.println();
}
}
}
Output
Wnter row count
3
0 1 2 3
1 2 3 4
2 3 4 5
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