Make a upside down triangle in java - java

I am trying to make the triangle I have made up side down.
Tried many times, but I don't know how to do this.
The code I have know is:
public static void drawPyramide(int lines, char symbol, boolean startDown) {
//TRIANGLE
if(startDown) {
//The triangle up side down should be here.
}
else {
int c = 1;
for (int i = 0; i < lines; i++) {
for (int j = i; j < lines; j++) {
System.out.print(" ");
}
for (int k = 1; k <= c; k++) {
if (k%2==0) System.out.print(" ");
else System.out.print(symbol);
}
System.out.print("\n");
c += 2;
}
}
}
Any suggestions how I can "flip" this triangle? Thanks.

To flip the triangle you really just need to change the direction of iteration. Instead of going from i = 0 to i < lines you need to go down from i = lines-1 to i >= 0
You also need to change the c to how many spaces and symbols you want to start with.
Could look like this:
int c = 2*lines;
for (int i = lines-1; i>=0; i--)
{
for (int j = i; j < lines; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= c; k++)
{
if (k % 2 == 0)
{
System.out.print(" ");
}
else
{
System.out.print(symbol);
}
}
System.out.print("\n");
c -= 2;
}

Reverse the first loop condition i.e. start from number of line and decrease it. Also adjust you c accordingly and make it reduce from high to low e.g. below:
int c = 2*lines-1;
for (int i = lines; i > 0; i--) {
for (int j = i; j < lines; j++) {
System.out.print(" ");
}
for (int k = 1; k <= c; k++) {
if (k%2==0) System.out.print(" ");
else System.out.print(symbol);
}
System.out.print("\n");
c -= 2;
}

import java.util.Scanner;
public class EquilateralTraingle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int side = sc.nextInt();
constructEquTri(side);
}
private static void constructEquTri(int length) {
// loop for each line
for (int i = length; i > 0; i--) {
// loop for initial spaces in each line
for (int k = 0; k < length - i; k++) {
System.out.print(" ");
}
// loop for printing * in each line
for (int j = 0; j < i; j++) {
System.out.print("*");
System.out.print(" ");
}
System.out.println();
}
}
}
/*Output:
10
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
*/

Related

where to add space between asterisks

Where/how to add space in between asterisks,
this is what i've done so far..
int i, j, k;
int n = 5;
for (i = 1; i <= n; i++) {
for (j = 0; j < (n - i); j++) {
System.out.print(" ");
}
for (j = 1; j <= i; j++) {
System.out.print("*");
}
for (k = 1; k < i; k++) {
System.out.print("*");
}
System.out.print("\n");
}
for (i = n - 1; i >= 1; i--) {
for (j = 0; j < (n - i); j++) {
System.out.print(" ");
}
for (j = 1; j <= i; j++) {
System.out.print("*");
}
for (k = 1; k < i; k++) {
System.out.print("*");
}
System.out.print("\n");
}
Your actual output is this:
*
***
*****
*******
*********
*******
*****
***
*
Where/how to add space in between asterisks?
If your expected output is this:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Use an extra space in each print statement
int i, j, k;
int n = 5;
for (i = 1; i <= n; i++) {
for (j = 0; j < (n - i); j++) {
System.out.print(" ");
}
for (j = 1; j <= i; j++) {
System.out.print("* ");
}
for (k = 1; k < i; k++) {
System.out.print("* ");
}
System.out.print("\n");
}
for (i = n - 1; i >= 1; i--) {
for (j = 0; j < (n - i); j++) {
System.out.print(" ");
}
for (j = 1; j <= i; j++) {
System.out.print("* ");
}
for (k = 1; k < i; k++) {
System.out.print("* ");
}
System.out.print("\n");
}
Check here a working demo
As long as I understand your question correctly, you should be able to replace "**" with "* *" if you want to " add space in between asterisks". Something like this:
String asterisks = "**";
asterisks = asterisks.replaceAll("\\*\\*, \\* \\*);

Java star pattern program not sending expected output

Working on a java program that should take as input an even number and the print out a star pattern of asterisk symbols with spaces. I have it working with an input of 6, but no higher and have no clue why.
My Code:
package starpattern;
import java.util.Scanner;
public class StarPattern {
public static void main(String[] args)
{
int rows, cols;
Scanner keyboard = new Scanner(System.in);
System.out.print("How many columns? ");
cols = keyboard.nextInt();
if (cols%2 == 0)
{
int col2 = cols;
int spaces = (cols/2 - 1);
int ASTS = 2;
int test = (col2/2);
while (cols > (col2/2))
{
for (int x = spaces; x > 0; x--)
{
System.out.print(" ");
}
for (int y = ASTS; y > 0; y--)
{
System.out.print("*");
}
for (int z = spaces; z > 0; z --)
{
System.out.print(" ");
}
System.out.println();
cols--;
ASTS +=2;
spaces--;
}
spaces = (cols/2 - 1);
while (cols > 0)
{
if (test != (col2/2))
{
for (int x = spaces; x < (cols/2); x++)
{
System.out.print(" ");
}
spaces -=2;
}
for (int a = ASTS-2; a > 0; a--)
{
System.out.print("*");
}
test++;
System.out.println();
cols--;
ASTS -= 2;
}
}
}
}
Each step up (10, 12,...) has some form of the same issue. Is there a way I can fix this on the bottom half (Starting at "while cols>0") So that it gives the proper output of spaces?
------Smart Coding------Good Coder
**Very Simplest Way To create Paramid use only three loops**
D:\Java>java ParamidExample
Enter a number
5
*
* * *
* * * * *
* * * * * * *
* * * * * * *
* * * * *
* * *
*
import java.util.*;
public class ParamidExample
{
public static void main(String args[])
{
System.out.println("Enter a number");
Scanner sc=new Scanner(System.in);
int no=sc.nextInt();
int count=1;
for(int i=1;i<=2*no-1;i++)
{
for(int j=count;j<=no;j++)
{
System.out.print(" ");
}
for(int k=1;k<=count*2-1;k++)
{
System.out.print("* ");
}
if(i<no)
count++;
else
count--;
System.out.println("");
}
}
}
Very Simplest Way To create Paramid use only three loops
I tried to find what you actually doing in your code, but it is a bit overkill :). Maybe your reversed method is wrong at all and it is hardcoded to work only for number 6.
However I appreciate your effort and I can show you, how to write it better. This is a fully working code in Java :
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("How many rows? ");
int rows = keyboard.nextInt();
triangle(rows);
triangleReversed(rows);
}
private static void triangle(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
for (int j = 0; j < i * 2 + 1; j++) {
System.out.print("*");
}
System.out.println("");
}
}
private static void triangleReversed(int n) {
for (int i = n-1; i >= 0; i--) {
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
for (int j = 0; j < i * 2 + 1; j++) {
System.out.print("*");
}
System.out.println("");
}
}
With this sample output :
How many rows? 12
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
***********************
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
Note that it is good to separate program into methods for better readability. Also note that method triangle and triangleReversed is almost identical, it differs only on theirs first row.

Drawing a pyramid of stars

