I wanna ask you something about my code. I want to display output as below:
2 3 4 1
6 7 8 5
10 11 12 9
14 15 16 13
But the output what is shown:
2 3 4 1
6 3 0 -3
2 -1 -4 -7
-2 -5 -8-11
Here is my current code:
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
System.out.printf("%3d", number);
if(j==2){
plus=-3;
}
number+=plus;
}
number+=8;
System.out.println("");
}
Can you tell me what's wrong with it? Thank you
You need to reset plus to 1 at the end of each iteration of the outer loop. See the below code in action here.
int number = 2;
int plus = 1;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.printf("%3d", number);
if (j == 2) {
plus = -3;
}
number += plus;
}
number += 8;
plus = 1;
System.out.println("");
}
Related
An empty 2D array a should be filled with the following values:
1 8 9 16 17
2 7 10 15 18
3 6 11 14 19
4 5 12 13 20
I've been having a lot of trouble with figuring out how to reverse the order of a column. This is the closest I've gotten:
int [][] a = new int[4][5];
int count = 1;
for(int c = 0; c < a[0].length; c++) {
for(int r = 0; r < a.length; r++) {
a[r][c] = count;
count++;
if(r% 2 == 0 && c % 2 != 0) {
count = 20;
a[r][c] = 20;
count--;
}
}
}
You should define a variable that defines the direction you want to move at each iteration, i've named it sign. if sign is positive, column will be filled in a downward manner, otherwise it would move in the opposite direction.
int [][] a = new int[4][5];
int count = 1;
int sign = 1;
for(int j = 0 ; j < 5 ; j++){
if(sign==1)
for(int i = 0 ; i < 4 ; i++){
a[i][j]=count;
count++;
}
else
for(int i = 3 ; i >=0 ; i--){
a[i][j]=count;
count++;
}
sign *= -1;
}
If we want to print the array we'll have :
for(int i = 0 ; i < 4; i++){
for(int j = 0 ; j < 5; j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
Resulting output would be :
1 8 9 16 17
2 7 10 15 18
3 6 11 14 19
4 5 12 13 20
you are on the right track, just reverse the row when inserting even numbered columns.
public static void main(String []args){
int count =1;
int columnCount =5;
int rowCount = 4;
int [][] a = new int[rowCount][columnCount];
for (int column = 0; column<columnCount; column ++ ) {
for (int row = 0; row <rowCount; row ++) {
if(column%2==0){
a[row][column] = count;
}else {
a[rowCount-row-1][column] =count;
}
count ++ ;
}
}
//validate results
for (int row = 0; row <rowCount; row ++) {
for (int column = 0; column<columnCount; column ++ ) {
System.out.print (a[row][column] +" ");
}
System.out.println();
}
}
this will give you the following results
$java -Xmx128M -Xms16M HelloWorld
1 8 9 16 17
2 7 10 15 18
3 6 11 14 19
4 5 12 13 20
I'm trying to write a program that accepts an integer from 5 to 9 from the user. If the user enters 6, the output to the user will be:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
6 5 4 3 2 1
6 5 4 3 2
6 5 4 3
6 5 4
6 5
6
I tried using a loop like:
for(int i = 0; i < no; i++)
{
for(int j = 0; j < no; j++)
{
System.out.print(no);
}
System.out.println();
}
But the output I got was:
666666
666666
666666
666666
666666
import java.util.*;
public class numberSequence
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number in the range 5 to 9: ");
int no = Integer.parseInt(in.nextLine());
if(no < 5 || no > 9)
System.out.println("Please enter a number in the range 5 to 9.");
else
{
// continuing from here
}
}
}
Loop would be -
for(int i = 1; i <= no; i++){
for(int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
for(int i = 1; i <= no; i++){
for(int j = no; j >= i; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
Input - no = 6
Output -
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
6 5 4 3 2 1
6 5 4 3 2
6 5 4 3
6 5 4
6 5
6
You need to use 2 for loops, the first one for increasing until value of n, the 2nd one for decreasing from value n to 1.
import java.util.Scanner;
public class numberSequence
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number in the range 5 to 9: ");
int no = Integer.parseInt(in.nextLine());
if(no < 5 || no > 9)
System.out.println("Please enter a number in the range 5 to 9.");
else
{
for(int i = 1; i <=no; i++)
{
for(int j = 1; j <=i; j++)
{
System.out.print(j);
}
System.out.println();
}
for(int i = no; i > 0; i--)
{
for(int j = i; j >=1; j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
}
Output:
1
12
123
1234
12345
123456
654321
54321
4321
321
21
1
You can try this
for (int i = 1; i <= no; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println("");
}
for (int i = no; i >= 1; i--) {
for (int j = i; j >= 1; j--) {
System.out.print(j);
}
System.out.println("");
}
you are printing "no" , you should print "j".
program print desire output is
public class Simple {
public static void main(String[] args) {
int no= 6;
for(int i = 1; i <= no; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(j);
}
System.out.println();
}
for(int i = no; i >= 1; i--)
{
for(int j = i; j >= 1; j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
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.
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?
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.