Here's my code for Ganesha Pattern. Can you please tell whats wrong with it

import java.util.*;

class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int row = 1, col;
if (n % 2 != 0 && (n >= 5 && n <= 99)) {
while (row <= n) {
col = 1;
while (col <= n) {
if ((row == 1) && (col == 1 || col >= n / 2 + 1)) {
System.out.print("* “);
} else if ((row == n) && (col == n || col <= n / 2 + 1)) {
System.out.print(”* “);
} else if ((col == 1) && (row == 1 || row <= n / 2 + 1)) {
System.out.print(”* “);
} else if ((col == n) && (row == n || row >= n / 2 + 1)) {
System.out.print(”* “);
} else if (row == n / 2 + 1 || col == n / 2 + 1) {
System.out.print(”* “);
} else {
System.out.print(” “);
}
col++;
}
row++;
System.out.println(”");
}
}

}

}

@devfest2021_ca0dcbd15233fafbThe code that you have sent is not in readable form.
The only mistake that I can gather is that when you have given spaces using System.out.print(" ") Give two spaces here instead of one. Try uploading the code on https://ide.codingblocks.com then goto file click save then send the link here.Remember try to send your code in readable form.

import java.util.*;

class ganeshaPattern {
public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int row = 1, col;
    if (n % 2 != 0 && (n >= 5 && n <= 99)) {
        while (row <= n) {
            col = 1;
            while (col <= n) {
                if ((row == 1) && (col == 1 || col >= n / 2 + 1)) {
                    System.out.print("* ");
                } else if ((row == n) && (col == n || col <= n / 2 + 1)) {
                    System.out.print("* ");
                } else if ((col == 1) && (row == 1 || row <= n / 2 + 1)) {
                    System.out.print("* ");
                } else if ((col == n) && (row == n || row >= n / 2 + 1)) {
                    System.out.print("* ");
                } else if (row == n / 2 + 1 || col == n / 2 + 1) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
                col++;
            }
            row++;
            System.out.println("");
        }
    }

}
}

@devfest2021_ca0dcbd15233fafb You just need to change spaces after printing a star i.e. just print start only without any space and when you print spaces just print single space not 2 spaces. modified code is below :

import java.util.*;

class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int row = 1, col;
        if (n % 2 != 0 && (n >= 5 && n <= 99)) {
            while (row <= n) {
                col = 1;
                while (col <= n) {
                    if ((row == 1) && (col == 1 || col >= n / 2 + 1)) {
						//just print stars not space
                        System.out.print("*");
                    } else if ((row == n) && (col == n || col <= n / 2 + 1)) {
						//just print stars not space
                        System.out.print("*");
                    } else if ((col == 1) && (row == 1 || row <= n / 2 + 1)) {
						//just print stars not space
                        System.out.print("*");
                    } else if ((col == n) && (row == n || row >= n / 2 + 1)) {
						//just print stars not space
                        System.out.print("*");
                    } else if (row == n / 2 + 1 || col == n / 2 + 1) {
						//just print stars not space
                        System.out.print("*");
                    } else {

						//Print single space not double
                        System.out.print(" ");
                    }
                    col++;
                }
                row++;
                System.out.println("");
            }
        }

    }
}
1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.