I need to write a program that will print the following output
*
* * *
* * * *
but my code till now is printing instead this
*
* *
* * *
My code is:
public class Pyramid2 {
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4 - i; j++) {
System.out.print(" ");
}
for (int k = 0; k < i; k++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Looks little weird, but just do not run the loop for value of i = 1, ideally pyramid is always 1, 2 , 3... so on structure type...
public static void main(String[] args) {
for (int i = 0 ; i < 4 ; i++) {
if (i != 1) {
for (int j = 0 ; j < 4 - i ; j++) {
System.out.print(" ");
}
for (int k = 0 ; k <= i ; k++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output
*
* * *
* * * *
I agree that there might be an error in the assignment, and that three separate print statements would be a perfectly good way of executing it.
Seems you could also slip in an if statement to exclude the line with two stars from the loop.
Please check the Lower Piramid Part, I was doing for some other answer,But that one is closed now. So check below once hope it may help
// * * * * * * *
// * * * * *
// * * *
// *
// * * *
// * * * * *
// * * * * * * *
// Insert input for Total Use only odd Number
int total = 11, t = 0;
// Upper Piramid
for (int i = 0; i < total / 2; i++) {
t = 0;
for (int k = i; k > 0; k--) {
System.out.print(" ");
t++;
}
for (int j = 0; j < total - (t * 2); j++) {
System.out.print("*");
}
for (int j = 0; j < t; j++) {
System.out.print(" ");
}
System.out.println("");
}
total = total - (total / 2);
// Lower Piramid
for (int i = total; i > 0; i--) {
t = 0;
for (int j = i; j > 1; j--) {
System.out.print(" ");
t++;
}
for (int j = 0; j < total - t; j++) {
System.out.print("*");
}
for (int j = 1; j < total - t; j++) {
System.out.print("*");
}
for (int j = i; j > 1; j--) {
System.out.print(" ");
}
System.out.println("");
}

Printing a Square with loops

//college work
Hello, I am trying to print a square by using loops, it also require the user to input the height and width.
The Output should look like this
....
. .
....
any help would be apprciated
import java.util.Scanner;
public class Ex1 {
public static void main(String[] args) {
Scanner input = new Scanner(System. in );
System.out.print("Please enter the height of the box: ");
int x = input.nextInt();
System.out.println("Please enter a width for the box: ");
int y = input.nextInt();
drawbox(x, y);
}
static void drawbox(int x, int y) {
for (int j = 0; j < y; j++) {
System.out.println("*");
System.out.println();
for (int i = 0; i < x - 2; i++) {
System.out.print("*");
for (int z = 0; z < y - 2; z++) {
System.out.print(" ");
}
System.out.println("*");
for (int i = 0; j < y; j++) {
System.out.println("*");
}
}
}
}
}
Change your loop from
for(int i = 0; j<y ; j++){
System.out.println("*");
}
To
for(int j = 0; j<y ; j++){
System.out.println("*");
}
To draw rectangle, change your drawbox code like:
static void drawbox(int x, int y) {
for (int i = 0; i < y; i++) {
System.out.print("*");
}
System.out.println();
for (int i = 0; i < x - 2; i++) {
System.out.print("*");
for (int j = 0; j < y - 2; j++) {
System.out.print(" ");
}
System.out.println("*");
}
for (int i = 0; i < y; i++) {
System.out.print("*");
}
System.out.println();
}
change
for(int i = 0; j<y ; j++){
System.out.println("*");
}
to
for(int i = 0; i<y ; i++){
System.out.println("*");
}
You could use this(see below). this will print *'s for the first and last row. for the middle rows, it prints a star followed by spaces and closes with a star. It also has another method allStars which reuses code for first and last lines.
static void drawbox(int height, int width){
for (int i = 1; i <= height; i++) {
if(i==1 || i==height){
allStars(width);
}else{
System.out.print("* ");
for(int k = 2; k < width; k++){
System.out.print(" ");
}
System.out.print("*");
System.out.print("\n");
}
}
}
static void allStars(int width){
for(int k = 1; k <= width; k++){
System.out.print("* ");
}
System.out.print("\n");
}
Your solution was good enough when issue fixed. But take this as easy way, hope you will get some idea from this
private static void printSquare(int width,int length){
for(int i=0;i<width;i++){
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append("* ");
for (int j=2;j<length;j++){
if(i==0){
stringBuilder.append("* ");
}else if(i==width-1){
stringBuilder.append("* ");
}else {
stringBuilder.append(" ");
}
}
stringBuilder.append("* ");
System.out.println(stringBuilder);
}
}
When printSquare(5,5);, Out put
* * * * *
* *
* *
* *
* * * * *
A simple algorithm to draw square by loops
public void draw(int length){
for(int i = 0; i < length; i++){
for(int j = 0;j < length; j++){
if(i!= 0 && j > 0 && i!= length - 1 && j!= length-1){
System.out.print(" ");
}
else System.out.print("*");
}
System.out.println();
}
}
Try this one:
import java.util.Scanner;
public class Rectangle {
public static void main(String args[]){
Scanner input = new Scanner(System. in );
System.out.print("Please enter the height of the box: ");
int x = input.nextInt();
System.out.print("Please enter a width for the box: ");
int y = input.nextInt();
drawbox(x, y);
}
static void drawbox(int x, int y) {
for (int j = 0; j < y; j++) {
System.out.print("* ");
}
System.out.println();
for (int i = 0; i < x; i++) {
System.out.print("* ");
for (int z = 0; z < y - 2; z++) {
System.out.print(" ");
}
System.out.println("*");
}
for (int k = 0; k < y; k++) {
System.out.print("* ");
}
}
}
Output:
Please enter the height of the box: 10
Please enter a width for the box: 10
* * * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * * *
try this
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World");
int row = 15;
int clmn =50;
for(int i= 0;i<row; i++ ){
for(int j= 0;j<clmn; j++ ){
if(i == 0 || i == row-1 ||j == 0 || j == clmn){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println("*");
}
}
}

Print an ASCII diamond of asterisks

My program that prints out a diamond like this:
...............*
..........* * *
.....* * * * *
* * * * * * *
.....* * * * *
..........* * *
...............*
But it only works if the parameter or each side of the diamond is 4. For example if I input 6, the spacing on the bottom triangle is wrong and I been trying to figure it out.
The bottom triangle doesn't change when the parameter is changed, only the top one does. It only works for input 4.
public static void printMoreStars(int n) {
int rowsPrime = 0;
for (int i = n + 1; i > 0; i--) {
for (int j = 0; j < (2 * i) - 1; j++) {
System.out.print(" ");
}
for (int d = 0; d < (2 * rowsPrime) - 1; d++) {
System.out.print("*" + " ");
}
System.out.println(); //new line character
rowsPrime += 1;
System.out.println(" ");
}
//bottom triangle
for (int i = 1; i < n + 1; i++) {
for (int j = 0; j < (2 * i) + 1; j++) {
System.out.print(" ");
}
for (int d = 0; d < rowsPrime; d++) {
System.out.print("*" + " ");
}
System.out.println(); //new line character
rowsPrime -= 2;
System.out.println(" ");
}
}
You made two mistakes when using rowPrimes. See my annotations below:
public class Stars {
public static void main(String[] args) {
printMoreStars(Integer.parseInt(args[0]));
}
public static void printMoreStars(int n) {
int rowsPrime = 0;
for (int i = n + 1; i > 0; i--) {
for (int j = 0; j < (2 * i) - 1; j++) {
System.out.print(" ");
}
for (int d = 0; d < (2 * rowsPrime) - 1; d++) {
System.out.print("*" + " ");
}
System.out.println(); //new line character
rowsPrime += 1;
System.out.println(" ");
}
rowsPrime -= 2; // <- middle line is already printed, so skip that
//bottom triangle
for (int i = 1; i < n + 1; i++) {
for (int j = 0; j < (2 * i) + 1; j++) {
System.out.print(" ");
}
for (int d = 0; d < (2 * rowsPrime) - 1; d++) { // <- changed condition to be the same as above
System.out.print("*" + " ");
}
System.out.println(); //new line character
rowsPrime--; // <- you have to decrease rowPrime by one.
System.out.println(" ");
}
}
}
To print a rhombus with dots on the left, you can use this code.
Try it online!
public static void main(String[] args) {
printRhombus(3);
}
public static void printRhombus(int n) {
for (int i = -n; i <= n; i++) {
// last element in the row
int last = n - Math.abs(i);
for (int j = -n; j <= last; j++)
if (Math.abs(i) + Math.abs(j) <= n)
System.out.print("*" + (j < last ? " " : ""));
else
System.out.print(".....");
System.out.println();
}
}
Output:
...............*
..........* * *
.....* * * * *
* * * * * * *
.....* * * * *
..........* * *
...............*
See also: Print a diamond shape with Java
Check This:
import java.io.*;
import java.lang.*;
import java.util.*;
class DiamondPattern {
static public int ReadInteger() {
try {
String inpString = "";
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
String s = reader.readLine();
return Integer.parseInt(s);
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
public static void main(String[] args) {
System.out.println("Program for displaying pattern of *.");
System.out.print("Enter the maximum number of *: ");
int n = ReadInteger();
System.out.println("\nHere is the Diamond of Stars\n");
for (int i = 1; i <= n; i++) {
for (int j = 0; j < (n - i); j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 0; j < (n - i); j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}
System.out.println();
}
}

Categories