Question

Write a program that prompts the user for a positive integer, n, and then prints a...

Write a program that prompts the user for a positive integer, n, and then prints a following shape of stars using nested for loop, System.out.print(“*”);, and System.out.println();. Example1:

Enter a positive integer: 3

*

* *

* * *

* *

*



Example2:

Enter a positive integer: 5

*

* *

* * *

* * * *

* * * * *

* * * *

* * *

* *

*

JAVA LANGUAGE!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class NestedLoopsPattern {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a positive integer: ");
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        for (int i = n-1; i >= 1; i--) {
            for (int j = 0; j < i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        in.close();
    }
}

Add a comment
Know the answer?
Add Answer to:
Write a program that prompts the user for a positive integer, n, and then prints a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that prompts the user to enter an integer, n , and then n...

    Write a program that prompts the user to enter an integer, n , and then n floating point numbers. As the numbers are read, the program will calculate the average of the negative numbers. In C language.

  • Write a program that reads an integer greater or equal to 2, n, and prints a...

    Write a program that reads an integer greater or equal to 2, n, and prints a shape of a nline hollow inverted pyramid of stars. Your program should interact with the user exactly as it shows in the following two executions: Execution example 1: Please enter an integer, greater or equal to 2: 5 ********* -*----- * --*--- * ---*--* ----* Execution example 2: Please enter an integer, greater or equal to 2: 3 ***** -* * --*

  • Write a program that prompts the user for positive integers, only stopping when a negative integer...

    Write a program that prompts the user for positive integers, only stopping when a negative integer or zero is given. The program should then print out how many of the positive integers were odd. Example: Enter a positive integer (0 or negative to stop): 9 Enter a positive integer (0 or negative to stop): 4 Enter a positive integer (0 or negative to stop): 7 Enter a positive integer (0 or negative to stop): -3 You entered 2 odd integers....

  • Write the following code in C. Write a program that takes a positive integer and prints...

    Write the following code in C. Write a program that takes a positive integer and prints that many stars These are sample outputs. Enter the number of stars: 9 Enter the number of stars: 4 Enter the number of stars: -3 Invalid input!

  • Write a program that receives a positive integer n from the user and then calculates the...

    Write a program that receives a positive integer n from the user and then calculates the sum below by using a loop. Your program needs to ask the user to re-enter a different number if the user enters a non-positive one. Display the result. 1 SUM 1 2 3 ... n x x n = = = + + + +

  • write in C++ Exercise#1: Is it a Prime? Write a program that prompts the user to...

    write in C++ Exercise#1: Is it a Prime? Write a program that prompts the user to enter a positive integer in the range [100, 100000]. If the integer is not in the specified range the program prompts the user again. The program prints whether the integer is a prime number or not. Sample input/output nter an integer in the range [10e, 10eeee]: 39 nter an integer in the range [100, 100000]: 120453 Enter an integer in the range [10e, 10e000e]:...

  • java programe Write a program that prompts the user to enter in an integer number representing...

    java programe Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from...

  • Write a program which prompts the user to enter a number of rows, and prints a...

    Write a program which prompts the user to enter a number of rows, and prints a hollow square star pattern with 2 diagonals. Note: you can assume that the integer will always be > 1 and will be an odd number. For example: Input Result 5 Enter number of rows: 5 ***** ** ** * * * ** ** ***** 9 Enter number of rows: 9 ** ** ** * * * * * * * * * * **...

  • A java program Write a program that prompts for and reads in a positive    integer...

    A java program Write a program that prompts for and reads in a positive    integer into a variable n. Your program should then sum    the first n ODD integers and display the sum. For    example, if 3 is entered for n, your program should display    the the sum 1 + 3 + 5. If 5 is entered, your program should    display the sum 1 + 3 + 5 + 7 + 9. What is the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT