Question

In: Computer Science

5.24 (Diamond Printing Program) Write an application that prints the following diamond shape. You may use...

5.24 (Diamond Printing Program) Write an application that prints the following diamond shape. You may use output statements that print a single asterisk (*), a single space or a single new- line character. Maximize your use of repetition (with nested for statements), and minimize the number of output statements."

Example for 2.24:

   *
***
*****
*******
*********
*******
*****
***
*

Solutions

Expert Solution

import java.util.Scanner;

public class PrintDiamond {
   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);
       System.out.println("Enter N : ");
       int n = sc.nextInt();

       // printing the first half
       for (int i = 1; i <= n; i+=2) {
           for (int j = 1; j <= i; j++) {
               System.out.print("*");
           }
           System.out.println();
       }
       // printing the second half
       for (int i = n - 1; i >=0; i-=2) {
           for (int j = 1; j <= i; j++) {
               System.out.print("*");
           }
           System.out.println();
       }
   }
}


Related Solutions

1. Write an application that prints the following diamond shape. You may use output statements that...
1. Write an application that prints the following diamond shape. You may use output statements that print a single asterisk (*), a single space or a single newline character. Maximize your use of repetition (with nested for statements), and minimize the number of output statements 2. Modify the application you wrote in Exercise 5.20 (Question 1 Above) to read an odd number in the range 1 to 19 to specify the number of rows in the diamond. Your program should...
use Python datetime module, strftime Write a program that processes the following textfile and prints the...
use Python datetime module, strftime Write a program that processes the following textfile and prints the report shown below. Textfile: Ask the user for the filename and BE SURE TO CHECK THAT IT EXISTS. The file will contain lines which each contain 2 strings (bookTitle and publicationDate) separated by a comma and a space. Note that the title of the book might contain spaces! Example of the textfile: Gone Girl, 5-24-2012 To Kill A Mockingbird, 7-11-1960 Harry Potter and the...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Use if statements to write a Java program that inputs a single letter and prints out...
Use if statements to write a Java program that inputs a single letter and prints out the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way: 2 = ABC    3 = DEF   4 = GHI    5 = JKL 6 = MNO   7 = PRS   8 = TUV 9 = WXY No digit corresponds to either Q or Z. For these 2 letters your program should print a message indicating that they are not...
Write an application that prints a table of the binary and octal equivalent of the decimal...
Write an application that prints a table of the binary and octal equivalent of the decimal numbers in the range 1 through 256. **Write in JAVA**
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
Write a mail merge application titled EmailMerge.java You will use two files for this program.  The first...
Write a mail merge application titled EmailMerge.java You will use two files for this program.  The first is a text file that contains a template letter. template.txt [ Dear <>, Because you are <> years old and <>, we have a free gift for you. You have absolutely nothing to buy; just pay the shipping and handling charge of $9.99. To claim your gift, call us immediately. Thank you, Office of Claims Department ] The tags <>, <>, and <> are...
(C++) Write a program that prints a table in the following format given a "x" read...
(C++) Write a program that prints a table in the following format given a "x" read from the keyboard. For example, if x is 4, it prints out 0 0 1 1 2 4 3 9 4 16 To avoid mismatch, put 8 white spaces between two numbers.
Write an application, CylinderStats, that reads the radius and height of a cylinder and prints its...
Write an application, CylinderStats, that reads the radius and height of a cylinder and prints its surface area and volume. Use the following formulas. Print the output to five decimal places. In the formulas, represents the radius and the height.Surface are 2πr(r+ h) Volume:πr2h Design and implement a class Cylinder that contains instance data that represents the cylinder’s radius and height. Define a Cylinder constructor to accept and initialize the radius and height.Include getter and setter methods for all instance...
Write an assembly language program that prints your first name in the output. Use immediate addressing...
Write an assembly language program that prints your first name in the output. Use immediate addressing with a hexadecimal constant to designate the operand of CHARO for each letter of your name. Comment each line except STOP and END. Cut and paste the Assembler Listing into your document and paste a screen shot of the Output area of the Pep8. Use the name "Kevin" as example Assembler Listing Screen Shot of Output area of the Pep8 program
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT