Number Pyramid In Java - java

Im sorry for bothering for this small thing but really i feel confused about this. I want to get output like;
9
89
789
6789
56789
456789
3456789
23456789
123456789
23456789
3456789
456789
56789
6789
789
89
9
But im taking my output after 123456789 line. My codes are;
for(int column = 1; column <= 9; column++) {
for(int row = 1; row <= 9; row++) {
if(column <= row) {
System.out.print(row);
} else {
System.out.print(" ");
}
}
System.out.println(' ');
}
Thanks For Your Attention.

This is a bit dirty, but gives the desired output:
for(int column = -9; column <= 9; column++)
{
if (column == 0) column = 2;
for(int row = 1; row <= 9; row++)
{
if(Math.abs(column) <= row)
{
System.out.print(row);
} else
{
System.out.print(" ");
}
}
System.out.println();
}

This is a different approach that works
String txt = "123456789";
String msk = " ";
boolean done = false;
boolean pos = true;
int rows = 0;
int c = 0;
int maxrows=19;
while (!done) {
System.out.println(msk.substring(c) + txt.substring(9-c));
if (pos) {
c++;
if (c>8) pos = false;
} else {
c--;
if (c<1) pos = true;
}
rows++;
if (rows==maxrows) done = true;
}

Here's a simple way:
int num = 9;
int spaces = 8;
boolean decreasingSpaces = true;
for (int row=0; row < 17; row++) {
for (int i=0; i < spaces; i++)
System.out.print(" ");
for (int j=num; j <= 9; j++)
System.out.print(j);
System.out.println();
if (decreasingSpaces) {
spaces--;
num--;
if (spaces < 0) {
decreasingSpaces = false;
spaces += 2;
num += 2;
}
} else {
spaces++;
num++;
}
}

/*output:
54321
5432
543
54
5
*/
class NumberPyramid1
{
public static void main(String args[])
{
int n=5;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print(" ");
}
for(int k=n;k>=i;k--){
System.out.print(k);
//n--;
}
System.out.println();
//n--;
}
}
}

for(int column = 1; column <= 9; column++) {
for(int row = 1; row <= 9; row++) {
if(column <= row) {
System.out.print(row);
} else {
System.out.print(" ");
}
}
System.out.println(); // println will create a newline
}

Related

Number Pattern in Java

