Question

In: Computer Science

need to output this in java Enter an integer: 5 1 2 2 2 3 3...

need to output this in java

Enter an integer: 5

1

2 2 2

3 3 3 3 3

4 4 4 4 4 4 4

5 5 5 5 5 5 5 5 5

4 4 4 4 4 4 4

3 3 3 3 3

2 2 2

1

(in the shape of a diamond)

please help me with the correct code, thanks

Solutions

Expert Solution

import java.util.Scanner;

public class PrintDiamond {

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


Related Solutions

JAVA Programming Implement the class DataProcess and prompt a user to enter 5 integer numbers. Once...
JAVA Programming Implement the class DataProcess and prompt a user to enter 5 integer numbers. Once The program should output the average, largest, and smallest of 5 numbers. You must implement the methods listed below in your program. static float getAverage(int[] data) {...} static int getLargest(int[] data) {...} static int getSmallest(int[] data) {...}
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1 and 100 inclusive. The user will be repeatedly prompted until they input a valid integer.
I need an idea of Java code that will convert an integer (1 to 3,999) into...
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
1. Prove or Disprove: If n is a nonnegative integer, then 5 | (2*4n + 3*9n)
1. Prove or Disprove: If n is a nonnegative integer, then 5 | (2*4n + 3*9n)
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute the sum of the numbers between these two numbers (including the numbers entered). If the user Enters 2 and 6. The program will calculate the sum of the numbers: 2+3+4+5+6 and will display the result. Enter First number> 2 Enter second number> 6 The sum is 20
I need to write a function that takes a user-provided string like 1-3-5, and output a...
I need to write a function that takes a user-provided string like 1-3-5, and output a corresponding series of letters, where A is assigned to 1, B is assigned to 2, C is assigned to 3, etc. So in the case of 1-3-5 the output would be ACE. For 2-3-4, it should print BCD. For ?-3-4 or --3-4 it should still print BCD. **CANNOT USE LISTS, SETS, DICTS, ETC. CANNOT USE SPLIT FUNCTION. ** Here is the code I have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT