Java Number Pyramid - java

I want to create a number pyramid like this,
7
5 6 5
3 4 5 4 3
1 2 3 4 3 2 1
but I couldn't create it. I pasted my code and its output is like this,
7
5 6 7 6 5
3 4 5 6 7 6 5 4 3
1 2 3 4 5 6 7 6 5 4 3 2 1
What should I do to fix this ? Is my code totally wrong or litte changes are enough ?
import java.util.Scanner;
public class App {
public static void main(String[] args){
Scanner myScan = new Scanner(System.in);
System.out.print("Enter mountain width: ");
int width = myScan.nextInt();
System.out.println("Here is your mountain ");
myScan.close();
System.out.println();
for (int i = width; i >= 1; i=i-2)
{
for (int j = 1; j <= i*2; j++)
{
System.out.print(" ");
}
for (int j = i; j <= width; j++)
{
System.out.print(j+" ");
}
for (int j = width-1; j >= i; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}

You may compute the row index, then it's easy to get the number ranges
for (int i = width; i >= 1; i -= 2) {
int row_idx = (width - i) / 2;
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = i; j <= i + row_idx; j++) {
System.out.print(j + " ");
}
for (int j = i - 1 + row_idx; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}

You just need to count the level. You can do this by adding additional count variable. Lets take an example:
On level 0 you just print 7 and count is 0.
On level 1 count is 1. So you need to only print from i's current value + count amount on left and right. But since we counted 1 extra on left so a value will be less on right.
.........
And code will go on like this.
See code for better explanation:
public static void main( String[] args ) {
Scanner myScan = new Scanner(System.in);
System.out.print("Enter mountain width: ");
int width = myScan.nextInt();
System.out.println("Here is your mountain ");
myScan.close();
System.out.println();
int counter = 0;
for (int i = width; i >= 1; i = i - 2) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = i; j <= width - counter; j++) {
System.out.print(j + " ");
}
for (int j = width - 1 - counter; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
counter++;
}
}
I just changed a little bit on your code to make it work. Cheers mate !

Related

How to make a numbers triangle in Java using loops, numbers starting from right

my project to create a triangle in Java with numbers starting from the right
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// loop
for (int i=1; i <= 6; i++) {
// space
for(int j=1; j <= 6-i; j++)
System.out.print(" ");
// numbers
for(int k= 1; k <= i; k++)
System.out.print(k);
// new line
System.out.println();
}
}
}
Here is one way. It uses String.repeat() to pad with leading spaces.
outer loop controls the rows.
Then print the leading spaces
inner loop iterates backwards, printing the value and a space
then print a newline
int rows = 6;
for (int i = 1; i <= rows; i++) {
System.out.print(" ".repeat(rows-i));
for (int k = i; k > 0; k--) {
System.out.print(k+ " ");
}
System.out.println();
}
prints
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
If you don't want to use String.repeat() then use a loop.
Well the only thing you have to do is change order like you are first adding space and then number instead add number and then space.
code -
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// loop
for (int i=1; i <= 6; i++) {
// numbers
for(int k= 1; k <= i; k++)
System.out.print(k);
// space
for(int j=1; j <= 6-i; j++)
System.out.print(" ");
// new line
System.out.println();
}
}
}
Now we get the result -
start k=i then decrement it till k>=1.
for(int k= i; k >=1 ; k--)
System.out.print(k+" ");
output
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int lines = scanner.nextInt();
for (int i = 0; i <= lines; i++) {
//space
int spaceNum = lines - I;
for (int j = 0; j < spaceNum; j++) {
System.out.print(" ");
}
//numbers
int numbers = lines - spaceNum;
for (int j = numbers; j > 0; j--) {
System.out.print(" " + j);
}
System.out.println();
}
}
}

How to print numbers which are below left diagonal in matrix?

