1

Hey I' m trying to create a pattern that's suppose to output multiples of 5 in a pattern like this:

5 10 15 20 25
  30 40 45 50
     55 60 65
        70 75
           80

but my output is like this

5 10 15 20 25                                                                                                                                 
 30 35 40 45                                                                                                                                  
  50 55 60                                                                                                                                    
   65 70                                                                                                                                      
    75 

but when i put asterisks in the print :

*****                                                                                                                                         
 ****                                                                                                                                         
  ***                                                                                                                                         
   **                                                                                                                                         
    *

here's my code:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) {

    int n = 5;
    int x =5;

    for(int i = n; i>=1; i--){

        for(int j = n-1; j>=i; j--){
            System.out.print(" ");
        }
        for(int k = 1; k<=i; k++){
            System.out.print(x+" ");
            x+=5;

        }

        System.out.println();
    }


    }
}

can somebody help me? i spent almost 3 hours trying to figure this out. any help would be appreciated. thanks!

1
  • 1
    I think your output is missing 35 Commented Nov 18, 2019 at 15:08

4 Answers 4

2

The cleanest way to achieve what you want is to do is by using printf as shown below:

public class Main {
    public static void main(String[] args) {
        int n = 5;
        int x = 5;

        for (int i = n; i >= 1; i--) {
            for(int j=1;j<n-i+1;j++)
                System.out.printf("%3s"," ");
            for (int k = 1; k <= i; k++) {
                System.out.printf("%3d",x);
                x += 5;
            }
            System.out.println();
        }
    }
}

Output:

  5 10 15 20 25
    30 35 40 45
       50 55 60
          65 70
             75

Update:

If you are not comfortable with printf, you can use the following solution:

public class Main {
    public static void main(String[] args) {
        int n = 5;
        int x = 5;
        String space=" ";
        for (int i = n; i >= 1; i--) {
            for(int j=1;j<(n-i)*3;j++)
                System.out.print(space);
            for (int k = 1; k <= i; k++) {
                System.out.print(x+space);
                x += 5;
            }
            System.out.println();
        }
    }
}

Output:

5 10 15 20 25 
  30 35 40 45 
     50 55 60 
        65 70 
           75 
Sign up to request clarification or add additional context in comments.

1 Comment

%4s specifies a length of 4 characters within which the given string will be printed. %4d specifies a length of 4 digits within which the given integer will be printed. I recommend you go through this tutorial.
2

You write three characters at a time (space, first digit, second digit), so you need to print three spaces to have the correct results.

Basically your same code, but with three spaces int the System.out.print:

for(int i = n; i>=1; i--){

    //This is the same loop, but written in a different way.
    for(int j = 0; i < n-i; j++){
        System.out.print("   "); //THREE SPACES HERE
    }
    for(int k = 1; k<=i; k++){
        System.out.print(x+" ");
        x+=5;

    }

    System.out.println();
}

Now, you are also printing the 5 at the start, which is one digit long, so you need another space before. So you need a conditional structure in order to do this:

//This is the same loop, but written in a different way.
for(int j = 0; i < n-i; j++){
    if(j == 0 && x <= 5) //First cycle (remember 5, which is 1 digit)
        System.out.print("  "); //TWO SPACES HERE
    else
        System.out.print("   "); //THREE SPACES HERE
}

Note that this only works with 2 digits numbers so you need to do a similar thing like the one we used with the number five if you're gonna use also 3 or more digits numbers.

Comments

1

I think this exercise is also a good moment to practice with System.out.printf()

Here is how i would do this:



public static void main(String[] args) {                   
    int x = 5;                                             
    int n = 35;                                            
    int max = calculateTotalNumbers(n) * x;  // calculate the highest number              
    int maxLenght = String.valueOf(max).length(); //calculate the numbers of digits in the highest number         

    for(int i = n; i>=1; i--){                             
        for (int j = 0; j < n - i; j++) {      
            //makes sure you will have the same length spaces everywhere            
            System.out.printf("%1$"+maxLenght+"s ", "");   
        }                                                  
        for (int k = 1; k <= i; k++) { 
            //this formatting will ensure every digit has the same length with padding included. Check out the javadoc of System.out.printf();                     
            System.out.printf("%" + maxLenght + "d ", x);  
            x += 5;                                        

        }                                                  
        System.out.println();                              
    }                                                      
}                                                          

// this method will calculate the total number of numbers you will print in the triangle
private static int calculateTotalNumbers(int n) {
    if (n <= 0) return 0;
    else return (n + calculateTotalNumbers(n -1));
}

Comments

-1
public class temp {
    public static void main(String[] args) {
        int n = 5;
        int p = 1;
        for (int i = 0; i < n ; i ++){
            String curr = "";
            for (int j = 0 ; j < n - (n - i); j ++){
                curr += "  ";
                if (j > 0) {
                    curr += " ";
                }
            }
            for (int j = i; j < n; j ++){
                curr += Integer.toString(n * p);
                curr += " ";
                p += 1;
            }
            System.out.println(curr);
        }

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.