How to generate a star Triangle pattern using Java - java

How to print a star triangle pattern using java. Pattern is something like this
* ====> row 1
* *
* *
* *
* * * * *
it can be n number of rows and from second row onwards there is odd number of white spaces between 2 stars like, 1,3,5, and last row have all the stars each separated by one white space.
Below is the Code I was working on to print the triangle?
public class Triangle
{
public static void main(String[] args)
{
int row = 4;
int space =0;
System.out.println("*");
for (int i=1;i<row;i++)
{
System.out.print("*");
for(space=0;space<i;space = space+i)
{
System.out.print(" ");
}
System.out.print("*");
System.out.println();
}
enter code here
for(int i=0;i<=4;i++)
{
System.out.print("* ");
}
}
}
How do I proceed?

public class Triangle {
public static void DrawWithStars(int dimension)
{
if(dimension < 0)
{
//Assuming that a triangle with dimension = 0 is a dot....
System.out.println("No valid dimension");
}
else
{
//To print the first dimension - 1 rows
for (int i = 0; i < dimension; i++)
{
for (int j = 0; j < dimension - i; j++) {
System.out.print(" ");
}
//Print the dot of the row 1 at the end
if(i != 0)
System.out.print("*");
for (int j = 0; j < 2 * i - 1; j++) {
System.out.print(" ");
}
System.out.println("*");
}
//To print the last row
for (int i = 0; i < dimension; i++)
{
System.out.print("* ");
}
System.out.println("*");
}
}
}

package apple;
public class Triangle
{
public static void main(String...strings){
int midspace = -1;
int row = 4;
String star = "";
for(int y=row-1; y>0; y--){
for(int space = 1;space <= y ; space++){
System.out.print(" ");
}
System.out.print("*");
for(int i = midspace; i>0; i--)
System.out.print(" ");
midspace += 2;
star = (y!=row-1) ? "*":"";
System.out.println(star);
}
for(int y=((row*2)-1); y>0; y--){
System.out.print("*");
}
}
}

Related

Print star pattern based on coordinate

I am new to java, I want to print a reversed star pattern based on the coordinate. After I set the coordinate, it will then print the star pattern
import java.util.Scanner;
public class coorTest {
public static void main(String[] args) {
int max = 5;
for (int y = 0; y < max; y += 2) {
int left_spacing = (int) Math.floor(y * 1.0 / 2.0);
for (int space = 0; space < left_spacing; space++) {
System.out.print(" ");
}
for (int x = 0; x < (max - y); x++) {
System.out.print("x");
}
System.out.println();
}
}
}
Try this. I am using (0,0) as a first coordinate.
public static void printStar(int x, int y) {
int starCount = 5;
int space = 0;
int count = 0;
// y-axis line move
for(int i=0; i<y; i++) {
System.out.println();
}
for (int i = 0; i < starCount; i++) {
// x-axis space move
for(int xAxis=0; xAxis<x; xAxis++) {
System.out.print(" ");
}
for (int j = 0; j < starCount; j++) {
if (count < space) {
System.out.print(" ");
count++;
continue;
} else if (j >= starCount - count) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
space++;
count = 0;
}
}

Drawing stars up and down

I want to get this result, where _ space characters :
*___*
_*_*_
__*__
public static void main(String args[]) {
int level = 2; // quantity line
int stars = 5; //quantity drawing stars
for(int i = 1;i <= level ; i++){
for(int j =1 ;j <= i; j++){
System.out.print(" ");
}
System.out.println("*");
}
}
So far, I have drawn,
*__
_*_
__*
And I don't know how to draw up ?
Steps to solve these type of questions:
consider * as 1 and spaces as 0. Now i need this output :
10001
01010
00100
first 1 is appearing according to row no. Row 0 - 1 at Col 0, Row 1 - 1 at Col 1
Second 1 is appearing at (total columns-current Row index-1)
print 1 for above two condition otherwise zero.
int rows=3; // quantity line
int cols=5; //quantity drawing stars
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
int k=cols-i-1;
if(i==j || j==k)
System.out.print("*");
else System.out.print(" ");
}
System.out.println();
}
int size=10; // Only one parameter is required which is quantity drawing stars
int length= size%2==0?size/2:size/2+1; // in case of odd one more line need to be print at last on which one Asteric appears.
for (int i = 0; i < length; i++) {
for (int j = 0; j < size; j++) {
if (i == j || i + j == size - 1) { //condition for diagonals
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
Output :
when size = 10;
* *
* *
* *
* *
**
when size = 11
* *
* *
* *
* *
* *
*
You can try below code and output is what you want..
for(int i=3;i>=1;i--)
{
for(int j=i;j<3;j++)
{
System.out.print(" ");
}
for(int j=1;j<=(2*i-1);j++)
{
if(j==1 || j==(2*i-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}

Using nested for loops to produce multiple lines of String output

I am learning Java for myself and have following problem:
Task: Write (explicitly) nested for loops to produce the following output:
5
5 5
5 5 5
5 5
5
My idea:
public class Exercises {
public static void main (String [] args) {
for (int line = 1; line <= 3; line++) {
for (int i = 1; i<= -2*line+6; i++){
System.out.print(" ");
}
System.out.println("5");
}
for ( int line = 4; line <= 5; line ++){
for (int i = 1; i <= 2*line-6; i++){
System.out.print(" ");
}
System.out.println("5");
}
}
}
My output:
5
5
5
5
5
I don't know how to get nested loops there. Can you give me not the answer, but some tips?
Try this solution:
/**
*
* #author Adil
*/
public class Exercises {
public static void main (String [] args) {
for (int i = 1; i <= 3; i++) {
for(int s = 4; s > i; s--) {
// add spacing
System.out.print(" ");
}
for (int j = 1; j < i; j++) {
//display/add star
System.out.print("5");
}
// add new line
System.out.println("");
}
for (int i = 1; i <= 3; i++) {
for (int s = 1; s < i; s++) {
// add spacing
System.out.print(" ");
}
for (int j = 4; j > i; j--) {
//display/add star
System.out.print("5");
}
// add new line
System.out.println("");
}
}
}
It will produce the following output:
5
55
555
55
5
Try this solution give below:
public class Exercises {
public static void main(String[] args) {
int noOfColumns = Integer.parseInt(args[0]); // Enter no of 5's in last
// column
int noOfRows = noOfColumns;
int index = noOfColumns;
int mid = noOfRows / 2;
for (int i = 0; i < noOfRows; i++) {
if (i <= mid) {
index = index - 2;
} else {
index = index + 2;
}
for (int j = 0; j < noOfColumns; j++) {
if (j % 2 == 0 && j >= index) {
System.out.print("5");
} else {
System.out.print(" ");
}
} // End of inner loop
System.out.println("");
} // End of outer loop
}
}

Print a Z shape pyramid using * stars

I am trying to write a program that outputs a Z pattern that is n number of * across the top, bottom, and connecting line using for loops.
Example:
Enter a number: 6
******
*
*
*
*
******
This is my current code, it's producing a half pyramid upside down.
import java.util.*;
public class ZShape {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
for (int x = 0; x <= n; x++) {
for (int y = n; y >= 1; y--) {
if (y > x) {
System.out.print("* ");
}
else
System.out.print(" ");
}
System.out.println();
}
}
}
This is the logic in the following code:
Loop over each row of the output (so from 0 to n excluded so that we have n rows)
Loop over each column of the output (so from 0 to n excluded so that we have n columns)
We need to print a * only when it is the first row (x == 0) or the last row (x == n - 1) or the column is in the opposite diagonal (column == n - 1 - row)
Code:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
for (int row = 0; row < n; row++) {
for (int column = 0; column < n; column++) {
if (row == 0 || row == n - 1 || column == n - 1 - row) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Sample output for n = 6:
******
*
*
*
*
******
(Note that this output has trailing white-spaces for each row, you did not specify whether they should be included, but it is easy to remove them by adding another check).
How about using three loops instead?
for (int x = 0; x < n; x++) {
System.out.print("*");
}
System.out.println();
for (int x = n-3; x >= 0; x--) {
for (int y = x; y >= 0; y--) {
System.out.print(" ");
}
System.out.println("*");
}
for (int x = 0; x < n; x++) {
System.out.print("*");
}
public class Star {
public static void main(String[] args) {
for (int i = 0; i <=4; i++) {
for (int j = 0; j <=4; j++)
{
if (i==4 || (i+j)==4 || i==0)
{
System.out.print(" * ");
}
else
{
System.out.print(" ");
}
}
System.out.println(" ");
}
}
}
Here is a logic to print Z shape:
when i = 1, then the First line will print only "#"
when i = n, then the Last line will print in same way by "#" only
when i = j, that means it will execute one diagonal line by "#" only
Code:
public static void main(String[] args) {
Scanner scr = new Scanner(System.in);
int n = scr.nextInt();
for (int i = 1; i <= n; i++) {
for(int j = n; j >= 1; j--) {
if (i == 1 || i == n || i == j) {
System.out.print("# ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}

Pascal's triangle without using arrays (only loops)

I am trying to output Pascal's triangle with asterisks (*). This is my code:
public static void main(String [] arg)
{
int n=3;
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i;j++)
{
System.out.print(" ");
}
boolean b=true;
for(int k=0;k<i*2+1;k++)
{
if(b)
{
System.out.print("*");
b=false;
}
else
{
System.out.print(" ");
b=true;
}
}
}
System.out.println(" ");
}
}
I have rechecked several times and failed to find any error.
Also let me know whether the if-block is implemented correctly.
The following code is not giving the required output as below:
*
* *
* * *
Your System.out.println() statement is outside of your for loop instead of inside.
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
System.out.print(" ");
}
boolean b = true;
for (int k = 0; k < i * 2 + 1; k++) {
if (b) {
System.out.print("*");
b = false;
} else {
System.out.print(" ");
b = true;
}
}
}
System.out.println(" "); // called only once
// output is
// * * * * * *
Just move it inside the close brace and your program will work.
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
System.out.print(" ");
}
boolean b = true;
for (int k = 0; k < i * 2 + 1; k++) {
if (b) {
System.out.print("*");
b = false;
} else {
System.out.print(" ");
b = true;
}
}
System.out.println(" "); // called once for each iteration
}
// output is
// *
// * *
// * * *
make inner loop reverse
for(int j=n-i;j>0;j--)
{
System.out.print(" ");
}
this should print from 3 starts then 2 then 1 star

Categories

Resources