Question

In: Computer Science

Write a Java program to print the pattern of asterisks shown below. For i=1 * For...

Write a Java program to print the pattern of asterisks shown below.

For i=1

*

For i=2

*

* *

For i=3

*

**

* * *

For i=n

*

* *

* * *

… … …

* * * * * * ……… n

Solutions

Expert Solution

This question can be completed in many ways like using recursion or by using loop etc.

Completing answer using recursion

Code: (Text FIle Also Included In End)


Output:


Code:

// Pattern Class
public class Pattern {

    // Main Method calling printPattern Method and Printing
    public static void main(String[] args) {
        /*
        * Here 1 tells that this is first turn of recurssion
        * and n is maximum number of recurssion required
         */
        printPattern(1, 10);
    }

    // Print Pattern Method
    private static void printPattern(int recurssionTurn, int n) {

        // Loop for Print *
        for (int i = 0; i < recurssionTurn; i++) {
            System.out.print("*");
        }
        System.out.println("");         // Break Line
        recurssionTurn++;              // Increasing Recurssing Turn
        if (recurssionTurn <= n) {        // If Recurssion has not reached target, call Again
            printPattern(recurssionTurn, n);
        }

    }

}


Related Solutions

I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
Java Programming Write a program that displays the following pattern *                         *       &nbsp
Java Programming Write a program that displays the following pattern *                         *          *          * *          *          *          *          *          *          *          *          *          *          *          *             *          *          *          *          *                         *          *          *                                     * Printing Pattern A * ** *** **** ***** ****** ******* Printing Pattern B ******* ****** ***** **** *** ** * Printing Pattern C * ** *** **** ***** ****** *******
Exercise 1. Write a function named printBox to print a 5 x 10 box of asterisks...
Exercise 1. Write a function named printBox to print a 5 x 10 box of asterisks using nested loops. When printBox() is called your function should display the pattern shown below. Write a function named printBox to print a 5 x 10 box of asterisks using nested loops. When printBox() is called your function should display the following information. ********** ********** ********** ********** ********** """ # place the code for the printBox function here printBox( numRows, numCols ) Exercise 2....
1: Answer these questions: (a) Write a Java program to print whole numbers between 1 to...
1: Answer these questions: (a) Write a Java program to print whole numbers between 1 to 1000 which are divisible by 3, 5 and by both numbers. (b) Differentiate between instance and local variable in Java (c) In a tabular form, differentiate between instance and class variable
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
JAVA Write nested while loop that will print out this pattern, based upon a number entered...
JAVA Write nested while loop that will print out this pattern, based upon a number entered by the user. User enters 4: 1234 1234 1234 1234 User enters 2: 12 12
Write a java program that will take a line of input and go through and print...
Write a java program that will take a line of input and go through and print out that line again with all the word numbers swapped with their corresponding numeric representations (only deal with numbers from one to nine). Sample runs might look like this: Please enter a line of input to process: My four Grandparents had five grandchildren My 4 grandparents had 5 grandchildren without array and methods.
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
In java coding, write a program that will print out a representation of a “W” using...
In java coding, write a program that will print out a representation of a “W” using a character that a user provides.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT