Backward and Forward Asterisks

Hi, Have a happy Sunday. Today I'm going to explain, how to deal with asterisks problems.

Just before the day, I've been attend the interview.

Question:
To print out Asterisk (*) in system console in specific order.
*****
****
***
**
*

This is backward asterisks, I'm failed to do that, because of less time, no test cases and unfortunate system freezes. Yeah, this is not accept by recruiters. It's very simple problem, I know.

Anyway 15 minutes passes by. No clues.

I'm going to give the answer by two ways here now.
1. Backward Asterisks
2. Forward Asterisks

Solution:

public class Asterisks{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the number of rows : ");
        int n = scan.nextInt();
        System.out.println("Backward Asterisks");
        for (int i = n; i >=0; i--) {
            for (int j = 0; j < i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        System.out.println("Forward Asterisks");
        for(int i= 0;i<n;i++){
            for(int j=0; j<i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Comments

Popular posts from this blog

Clear the app data programmatically in android

convert an integer to a string in java

Print the large string values in android log cat.