for (i = 1; i < 3; i++)
{
for (j = 0; j < 2; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
The above code is not able to print numbers which are below the left diagonal. For a 3x3 matrix my code is printing:
1 2 3
4 5 6
7 8 9
OUTPUT :
4 5
7 8
Desired output:
4
7 8
You could add an if statment like this :
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
if (i>j) {
System.out.print(a[i][j] + " ");
}
}
System.out.println();
}
Or better you could do :
for(int i=0;i<a.length;i++) {
for(int j=0;i>j;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
A simple way to achieve this depending on the matrix size is
final int matrixSize = 7; // your matrix size
for (int i = 0; i < matrixSize; ++ i) {
for (int j = 0; j < i; ++ j) System.out.print(a[i][j] + " ");
System.out.print("\n");
}

read all elements of martix diagonally from upper left to lower right

I am having a java code that reads diagonal values from upper right to lower left , But I want code to read values from upper left to lower right.
int [][]mat = { {1,2,3},
{4,5,6},
{7,8,9},
};
int N=3;
for (int s=0; s<N; s++) {
for (int i=s; i>-1; i--) {
System.out.print(mat[i][s-i] + " ");
}
System.out.println();
}
for (int s=1; s<N; s++) {
for (int i=N-1; i>=s; i--) {
System.out.print(mat[i][s+N-1-i] + " ");
}
System.out.println();
}
and output of above code is
1
4 2
7 5 3
8 6
9
I want sequence like
3 2 6 1 5 9 4 8 7
int [][]mat = { {1,2,3},
{4,5,6},
{7,8,9},
};
int N=3;
for (int s=N-1; s>-N; --s) {
int iMin = s>0 ? s : 0;
int iMax = s>0 ? N : N+s;
for (int i=iMin; i<iMax; ++i) {
System.out.print(mat[i-s][i] + " ");
}
}
System.out.println();
Output
3 2 6 1 5 9 4 8 7
I have modified your code to achieve the needed output.
for (int s = N - 1; s > -1; s--) {
for (int i = s; i < N; i++) {
System.out.print(mat[i - s][i] + " ");
}
System.out.println();
}
for (int s = 1; s < N; s++) {
for (int i = s; i < N; i++) {
System.out.print(mat[i][i - s] + " ");
}
System.out.println();
}

Printing pyramid of numbers

I have homework to make a triangle that looks like this:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
I have been able to create almost half the triangle with the following code:
public static void main(String[] args) {
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}
}
I have been unable to figure out how to mirror the other half of the triangle with my code to look like the triangle above. The instructor hinted that using the for loop with the tab return \t is the way to do this.
Like #Maroun's answer but simpler
int size = 6;
for (int i = 1; i <= size; i++) {
for (int j = i; j < size; j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}
or
int size = 6;
for (int i = 1; i <= size; i++) {
for (int j = i - size + 1; j <= i; j++)
System.out.print((j > 0 ? j : "") + " ");
System.out.println();
}
prints
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
You can try this one:
int x = 0;
for(int i = 1; i <= 5; i++) {
for(int k = x; k < 4; k++) {
System.out.print(" ");
}
x++; //less spaces will be printed in the next iteration
for(int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
I wrote another loop that begins with 4 spaces and then the amount of spaces is reduced.
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Tip: Always open { for your loops and conditions, even for one operation, this might prevent bugs.

Removing a blank line of text for loop Java

I am trying to print some timestables using a nested for loop, I got it to work but I have an extra blank line, I was able to remove it using an if statement but I want to know if there is a better way to do this. Output needs to look like this.
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
static void timesTables(){
for (int i = 1; i <= 2 ; i++){
for (int j = 1; j <= 5; j++){
int output = i * j;
System.out.print(output + " ");
}
}
}
If you want that output you can add and extra println
static void timesTables(){
for (int i = 1; i <= 5 ; i++){
for (int j = 1; j <= 5; j++){
int output = i * j;
System.out.print(output + (j < 5)? " ": "");
}
System.out.println();
}
you can add an extra println with a if condition for your desired output. At the 5th iteration of outer for loop, extra blank line would not be printed.
static void timesTables(){
for (int i = 1; i <= 5 ; i++){
for (int j = 1; j <= 5; j++){
int output = i * j;
System.out.print(output + " ");
}
if(i<5)
System.out.println();
}
Adding on to what others have been writing:
static void timesTables()
{
int numRows = 5;
int numCols = 5;
for ( int i = 1; i <= numRows; i++ )
{
for ( int j = 1; j <= numCols; j++ )
{
int output = i * j;
System.out.print( output + ( j < numCols )? " ": "" );
}
if( i < numRows )
{
System.out.println();
}
}
}

Categories