Question

Write a JAVA program to display a “*” triangle and a half diamond of size n,...

Write a JAVA program to display a “*” triangle and a half diamond of size n, where n is the input of the program. For example, when the user enters 4 as n’s value, the program should print the following shapes of “*”: A triangle of size 4:

*

***

*****

*******

A half diamond of size 4:

*

***

*****

*******

*****

***

*

Extra Credit (+5) Print the following pattern instead. A triangle of size 4, using *:

*

***

*****

*******

A diamond of size 4, using *:

*

***

*****

*******

*****

***

*

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

import java.util.Scanner;

public class Main

{

public static void printTriangle(int n)

{

int i, j;

// outer loop to handle number of rows

// n in this case

for(i=1; i<=n; i++)

{

// inner loop to handle number of columns

// values changing acc. to outer loop

for(j=1; j<=2*i-1; j++)

{

// printing stars

System.out.print("*");

}

// ending line after each row

System.out.println();

}

}

public static void printDiamond(int n)

{

int i, j;

// outer loop to handle number of rows

// n in this case

for(i=1; i<=n; i++)

{

// inner loop to handle number of columns

// values changing acc. to outer loop

for(j=1; j<=2*i-1; j++)

{

// printing stars

System.out.print("*");

}

// ending line after each row

System.out.println();

}

for(i=n-1; i >= 1; i--)

{

for (j=1; j<=2*i-1; j++) {

// printing stars

System.out.print("*");

}

// ending line after each row

System.out.println();

}

}

// Driver Function

public static void main(String args[])

{

Scanner sc = new Scanner(System.in);

int n;

System.out.println("Enter the number of rows: ");

n = sc.nextInt();

printTriangle(n);

System.out.println("\n");

printDiamond(n);

}

}

​​​​​​​

Add a comment
Know the answer?
Add Answer to:
Write a JAVA program to display a “*” triangle and a half diamond of size n,...
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 Java program that reads in a positive integer from the user and outputs the...

    Write a Java program that reads in a positive integer from the user and outputs the following triangle shape composed of asterisk characters. For example, if user enters 5 as the input, then your program should display the following shape: ***** **** *** ** *

  • IN JAVA Overview In this project you will implement a Java program that will print several...

    IN JAVA Overview In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide the user to decide between different forms...

  • Write a Python program in this Jupyter Notebook. This program will allow a user to choose...

    Write a Python program in this Jupyter Notebook. This program will allow a user to choose from a menu to print various text shapes includeing rectangle, triangle, and diamond as well as a set of numbers in a pattern. This program will allow the user to select the type, the size and/or the fill character for a shape. Your program will verify user inputs such as size for certain range specifications, depending on the shapes.

  • Write a java program that asks the user to enter an integer. The program should then...

    Write a java program that asks the user to enter an integer. The program should then print all squares less than the number entered by the user. For example, if the user enters 120, the program should display 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100.

  • Hello Guys. I need help with this its in java In this project you will implement...

    Hello Guys. I need help with this its in java In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide...

  • Write a JAVA method that expands a given binomial (ax + by)n, where integers a, b,...

    Write a JAVA method that expands a given binomial (ax + by)n, where integers a, b, n are user inputs. For example, if a = 2, b = -12, n = 4 are entered the method should print or return (2x - 12y)^4 = 16x^4 - 384x^3y + 3456x^2y^2 – 13824xy^3 + 20736y^4 Use the Pascal’s triangle method using only one 1-dim array to calculate all binomial coefficients. 1. Write a JAVA method that expands a given binomial (ax by)",...

  • Java only please Write a program that displays the following menu: Geometry Calculator 1.       Calculate the...

    Java only please Write a program that displays the following menu: Geometry Calculator 1.       Calculate the Area of a Circle 2.       Calculate the Area of a Triangle 3.     Calculate the Area of a Rectangle 4.       Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius of the circle and then display its area. Use the formula:      area = ∏r2    Use 3.14159 for ∏. If the user enters 2 the program should ask for...

  • In BlueJ using Java, write a program that can input and display a person’s family tree....

    In BlueJ using Java, write a program that can input and display a person’s family tree. This project requires a GUI class that allows the user to input the members of the family tree and print the family tree.

  • In Java, write a program using a loop that asks the user to enter a series...

    In Java, write a program using a loop that asks the user to enter a series of decimal numbers. The user must enter -88 to end the input of the decimal numbers. After the user enters all numbers, the program should display the sum of all numbers entered.

  • Question 4 Write a program that display the area of a triangle. The program calls the...

    Question 4 Write a program that display the area of a triangle. The program calls the following two functions: .getBaseHeight - This function uses a reference parameter variables to accept a double arguments base and height. This function should ask the user to enter the triangle's buse and height. Input validation: base and height should be positive numbers. calArea - This function should accept the triangle's base and height as arguments and return the triangle's area. The area is calculated...

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