I am trying to make a program that outputs
So far I have done:
public class printPattern {
public static void main(String[] args) {
int a = 6;
int i, j;
int max = 1;
int num;
for(i = 1; i <= a; i++){
num = 1;
System.out.println("0");
for(j = 1; j <= max; j++){
System.out.print(num);
System.out.print(" ");
num++;
}
max++;
}
}
}
But the output I am getting is
The "0" is there to show the spaces, but I want to remove the entire line which contains the first "0" so that the output starts with a "1". I am unsure what to change. Any help would be much appreciated. Thank You.
I suggest adding conditions (if we need to print out delimiters):
for (int i = 1; i <= a; ++i) {
if (i > 1)
System.out.println(); // more than 1 line, need delimiter (new line)
for (int j = 1; j <= i; ++j) {
if (j > 1)
System.out.print(" "); // more than 1 column, need delimiter (space)
System.out.print(j);
}
}
Most shortest form:
String str = "";
for (int i = 1; i <= 6; i++) {
str = str + " " + i;
System.out.println(str);
}
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
Here what I've got
Here you can check https://code.sololearn.com/c9ALHSGAa6ZZ
class printPattern {
public static void main(String[ ] args) {
int a = 6;
int i, j;
int max = 1;
int num;
for (i = 1; i <= a; i++) {
num = 1;
for (j = 1; j <= max; j++) {
System.out.print(num);
System.out.print(" ");
num++;
}
System.out.println();
max++;
}
}
}
public class printPattern {
public static void main(String[] args) {
int a = 6;
int i, j;
int max = 1;
int num;
for(i = 1; i <= a; i++){
num = 1;
for(j = 1; j <= max; j++){
System.out.print(num);
System.out.print(" ");
num++;
}
System.out.println(" ");
max++;
}
}
}
this is working as you asked. just remove 0 from print statement
How about this ?
public void pyramid(int size) {
for(int i = 1; i <= size; i++) {
for(int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println("");
}
}

Printing a Diamond with * and "o" in the centre using do while loop in java

Im trying to print a Diamond with * and "o" in the centre using do while loop. I have already done this with For loop but unsure how to change it to while loop. Can anyone give some support.
public static void diamond1() {
System.out.println("Diamond Height: " + DIAMOND_SIZE);
System.out.println("Output for: For Loop");
int noOfRows = DIAMOND_SIZE;
//Getting midRow of the diamond
int midRow = (noOfRows)/2;
//Initializing row with 1
int row = 1;
//Printing upper half of the diamond
for (int i = midRow; i > 0; i--)
{
//Printing i spaces at the beginning of each row
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
//Printing j *'s at the end of each row
for (int j = 1; j <= row; j++) {
System.out.print("* ");
}
System.out.println();
//Incrementing the row
row++;
}
//Printing lower half of the diamond
for (int i = 0; i <= midRow; i++) {
//Printing i spaces at the beginning of each row
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
//Printing j *'s at the end of each row
int mid = (row+1) / 2;
for (int j = row; j > 0; j--) {
if(i==0 && j==mid) {
System.out.print("o ");
}
else {
System.out.print("* ");
}
}
System.out.println();
//Decrementing the row
row--;
}
}
How would I go about doing this?
In general, a for loop like this:
for (command1; statement; command2){
// Code to loop
}
is equal to this while loop:
command1;
while(statement){
// Code to loop
command2;
}
and equal to this do-while loop:
command1;
do{
if (statement){
// Code to loop
command2;
}
} while(true);
If it is certain that the first loop must happen then you can use the below do-while:
command1;
do{
// Code to loop
command2;
} while(statement);
Here you go :
public static void Diamond() {
int DIAMOND_SIZE = 5;
System.out.println("Diamond Height: " + DIAMOND_SIZE);
System.out.println("Output for: For Loop");
int noOfRows = DIAMOND_SIZE;
//Getting midRow of the diamond
int midRow = (noOfRows)/2;
//Initializing row with 1
int row = 1;
int i = midRow;
while(i > 0){
//Printing i spaces at the beginning of each row
int j = 1;
while(j <= i){
System.out.print(" ");
j++;
}
//Printing j *'s at the end of each row
j = 1;
while(j <= row){
System.out.print("* ");
j++;
}
System.out.println();
//Incrementing the row
row++;
i--;
}
i = 0;
while(i <= midRow){
//Printing i spaces at the beginning of each row
int j = 1;
while(j <= i){
System.out.print(" ");
j++;
}
//Printing j *'s at the end of each row
int mid = (row+1) / 2;
j = row;
while(j > 0){
if(i == 0 && j == mid)
System.out.print("o ");
else
System.out.print("* ");
j--;
}
System.out.println();
//Decrementing the row
row--;
i++;
}
}

How To Traverse And Print A 2D Array In ZigZag Path

Suppose I have an array
A[][] = {{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9,10,11,12}};
And I want to print a wave so that the output comes out like this
{1,5,9,10,6,2,3,7,11,12,8,4}
How can I do this??
here is my code but it is giving me ArrayIndexOutOfBound
public class Wave1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [][] a={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}} ;
System.out.println("Row "+a.length);
for(int i=0;i<a.length;i++){
System.out.println("Column "+i+"th "+a[i].length);
}
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
System.out.print(a[i][j]+" ");
}
}
System.out.println();
for(int i=0;i<a.length+1;i++){
if(i%2==0){
for(int j=0;j<a.length;j++){
System.out.print(a[j][i]+" ");
}
}
else{
for(int j=a.length-1;j>=0;j--){
System.out.print(a[j][i]+" ");
}
}
}
Thanks in advance
It seems what you want is to print each column of the matrix, where the even indexed columns are printed in ascending order and the odd indexed columns are printed in descending order.
for (int col = 0; col < a[0].length; col++) {
if (col % 2 == 0) {
for (int row = 0; row < a.length; row++)
System.out.print(a[row][col] + " ");
System.out.println();
} else {
for (int row = a.length - 1; row >= 0; row--)
System.out.print(a[row][col] + " ");
System.out.println();
}
}
Output :
1 5 9 13 17
18 14 10 6 2
3 7 11 15 19
20 16 12 8 4
You can do it easily by taking two for loops, one for forward scanning and another for backward scanning.
Here is the code snippet:
public static void main (String[] args)
{
int [][] a={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}};
for(int k = 0;k < 4;k++) {
for(int i = 0;i < a.length;i++) {
System.out.print(a[i][k] + " ");
}
k++;
for(int i = a.length - 1;i >=0 ;i--) {
System.out.print(a[i][k] + " ");
}
}
}
Output:
1 5 9 13 17 18 14 10 6 2 3 7 11 15 19 20 16 12 8 4
That's like traversing an array similiar to traversing a tree in zigzag traversal, but in this case we do not need stacks just modular arithmetich will be enough, here is your solution;
Method #1 - dummyPrint
private static void dummyPrint(int[][] array) {
System.out.print("Dummy Print: ");
for(int j = 0; j < array[0].length; j++) {
if(j%2 == 0) {
for(int i = 0; i < array.length; i++)
System.out.printf("%2d ", array[i][j]);
} else {
for(int i = array.length-1; i >= 0; i--)
System.out.printf("%2d ", array[i][j]);
}
}
System.out.println();
}
Method #2 - prettyPrint
private static void prettyPrint(int[][] array) {
System.out.println("Pretty Print;");
System.out.println("*************");
for(int j = 0; j < array[0].length; j++) {
if(j%2 == 0) {
for(int i = 0; i < array.length; i++)
System.out.printf("%2d ", array[i][j]);
} else {
for(int i = array.length-1; i >= 0; i--)
System.out.printf("%2d ", array[i][j]);
}
System.out.println();
}
}
Demonstration Code
public class ArrayDemo {
public static void main(String[] args) {
int [][] array = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
dummyPrint(array);
System.out.println();
prettyPrint(array);
}
private static void dummyPrint(int[][] array) {
System.out.print("Dummy Print: ");
for(int j = 0; j < array[0].length; j++) {
if(j%2 == 0) {
for(int i = 0; i < array.length; i++)
System.out.printf("%2d ", array[i][j]);
} else {
for(int i = array.length-1; i >= 0; i--)
System.out.printf("%2d ", array[i][j]);
}
}
System.out.println();
}
private static void prettyPrint(int[][] array) {
System.out.println("Pretty Print;");
System.out.println("*************");
for(int j = 0; j < array[0].length; j++) {
if(j%2 == 0) {
for(int i = 0; i < array.length; i++)
System.out.printf("%2d ", array[i][j]);
} else {
for(int i = array.length-1; i >= 0; i--)
System.out.printf("%2d ", array[i][j]);
}
System.out.println();
}
}
}
The Output
Dummy Print: 1 5 9 10 6 2 3 7 11 12 8 4
Pretty Print;
*************
1 5 9
10 6 2
3 7 11
12 8 4
There is a discrepancy with the code you print and the desired output. In your code, there is a line that prints "row" and another that prints "column".
This code
public class Wave1 {
public static void main(String[] args) {
int [][] a={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}} ;
String separator="";
System.out.print("{");
for (int[] row: a) {
for (int cell: row) {
System.out.print(separator+cell);
separator=",";
}
}
System.out.println("}");
}
}
prints
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}
I was not able to figure out the order in which the numbers should be printed. I assumed it did not matter.
public class Wave1 {
public static void main(String args[])
{
int [][] wave={{1,2,3,4},{5,6,7,8},{8,9,10,11},{12,13,14,15}};
for(int i=0;i<wave.length;i++){
if(i%2==0){
for(int j=0;j<wave[0].length;j++){
System.out.print(wave[i][j]+" ");
}
System.out.println();
}
else{
for(int j=wave[0].length-1;j>=0;j--){
System.out.print(wave[i][j]+" ");
}
System.out.println();
}
}
}
}
public static void wave(int[][]input)
{
int row=0;
int col=0;
int steps=1;
System.out.print(input[0][0]+" ");
while(steps<=input.length*(input[0].length)-1)
{
if(col%2==1)
{
row--;
}
else
{
row++;
}
if(row<0)
{
row++;
if(col%2==1)
{
col++;
}
}
if(row>input.length-1)
{
row--;
if(col%2==0)
{
col++;
}
}
System.out.print(input[row][col]+" ");
steps++;
}
}
Check this one:
Use this function it will take 2D Array as its input and would print a zigzag Path
public static void wavePrint(int input[][]){
int count = 1;
int rows = input.length;
int cols = input[0].length;
for(int i = 0; i < cols; i++){
if(count % 2 != 0){ //odd column
int sine_e = 0;
while(sine_e < rows){
System.out.print(input[sine_e][i]+" ");
sine_e++;
}
count++;
}else{
int sine_e = rows - 1; //even column
while(sine_e >= 0){
System.out.print(input[sine_e][i]+" ");
sine_e--;
}
count++;
}
}//end of for loop
}

