Print a diagonal triangle in java - java

I need to print out a triangle that looks like this:
*
**
***
****
The code I have right now
for(line = 0; line < size; line++){
for(count = 0; count < line; count++){
System.out.print("*");
for(space = 0; space < line; space++)
System.out.print(" ");
}
System.out.println();
}
I get this
*
**
***
****
*****
******

for(line = 0; line < size; line++){
for(space = 0; space < line; ++space)
System.out.print(" ");
for(count = 0; count < line; count++)
System.out.print("*");
System.out.println();
}

You're printing the spaces on the same line. Call System.out.println(); before printing the spaces.
Edit - Example:
for (line = 0; line < size; line++){
for(space = 0; space < line - 1; space++)
System.out.print(" ");
for (count = 0; count < line; count++)
System.out.print("*");
System.out.println();
}

You need to first print the prefix spaces. and then print the stars.
Have a try with this:
int line = 0;
int size = 6;
int count = 0;
int space = 0;
for (line = 0; line < size; line++) {
//print spaces
for (space = 0; space < line; space++)
System.out.print(" ");
//Print stars
//Note: here count condition should be count < line+1, rather than count < line
//If you do not do so, the first star with print as space only.
for (count = 0; count < line+1; count++) {
System.out.print("*");
}
System.out.println();
}
Output in console:
*
**
***
****
*****
******

class Pyramid
{
public static void main(String args[])
{
java.util.Scanner pyr=new java.util.Scanner(System.in);
System.out.println("type a no. to make Pyramid");
int n= pyr.nextInt();
for (int i=1; i<=n; i++)
{
for(int j=n; j>i; j--)
{
System.out.print(" ");
}
for(int k=1; k<=i; k++)
{
System.out.print("* ");
}
System.out.println();
}
}

Just printing the spaces before the asterisks would be fine.

Related

Why is my program printing the next line at a location i didnt ask for?

Basically i have to print 2 traingles, top up upside down, buttom one upside up. They are both the same legnth, my program works fine yet for some reason my second triangle gets slighty tilted to the right.
Can anyone please explain to me how to fix and why this bug happens?
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter number");
int num = s.nextInt();
for (int i = 0; i < num; i++) {
for (int j = num; j > i; j--) {
System.out.print("*");
System.out.print(" ");
}
System.out.println();
for (int k = 0; k <= i; k++) {
System.out.print(" ");
}
}
// second part
for (int i = 0; i < num; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
System.out.print(" ");
}
System.out.println();
for (int k=num; k>i; k-=2) {
System.out.print(" ");
}
}
}
}
* * *
* *
*
*
* *
* * *
A couple of small tweaks:
in the end of the first outer for loop
in the second outer for-loop at the end
See code comments below
for (int i = 0; i < num; i++) {
for (int j = num; j > i; j--) {
System.out.print("*");
System.out.print(" ");
}
System.out.println();
for (int k = 0; k < i; k++) { // stop condition changed
System.out.print(" ");
}
if (i < num -1) { // this was added
System.out.print(" ");
}
}
// second part
for (int i = 0; i < num; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
System.out.print(" ");
}
System.out.println();
for (int k=num-1; k>i+1; k-=1) { // stop condition change
System.out.print(" ");
}
}

Drawing a Diamond with User Input (not double of the user input)

