-1

I tried to print the below pattern in java

***1***
**2*2**
*3*3*3*
4*4*4*4

like this way

public class Sample {
    public static void main(String[] args) {
        int m, n;
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the no of rows");
        m = s.nextInt();
        System.out.println("Enter the no of columns");
        n = s.nextInt();
        s.close();
                //Printing the number of rows
        for (int i = 1; i <= m; i++) {
                       //printing number of columns
            for (int j = n; j > 0; j--) {
                                //Printing values in the pattern
                for (int k = 0; k < j; k++) {
                    if (k == j / 2)
                        System.out.print(i);
                }
                System.out.print("*");
            }
            System.out.print("\n");
        }
    }
}

I am facing problem at the logic of finding the positions to print the values in each row. Its asked at my previous interview.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Amar546
  • 73
  • 10
  • 1
    You are attempting to print * instead of @, also what are your inputs, and what does it print out? Any errors? – Farlan Mar 10 '14 at 13:59
  • 1
    Why do you need the number of columns? Isn't the width depending on the rows? – Thomas Mar 10 '14 at 14:00
  • Does task specified that you need to ask for number of row and columns? If not then you shouldn't as user for number of columns, it should be calculated dynamically based on number of rows. – Pshemo Mar 10 '14 at 14:01
  • yes. im attempting to add * i forgot to add the modified one. output is not printing like this its crossing the column size which i've given – Amar546 Mar 10 '14 at 14:01
  • in the above example rows are 4 and columns are 7 like i want to implement user given values – Amar546 Mar 10 '14 at 14:03
  • 2
    I still don't understand why you need the number of columns. Let me ask this if you have m(rows) = 5 for example and n(columns) = 5 How should the output look like? – Thresh Mar 10 '14 at 14:04
  • 1
    @user2168626 I think that Thresh is asking how program should print it. Maybe explain how first row should look like. Is it `****1` `***1*` or `**1**` or `*1***` or maybe `1****`. If first row for 5 rows should always look like `****1****` then don't even ask users for number of columns, because it should depend only on number or rows. – Pshemo Mar 10 '14 at 14:10
  • yeah sadly doesn't appear well in the comments :( – Thresh Mar 10 '14 at 14:13
  • Please modify the question to clear how the output should be for same number of rows and columns – Thresh Mar 10 '14 at 14:18
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Your question can be answered very quickly and easily with your step-debugger. You should always try and solve your problems with a step debugger before coming to StackOverflow. –  May 20 '18 at 14:31

2 Answers2

3

Try to figure out a formula when the asterisk(*) is replaced with number.

  • Hint1: the formula depends on the distance of the given symbol from the middle of the row
  • Hint2: the formula depends on the remainder modulo 2 of the position in the current row(and the number of the row too)

The formula is simple enough if you note the two dependencies I mention above.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

we can write logic based on the output. Right now you are passing the value row = 4 and column =7. For that you have provided output. Based on that I have written this program, which output matching with that, we can modify/tune this program also:

import java.util.Scanner;

public class Sample {
    public static void main(String[] args) {
        int m, n;
       Scanner s = new Scanner(System.in);
       System.out.println("Enter the no of rows");
        m = s.nextInt();
        System.out.println("Enter the no of columns");
        n = s.nextInt();
       s.close();
        String[][] str = new String[m][n];
        int frontPos = 0;
        int backPos = n-1;

        for (int i=0; i<m; i++){
            int l = Math.round((n)/(i+2));


            if(i==(m-1)){
                frontPos = 0;
                backPos = n-1;
            } else {
            frontPos = l;
            backPos = n-1-l;
            }
            //System.out.println("Difference =="+frontPos);
            boolean contFlag = false;


            do{
                //System.out.println("frontPos=="+frontPos+"|backPos=="+backPos);
                if(frontPos == backPos){
                    str[i][frontPos] = new Integer(i+1).toString();
                } else if(frontPos < backPos){
                    str[i][frontPos] = new Integer(i+1).toString();
                    str[i][backPos] = new Integer(i+1).toString();
                }
                if((backPos-frontPos) > l){

                    contFlag = true;
                    frontPos = frontPos + (l+1);
                    backPos = backPos -(l+1);
                } else {
                    contFlag = false;
                }
            } while(contFlag);


            //System.out.print("\n");
        }

        for(int a=0; a<m; a++){
            for(int b=0; b<n; b++){
                if(str[a][b]==null){
                    System.out.print("*");
                } else {
                System.out.print(str[a][b]);
                }
            }
            System.out.print("\n");
        }
    }
}
Richard Miskin
  • 1,260
  • 7
  • 12
s.saroj
  • 34
  • 2