N * N matrix by Java

This is what I written:
import javax.swing.JOptionPane;
public class JavaTest{
public static void main(String[] args){
String numberString = JOptionPane.showInputDialog(null, "Enter number here: ",
null, JOptionPane.INFORMATION_MESSAGE);
int number = Integer.parseInt(numberString);
printMatrix(number);
}
public static void printMatrix(int n){
int[][] myList = new int[n][n];
String output = "";
for (int row = 1; row <= n; row++){
for (int col = 1; col <= n; col++){
myList[row][col] = (int) (Math.random() * 2);
}
}
for (int row = 1; row <= n; row++){
for (int col = 1; col <= n; col++){
if (col == n){
output += "\n" + myList[row][col];
}
else{
output += myList[row][col] + " ";
}
}
}
if (n < 0){
JOptionPane.showMessageDialog(null,
"Invalid input!");
}
else{
JOptionPane.showMessageDialog(null,
output);
}
}
}
I run it and enter 3 in a dialog box, and the eclipse IDE shows that
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at JavaTest.printMatrix(JavaTest.java:17)
at JavaTest.main(JavaTest.java:8)
I guess at line 17 and 8 the program goes wrong but I don't know how to improve it.
What can I do to improve my code? Thanks!
You're looping from 1 to n:
for (int row = 1; row <= n; row++){
for (int col = 1; col <= n; col++){
Indexes begin at 0, not at 1. The loops should be from 0 to n-1:
for (int row = 0; row < n; row++){
for (int col = 0; col < n; col++){
(This same error may likely be in other places than just the first line that threw the exception.)

Trouble printing chars thru a 2 dim java array

I tried to run this code as a score table where I have chars and ints as below for heading;
A B c
1
2 65 //this is where I'm stuck again!
3
In order to print the score like (65) above in a particular place (matrix) but as soon as I try to add the print statements the table falls apart. Any help would be appreciated;
public class Table3 {
static int[][] list = new int[4][4];
//private char column = 'A';
//private int row = 1;
private static int row = 1;
public Table3(){
//column = 'A';
for (int i = 0; i < 4; i++) {
for (int j = 1; j < 4; j++)
list[i][j] = 0;
}
}
public static void table(char col, int row, int value) {
//System.out.printf("\n\n%s\n", "Table");
for (int i = 1; i < 4; i++) {
System.out.print(row + " ");
row++;
for (int j = 1; j < 4; j++)System.out.print(col + " ");
System.out.println("\n");
col++;
if (row >= 0 && row <= 4 && col >=0 && col <= 4)
System.out.print(list[col][row]=value);
System.out.println("\n");
}
}
}
Client
public class TableTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Table3 t = new Table3();
t.table('A', 5, 5);
}
}
Learn how to use System.out.printf. The docs are here.
public class Table3
{
static int numRows = 4;
static int numCols = 4;
static int[][] list = new int[numRows][numCols];
public Table3()
{
//column = 'A';
for (int i = 0; i < 4; i++)
{
//this is the row number so you don't have to print it manually
//just print the array
list[i][0] = i;
//initialize the list to 0
for (int j = 1; j < 4; j++)
{
list[i][j] = 0;
}
}
}
public static void table(char col, int row, int value)
{
list[row][col] = value;
int columnWidth = 5; //in characters
//empty space before first column header
for (int i = 0; i < columnWidth; i++)
{
System.out.print(" ");
}
//print the column headers (A through C)
for (int i = 1; i < numCols; i++)
{
System.out.printf("%-" + columnWidth" + "c", (char)(64 + i));
}
System.out.println(); //get off of the column header row
//print the rest of the table
for (int i = 1; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
if (list[i][j] == 0)
{
System.out.printf("%" + columnWidth + "s", " ");
}
else
{
System.out.printf("%-" + columnWidth + "d", list[i][j]);
}
}
System.out.println("\n");
}
}
}

Categories