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();
}
}
}
Related
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 !
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");
}
**This is my code that look a way to find a sum in an array **
public class Piecedemonei {
public static void recherche(int[] tab) {
int num;
int quo;
for (int i = 0; i <= tab.length - 1; i++) {
int somme = 18;
System.out.println("Solution " + i);
for (int j = i; j < tab.length; j++){
if (tab[j] <= somme) {
num = somme / tab[j];
System.out.print(num+"*" + " " + tab[j]);
System.out.println(" ");
somme -= num * tab[j];
j=0;
}
}
}
}
public static void main(String[] args) {
int aba[] = { 7, 6, 4, 5 };
recherche(aba);
System.out.println();
}
}
Output
Solution 0
2 7 + 1 4
Solution 1
Solution 2
Solution 3
**I am looking a way to improve my code so that the output looks like this : **
Solution 0
2*7 + 1*4
Solution 1
3*6
Solution 2
4*4 + 1*4
Solution 3
**Why cant I reset my loop so that it does the same thing again and again ? **
Change
int somme = 18;
for (int i = 0; i <= tab.length - 1; i++) {
System.out.println("Solution " + i);
for (int j = 0; j < tab.length; j++){
...
to
for (int i = 0; i <= tab.length - 1; i++) {
int somme = 18;
System.out.println("Solution " + i);
for (int j = i; j < tab.length; j++) {
...
Notice the int j = i in the inner loop.
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.
My professor wants me to print out the matrices side by side with the "+" between the two matrices and then a "=" sign. In the end he wants us to add the matrices together.
This is the work so far.
So the result would come out as:
1 2 3 9 8 7 10 10 10
4 5 6 + 6 5 4 = 10 10 10
7 8 9 3 2 1 10 10 10
enter code here public static void main(String[] args) {
int matrix1[][] = {{1,2,3},{4,5,6},{6,7,8}};
int matrix2[][] = {{9,8,7},{6,5,4},{3,2,1}};
int result1;
int[][] result2 = new int[2][3];
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[0].length; j++) {
System.out.printf(matrix1[i][j] + " ");
System.out.print("");
}
System.out.println("");
}
for (int i = 0; i < matrix2.length; i++) {
for (int j = 0; j < matrix2[0].length; j++) {
System.out.printf(matrix2[i][j] + " ");
}
System.out.println("");
}
}
My problem is, how could I print it side by side with the solutions?
Consider the two printing loops for your matrices:
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[0].length; j++) {
System.out.printf(matrix1[i][j] + " ");
}
System.out.println("");
}
for (int i = 0; i < matrix2.length; i++) {
for (int j = 0; j < matrix2[0].length; j++) {
System.out.printf(matrix2[i][j] + " ");
}
System.out.println("");
}
They print matrix 1, then 2 - and so the matrices will be below each other.
If you want the matrices side by side, you need to print line 1 of every matrix, then - after a new line - line 2 of every matrix, etc. By re-arranging how the loops go through the matrices, you could have your new layout.
You unfortunately cannot print them one at a time, you need to take it row by row. This solution requires both matrix1 and matrix2 to be of equal height. But here's a template that should get you started.
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[i].length; j++) {
}
if (i == matrix1/2) {
} else { //One part of if handles when "+" is needed, other one doesn't
}
for (int j = 0; j < matrix2[i].length; j++) {
}
if (i == matrix1/2) {
}
for (int j = 0; j < ???; j++) {
}
}