0

Here is the question. I have to print this pattern eg For n = 5

For n = 5 
****1
***232
**34543
*4567654
567898765

I have written logic for this problem but I am not able to solve it. I can't make the last pattern print i.e. the decrease one 2 4,3 5,4

Here's my pattern For n = 5
****1
***232
**34544
*4567666
567898888    

Can anyone help me out and tell what's wrong with my logic. How to fix it

My code down below

import java.util.Scanner;
import java.lang.*;
public class Main {
    public static void main(String[] args) {

        solve();
    }





    public static void solve(){
        int n;
        Scanner obj = new Scanner(System.in);
        n = obj.nextInt();

        for(int row =1;row<=n;row++){
            int  ans1 =  row;
            int spaces =1;
            
            for( spaces = 1;spaces<=n-row;spaces++){
                System.out.print("*");

            }
            for (int pattern01 = 1; pattern01<=row;pattern01++){
                System.out.print(ans1);
                ans1 = ans1 +1;
            }

            for ( int pattern2 = 1 ; pattern2<=row-1; pattern2++){
                ans1= 2*row-2;
                System.out.print(ans1);
                ans1--;


            }

        System.out.println();

        }

    }

}
4
  • Step through your code with the debugger. Commented Oct 10, 2022 at 2:46
  • 3
    Your problem is the line ans1= 2*row-2; - I don't think you want to do that every time you're in that last loop. Commented Oct 10, 2022 at 2:54
  • @DawoodibnKareem edit this is the problem. I want to decrease it value by 1. I know my logic is wrong. Is there any way to achieve this for eg row =3 ans1= 2*row-2; I decrease it value by 1 but it does not work ` Commented Oct 10, 2022 at 3:41
  • 2
    If you want it to decrease, then don't keep resetting it back to its previous value. Try moving that line to before the loop. Commented Oct 10, 2022 at 4:04

1 Answer 1

1

The issue you are having is that you are not decrementing 'ans1' correctly. If you are not sure how to use the debugger then observe your output through the console with print-out statements. Specifically the 3rd loop when 'ans1' is supposed to count backwards. Also, keep in mind the value of 'ans1' after you exit the second for-loop.

This is your second loop when you start to count up from row number variable. You increment 'ans1' by 1 each iteration.

for (int pattern01 = 1; pattern01 <= row;pattern01++)   {
   System.out.print(ans1);
   ans1 = ans1 + 1;
}

One thing to consider is that you are incrementing 'ans1' and what is the value of it before you enter your third loop to decrement? You need to do something here before you enter the loop and start printing.

Also, in your third loop you are not decrementing correctly. You should be counting backwards, or rather just decrementing by 1.

for(int pattern2 = 1;pattern2 <= row-1; pattern2++){
   ans1= 2*row-2; // Your focus should be here, does this look right? Do you need it?
   System.out.print(ans1); // You should decrement first and then print
   ans1--; // this is correct, but in the wrong spot
}

I know you can pull it off :) You got this.

Sign up to request clarification or add additional context in comments.

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.