Write a Java program similar to the triangle rectangle exercise. In this exercise, ask the user for two integers, the first is an integer x that is used to draw the area of the right triangle on the screen.
x
The second integer will be the width of the triangle. The height of the triangle will always be the same as the width.
width
The difference is in the orientation of the right triangle.
0 5
00000 0000 000 00 0
import java.util.Scanner; public class RightTriangle { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Prompt the user for the size of the triangle side System.out.print("Enter an integer for the size of the triangle side: "); int side = scanner.nextInt(); // Prompt the user for the width of the triangle System.out.print("Enter an integer for the width of the triangle: "); int width = scanner.nextInt(); // Draw the right-angled triangle for (int i = 0; i < width; i++) { // Print spaces according to the current position for (int j = 0; j < i; j++) { System.out.print(" "); } // Print the number according to the size of the side for (int k = 0; k < side; k++) { System.out.print("0"); } System.out.println(); } scanner.close(); } }
Click here to view the exercise solution
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
The code has been successfully copied to the clipboard.
Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!
Write a program in Java that asks for a number (x) and a width to show a square of that width.
Write a program in Java that requests a number x and then displays a rectangle 3 columns wide and 5 rows high using that digit.
Write a program in Java that asks for a symbol and a width to paint a triangle of that width and height using that number to draw it.
Write a program in Java that asks the user for a symbol and a width. You will use these to draw a hollow square of the given width and height.
Write a program in Java that asks the user for a symbol, a width and a height to draw a hollow rectangle with those dimensions.
Create a Java program to draw a parallelogram, with the width, height and character requested from the user.
Write a program in Java that asks the user for a string and displays a right-aligned triangle.
Create a Java program that asks the user for text and draws a pyramid on the screen.
Create a program Java to draw the graphic of the function y=(x-4)² for a range of integer values of x requested from the user.
Practice with exercises in Java to create geometric shapes of different types.
Free programming course with practical exercises and solutions in C#. Start learning right now!
Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play
Own and third party cookies to improve our services. If you go on surfing, we will consider you accepting its use.