Printing pyramid of numbers - java

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.

Related

Java Number Pyramid

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 !

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");
}

Struggling with nested for loops

Alright so, I'm new to nested for loops adn I'm having a bit of an issue on understanding them. I've read many guides, but I still don't fully understand.
Alright the prompt:
Write nested for loops that produce the following output:
000111222333444555666777888999
000111222333444555666777888999
000111222333444555666777888999
What I have so far
for(int num2 = 0; num2 <= 9; num2++) {
for(int num1 = 0; num1 <= 2; num1++) {
System.out.println(num2 + " " + num2 + " " + num2);
}
}
And the output is
0 0 0
0 0 0
0 0 0
1 1 1
1 1 1
1 1 1
2 2 2
2 2 2
2 2 2
3 3 3
3 3 3
3 3 3
4 4 4
4 4 4
4 4 4
5 5 5
5 5 5
5 5 5
6 6 6
6 6 6
6 6 6
7 7 7
7 7 7
7 7 7
8 8 8
8 8 8
8 8 8
9 9 9
9 9 9
9 9 9
What am I doing wrong?
You got 3 copies of each number.
the outer loop:
for (int i = 0; i < 10; i++) {
chooses which number you want to print so that is fine.
The inner loop however is comparing j against the chosen number. You want 3 copies, not a variable number of copies. This change will make 3 copies:
for (int j = 0; j < 3; j++) {
You also don't need this:
System.out.println(i);
EDIT: I just noticed you need 3 of these outputs.
add an outer loop:
for (int x = 0; x < 3; x++) {
and a blank space
System.out.println(" ");
So the final result should be:
for (int x = 0; x < 3; j++) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(i);
}
}
System.out.println(" ");
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(j + "" + j + "" + j);
}
System.out.println();
}
In the program provided by you the following events take place:-
In first loop variable i is initiated, condition for loop is checked and then it moves to the second loop if condition is true.
Now second loop iterates over the value of j until the condition is false and then control returns to the first loop.
Try to follow the working of loop and you can see yourself where were you wrong.
Try this:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 3; k++) {
System.out.print(j);
}
}
System.out.println("");
}
loop 1: You want the sequence 3 times, each occurrence on its own line.
loop 2: You want the sequence to have digits 0 through 9 ascendingly.
loop 3: You want the sequence to have each digit in succession 3 times.
for (int k = 0; k<3, k++){
for (int i = 0; i< 10; i++) {
for (int j = 0; j < 3;j++) {
System.out.println(i);
}
}
System.out.println("")};
}
}
for(int k=0;k<3;k++) {
for (int i = 0; i< 10; i++) {
for (int j = 0; j < 3;j++) {
System.out.println(i);
}
}
}
Although I am by far most inexperienced guy here, I think this should give exact output you're looking for.

Nested For Loop Pattern

I've been struggling with this for loop pattern in Java(with spaces on both sides)
0
000
00000
0000000
00000
000
0
Heres what I have been doing:
for (int i = 1; i <= 5; i++) {
for (int s = 5; s > i; s--) {
System.out.print(" ");
}
for (int j = 1; j < i; j++) {
System.out.print("0");
}
for (int j = 1; j < i; j++) {
System.out.print("0");
}
System.out.println("");
}
for (int i = 1; i <= 5; i++) {
for (int s = 1; s < i; s++) {
System.out.print(" ");
}
for (int j = 5; j > i; j--) {
System.out.print("0");
}
for (int j = 5; j > i; j--) {
System.out.print("0");
}
System.out.println("");
}
As you can probably tell, I've been able to figure out how to print this pattern using an odd number of 0's in each row, but I can't for the life of me figure out how to do it with odd numbers. Thanks.
Sometimes it helps to parameterize the problem a bit; then you can write it down on paper and see if there are any patterns. For example, in your desired output:
0
000
00000
0000000
00000
000
0
First, let's pick a convenient way to describe a row. Let's say a row i is s spaces followed by z zeros, and the whole pattern can be rows rows long. Now write it out. When rows is 7:
i s z
0 3 1
1 2 3
2 1 5
3 0 7
4 1 5
5 2 3
6 3 1
How about if rows is, say, 5:
i s z
0 2 1
1 1 3
2 0 5
3 1 3
4 2 1
What about even numbers? We have to pick how we want it to look, how about:
00
0000
000000
000000
0000
00
So, when rows is 6:
i s z
0 2 2
1 1 4
2 0 6
3 0 6
4 1 4
5 2 2
Ok, so now, let's look for patterns! z is easy: We can see, both from the numbers and visually that z is a function of s and rows:
z = rows - (2 * s);
That is, we know the total width is equal to the height (rows), and we know there are the same number of spaces before and after the zeroes.
So now it boils down to figuring out s from i and rows. Well, after a bit of head scratching and experimenting, we can see that when i < rows / 2:
s = (rows - 1) / 2 - i;
And when i >= rows / 2:
s = i - rows / 2;
And now, it all comes together, e.g.:
void diamond (int rows) {
for (int i = 0; i < rows; ++ i) {
int s;
if (i < rows / 2)
s = (rows - 1) / 2 - i;
else
s = i - rows / 2;
int z = rows - (2 * s);
// print s spaces, then z zeroes, then a newline
}
}
I'll leave the task of printing s spaces and z zeroes as an exercise to the reader.
This is a fairly general problem solving technique for this type of problem:
Parameterize.
Write it down.
Look for patterns.
Step 1 is the most important step, as it defines a way to convert your complex-looking task (drawing a diamond) into a much simpler problem (determining s as a function of i and rows).
you can try this -
for (int i = 1; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
}
try this,
public static void main(String []args){
int i = -1;
for (int j = 0; j < 7; j +=2) {
i++;
for (int k = i; k < 3; k++)
System.out.print(" ");
for (int z = 0; z <= j; z++) {
System.out.print(0);
}
System.out.println();
}
i=0;
for(int j=4; j>=0;j-=2){
i++;
for(int k=0;k<i;k++)
System.out.print(" ");
for(int z=0;z<=j ;z++)
System.out.print(0);
System.out.println();
}
}
How about just changing the first j=1 to j=2 and changing the first j=5 to j=4, so that you get one less zero from each of those loops?

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