So I've been trying to draw a Diamond shape in Java and I've got the top part of the diamond done but the bottom part is not printing as I want it to. Instead of decreasing towards the end, it stays the same or it increases as it go down.
import java.util.Scanner;
public class Q1_Diamond{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("How many lines? ");
int lines = sc.nextInt();
// Top half of Diamond
for(int i = 0; i < lines/2; i++){
for(int j = 0; j < (lines-1)-i; j++){
System.out.print(" ");
}
for(int j = 0; j < i; j++){
System.out.print("*");
}
for(int j = 0; j < i+1; j++){
System.out.print("*");
}
System.out.println();
}
// Bottom half of Diamond
// Even number of lines
if(lines % 2 == 0){
for(int k = 0; k < (lines/2); k++){
for(int j = 0; j < (lines/2)+k; j++){
System.out.print(" ");
}
for(int j = 0; j < (lines/3 - 0.5f); j++){
System.out.print("*");
}
for(int j = 0; j < lines/2+1; j++){
System.out.print("*");
}
System.out.println();
}
}
// Odd number of lines
else{
not done yet...
}
}
}
Remove the if condition (i.e. if(lines % 2 == 0)) for the lower half and simply repeat the same code as the upper half with the following loop declaration:
for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--)
Complete code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many lines? ");
int lines = sc.nextInt();
// Top half of Diamond
for (int i = 0; i < lines / 2; i++) {
for (int j = 0; j < (lines - 1) - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
// Bottom half of Diamond
for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--) {
for (int j = 0; j < (lines - 1) - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
A sample run:
How many lines? 10
*
***
*****
*******
*********
*********
*******
*****
***
*
Another sample run:
How many lines? 11
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
[Update]
You can extract the common code into a method e.g. printLine as shown below:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many lines? ");
int lines = sc.nextInt();
// Top half of Diamond
for (int i = 0; i < lines / 2; i++) {
printLine(lines, i);
}
// Bottom half of Diamond
for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--) {
printLine(lines, i);
}
}
static void printLine(int lines, int i) {
for (int j = 0; j < (lines - 1) - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
Another way to tackle this is to determine expressions for the number of spaces and asterisks in each line of the star. Obviously this will need to be in terms of the number of lines (n) and the line we're on.
If we number lines from i = 0 to n-1 we get
nAsterisks = 1 + 2 * min(i, n-i-1)
and
nSpaces = (n+1)/2 - 1 - min(i, n-i-1)
With these we can write some compact Java:
static void printStar(int n)
{
for(int i=0, k=0; i<n; i++, k=Math.min(i, n-i-1))
{
for(int j=0; j<(n+1)/2-1-k; j++) System.out.print(" ");
for(int j=0; j<1+2*k; j++) System.out.print("*");
System.out.println();
}
}
For printStar(8) we get:
*
***
*****
*******
*******
*****
***
*
and printStar(9) gives
*
***
*****
*******
*********
*******
*****
***
*

How do i draw an Arrow in Java?

Here is what I am trying to create:
*
**
***
****
*********
****
***
**
*
Here is what i have created:
*
**
***
****
**********
****
***
**
*
Here is my code:
for(int i = 1; i <= 5; i++){
for(int j = 1; j<=i; j++){
System.out.print("*");
}
System.out.println();
if(i == 4){
for(int f = 0; f < 5; f++){
System.out.print("*");
}
}
}
for(int i = 1; i <= 5; i++){
for(int j = 4; j>=i; j--){
System.out.print("*");
}
System.out.println();
}
I dont know how to indent the tail part, please dont give me the answer, just tell me where the problem is and I will try to do it. Thank you!
Given the shape you have provided, and assuming it's going to be output in a monospace font, we can draw it on a grid.
Splitting it visually into 2 sections, we can see that there are 2 main modes.
The tail (green), and the head (red).
There's also a second mode. where the number of stars in the head increase and decrease.
The size of the tail could potentially vary, as could the size of the head, and it would still be a shape we recognise as an arrow.
When outputting text, the easiest iteration order is generally left to right, up to down, unless dealing with right to left languages, or vertical reading, I'm going to assume western culture for the output, as that is what's been popularised in programming output streams.
So, the question is, how best to build up the strings required for output.
Given the format that the output is going to be on a stream,
width is going to be your outside loop, and tail/length your inside loop.
The code you provided uses 10 for width, by splitting it into 2 groups of 5, with the second one being offset by one.
for(int i = 1; i <= 5; i++){
for(int j = 1; j<=i; j++){
System.out.print("*");
}
System.out.println();
if(i == 4){
for(int f = 0; f < 5; f++){
System.out.print("*");
}
}
}
for(int i = 1; i <= 5; i++){
for(int j = 4; j>=i; j--){
System.out.print("*");
}
System.out.println();
}
so translating your code back to the drawings, you loop over width, and on the last iteration output the line of stars for the tail.
What you need to do, is output a space on all other lines.
There are a variety of ways to do so, you could output the character, or use one of the fixed width formatting functions using printf.
But given your current code, the minimal difference will be to output a space character, when the width iteration for the increasing mode is not 4, and for the loop of width - 1.
Looking at your end result, your tail is being printed after your head. That needs to move earlier.
Your tail is too long, need to offset that by 1.
And you need to insert pink and brown sections by printing spaces, the same amount of times as you output the tail, whenever you do not output the tail.
You should add another inner for-loop that prints " " before the for-loop that prints "*".
You can use printf method to tell Java print in specific format. For example, you need 5 spaces prefixed then print your text:
System.out.printf("%5s","hello");
To get your desired output, try the below code. Note: There may be better solutions.
for (int i = 1; i <= 5; i++) {
if (i != 5) // middle line should not be prefix with spaces
// empty 5 spaces before starting the loop
System.out.printf("%5s", "");
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
if (i == 4) {
for (int f = 0; f < 5; f++) {
System.out.print("*");
}
}
}
for (int i = 1; i <= 5; i++) {
// print 5 space before
System.out.printf("%5s", ""); // <== note the printf here with empty string
for (int j = 4; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}
Here's a method you can use to print the arrow:
private static void printArrow (final int min, final int max, final int tip)
{
final int numSpaces = tip - max - 1;
// print the top
for (int i = min; i <= max; i++)
{
for (int j = 0; j < numSpaces; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.println();
}
// print the tip
for (int i = 0; i < tip; i++)
{
System.out.print("*");
}
System.out.println();
// print the bottom
for (int i = max; i >= min; i--)
{
for (int j = 0; j < numSpaces; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.println();
}
}
Call it from main with:
public static void main(String[] args)
{
// this will print the arrow in your question
printArrow(1, 4, 9);
}
Another Example:
printArrow(1, 10, 19);
Outputs:
*
**
***
****
*****
******
*******
********
*********
**********
*******************
**********
*********
********
*******
******
*****
****
***
**
*
Here is my answer. You just need to first insert the space and then print * until you reach the middle line. If you want to make it dynamic, just replace the boundary value for k dynamically the same as the boundary for the f:
public static void main(String[] args) {
int k = 1;
int m = 0;
for (int i = 1; i <= 5; i++) {
m = i - 1;
while (k != 6 && m != 4) {
System.out.print(" ");
k++;
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
if (i == 4) {
for (int f = 0; f < 5; f++) {
System.out.print("*");
}
}
k = 1;
}
k = 1;
for (int i = 1; i <= 5; i++) {
while (k != 6) {
System.out.print(" ");
k++;
}
for (int j = 4; j >= i; j--) {
System.out.print("*");
}
System.out.println();
k = 1;
}
}
EDIT
I realized the output is one* behind. So just need to change the boundary value of k from 5 to 6 (from the value of boundary for f to the value of boundary for f+1)
The output looks like this:
for (int i = 1; i <= 5; i++) {
if (i!=5) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
if (i == 4) {
for (int f = 0; f < 5; f++) {
System.out.print("*");
}
}
}
for (int i = 1; i <= 5; i++) {
System.out.print(" ");
for (int j = 4; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}

java nested for loop spaces and numbers

I can't seem to figure out how to figure this problem out. I want to add a number and remove a space on each line working down from 1.
for (int line = 1; line <= 5; line++) {
for (int space = 5; space >= line + 1; space--) {
System.out.print(" ");
}
System.out.println(line);
}
The trick to your problem is recognizing that 5 characters need to be printed on each line. Each digit is printed the same number of times it represents, with spaces filling in the rest.
for (int i=1; i <= 5; i++) {
// print 5 minus i spaces
for (int j=5; j >= i + 1; j--) {
System.out.print(" ");
}
// repeat the ith digit i times
for (int k=0; k < i; ++k) {
System.out.print(i);
}
System.out.print("\n");
}
Output:
1
22
333
4444
55555
But note that you could even simplify this further, if you want fewer lines of codes, to this:
for (int i=1; i <= 5; ++i) {
String line = new String(new char[5-i]).replace("\0", " ") +
new String(new char[i]).replace("\0", String.valueOf(i));
System.out.println(line);
}
Try this
for (int line = 1; line <= 5; line++) {
for (int space = 5; space >= line + 1; space--) {
System.out.print(" ");
}
for (int k = 0; k < line ; k++) {
System.out.print(line);
}
System.out.println("");
}

Inverse parallelogram

I'm trying to make the output something like this:
I know the problem is in the third loop, but I dont know what to do to make the spacing work for this.
import java.util.Scanner;
public class Tester {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int x, y;
System.out.print("Enter the number of rows: ");
x = in.nextInt();
System.out.print("Enter the number of stars: ");
y = in.nextInt();
//loop for x lines
for(int i = 0; i < x; i++){
//loop for y stars
for(int j = 0; j < y; j++){
System.out.print("* ");
}
System.out.println();
for(int l = 0; l <= i; l--){
System.out.print(" ");
}
}
}
}
You need to do a couple things.
The first is move your last nested for loop (the one that prints spaces) to the beginning of the first for loop. You will also want to remove the space you have added to the for loop that prints the asterisks.
Then, for the output you have showed us, you need to start you main for loop at the end and go backwards.
Try the following:
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int x = in.nextInt();
System.out.print("Enter the number of stars: ");
int y = in.nextInt();
//loop for x lines
//This starts at x and goes toward 0
for(int i = x; i > 0; i--){
//Insert spaces based on line number
//This is at the beginning now
for (int s = 0; s < i; s++)
System.out.print(" ");
//Print y stars
//Removed the space after the asterisk
for(int j = 0; j < y; j++)
System.out.print("*");
System.out.println();
}
}
Tested here and matches the output in your first image
for (int i = 0; i < x; i++){
for (int j = x-1; j > i; j--) {
System.out.print(" ");
}
for(int j = 0; j < y; j++){
System.out.print("*");
}
System.out.println();
}
You have to reorder your for loops. Please note the change in condition in the second for loop in given ode below:
for(int i = 0; i < x; i++){
for(int l = 0; l <= x-i; ++l){
System.out.print(" ");
}
//loop for y stars
for(int j = 0; j < y; j++){
System.out.print("* ");
}
System.out.println();
}
Another way:
String stars = String.format("%0" + y + "d", 0).replace('0', '*');
for (int i=x; i > 0; i--)
{
System.out.println(String.format("%0$"+i+ "s", ' ')+stars);
}
System.out.println(stars);

Categories