Skip to main content

Java : Most Frequently Asked Star Patterns in Coding Interview

Today we are you going to learn some coding tricks to show the patterns on console using stars (*). Let jump into the sections.
1. Christmas Tree : 
public class ChristmasTree {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
        System.out.println("Christmas tree drawing program.");
        System.out.print("Enter the height: ");
        int n = sc.nextInt();
        System.out.print("Enter the levels: ");
        int level = sc.nextInt();
if (n < 1) {
System.out.println("Height must be positive number.");
} else if (level < 1) {
System.out.println("Number of levels must be positive");
} else {
for (int i = 0; i < level; i++) {
printStars(n);
}
}
        sc.close();
    }
private static void printStars(int height) {
for (int j = 0; j < height; j++) { // Number of rows!!!
for (int k = j; k < height - 1; k++) { // For printing the spaces!!!
System.out.print(" ");
}
for (int m = 0; m <= (j * 2); m++) { // Printing the stars!!!
System.out.print("*");
}
System.out.println();
}
}
Output: Christmas tree drawing program.
Enter the height: 10
Enter the levels: 3

2. Rectangle :
public static void rectangle(){
int number = 7;
        for (int i = 0; i < number; i++) {
            if (i == 0 || i == 6) {
                for (int j = 0; j < number; j++) {
                    System.out.print("*");
                }
                System.out.println();
            }
            if (i >= 1 && i <= 5) {
                for (int j = 0; j < number; j++) {
                    if (j == 0 || j == 6) {
                        System.out.print("*");
                    } else if (j >= 1 && j <= 5) {
                        System.out.print(" ");
                    }
                }
                System.out.println();
            }
        }
}

3. W- Using *:
public class WPattern{
private static void stars(int count) {
        for (int i = 0; i < count; ++i)
            System.out.print("*");
    }
    private static void spaces(int count) {
        for (int i = 0; i < count; ++i)
            System.out.print(" ");
    }
    public static void main(String[] args) {
        int n = 8;
        for (int i = 0; i < n; ++i) {
            stars(i + 1);
            spaces(n - i - 1);
            stars(n - i + 1);
            spaces(2 * i);
            stars(n - i);
            spaces(n - i - 1);
            stars(i + 1);
            System.out.println();
        }
    }
}

Comments

Popular posts from this blog

Who is Peter Lynch and what is his philosophy in equity market investment? 25 Golden Rules of the most successful Fund Manager.

Peter Lynch (born January 19, 1944) is an American investor, mutual fund manager, and philanthropist. As the manager of the Magellan Fund at Fidelity Investments between 1977 and 1990, Lynch averaged a 29.2% annual return, consistently more than doubling the S&P 500 stock market index and making it the best-performing mutual fund in the world. During his 13 year tenure, assets under management increased from $18 million to $14 billion. He also co-authored a number of books and papers on investing and coined a number of well known mantras of modern individual investing strategies, such as Invest in what you know and ten bagger. Lynch is consistently described as a "legend" by the financial media for his performance record. Base on his career I have compiled his investing rules here. 25 GOLDEN RULES by @Peter Lynch 1: Nobody can predict interest rates, the future direction of the economy or the stock market. Dismiss all such forecasts & concentrate on what's actual

What is version checking in Hibernate ?

Version checking used in hibernate when more then one thread trying to access same data. For example : User A edit the row of the TABLE for update ( In the User Interface changing data This is user thinking time) and in the same time User B edit the same record for update and click the update. Then User A click the Update and update done. Change made by user B is gone. In hibernate you can prevent slate object updation using version checking. Check the version of the row when you are updating the row. Get the version of the row when you are fetching the row of the TABLE for update. On the time of updation just fetch the version number and match with your version number (on the time of fetching).

What are JEE Containers? What are their different types?

Containers are the interface between a component and the low-level, platform-specific functionality that supports the component. Before it can be executed, a web, enterprise bean, or application client component must be assembled into a Java EE module and deployed into its container. The assembly process involves specifying container settings for each component in the Java EE application and for the Java EE application itself. Container settings customize the underlying support provided by the Java EE server, including such services as security, transaction management, Java Naming and Directory Interface (JNDI) API lookups, and remote connectivity. Here are some of the highlights. The Java EE security model lets you configure a web component or enterprise bean so that system resources are accessed only by authorized users. The Java EE transaction model lets you specify relationships among methods that make up a single transaction so that all methods in one transaction are treated as a