Create a Square of stars with an inputed number - java

I need a code to help me print a square of stars with an Integer input (in this case the number is 5)
the square must be empty on the inside.
for example:
Looking for this output
* * * * *
* *
* *
* *
* * * * *
what I get
* * * *
*
*
*
* * * *
I am missing my right side of the square.
MY code:
System.out.print("Please enter a number: ");
side = input.nextInt();
for (int i = 0; i < side - 1; i++) {
System.out.print("* ");
}
for (int i = 0; i < side; i++) {
System.out.println("*");
}
for (int i = 0; i < side; i++) {
System.out.print("* ");
}
}
input
5
output
* * * *
*
*
*
* * * *

You can do this with a nested for loop.
for (int i = 0; i < side; i++) {
for (int j = 0; j < side; j++) {
if (i == 0 || i == side - 1 || j == 0 || j == side - 1) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
Print a * if it is either first row/column or last row/column; otherwise print two spaces.

It can be done using a single For Loop with a Complexity of O(n)
public static void main(String[] args) {
System.out.print("Please Enter a Number ");
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
if(number >= 3) {
String endPattern = new String(new char[number]).replace("\0", " *");
String midPattern = String.format("%-"+(number-2)*2+"s %s", " *"," *");
for(int i=1; i<= number; i++) {
if(i==1 || i==number) {
System.out.print(endPattern);
}
else {
System.out.print(midPattern);
}
System.out.println();
}
}
}
Output (for input 3)
Please Enter a Number 3
* * *
* *
* * *
output (for input 7)
Please Enter a Number 7
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *

You need to add an extra star at the end of the middle for loop. This can be done by nested a second for loop of spaces followed by printing the star.
for (int i = 0; i < side; i++) {
System.out.print("*");
for (int j = 0; j < side; j++) {
System.out.print(" ");
}
System.out.println("*");
}

Related

Printing pattern using Jagged Array

Given the jagged Array, we are asked to use a looping statement to display the character based on the position. Display a "*" if the position matched or a " " if it doesn't.
int arr [][] = {{0,4,8,12,13,14,15,18,19,20,21,24,28},
{0,4,7,9,12,16,18,22,25,27},
{0,1,2,3,4,6,10,12,16,18,22,26},
{0,4,6,10,12,13,14,15,18,19,20,21,26},
{0,4,6,7,8,9,10,12,18,26},
{0,4,6,10,12,18,26}};
I have created a program, but the output is not what I expected and I am now stuck.
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[i].length - 1; j++)
{
for (int spaces = 1; spaces < arr[i][j + 1]-arr[i][j]; spaces++)
{
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
The output was suppose to be Happy but I get:
enter image description here
It's because of your program does not compare the first j value (which is 0) with itself. Since every value equals itself you can add manually * for each line like this.
int arr [][] = {{0,4,8,12,13,14,15,18,19,20,21,24,28},
{0,4,7,9,12,16,18,22,25,27},
{0,1,2,3,4,6,10,12,16,18,22,26},
{0,4,6,10,12,13,14,15,18,19,20,21,26},
{0,4,6,7,8,9,10,12,18,26},
{0,4,6,10,12,18,26}};
for (int i = 0; i < arr.length; i++)
{
System.out.print("*");
for (int j = 0; j < arr[i].length - 1; j++)
{
for (int spaces = 1; spaces < arr[i][j + 1]-arr[i][j]; spaces++)
{
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
Try this.
for (int i = 0; i < arr.length; i++) {
for (int j = 0, p = -1; j < arr[i].length; p = arr[i][j++])
System.out.print(" ".repeat(arr[i][j] - p - 1) + "*");
System.out.println();
}
output:
* * * **** **** * *
* * * * * * * * * *
***** * * * * * * *
* * * * **** **** *
* * ***** * * *
* * * * * * *

I am trying to make a pattern (triangle in java)

I am trying to make this pattern in java:
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
This is the code which I have written:
package practise;
import java.util.Scanner;
public class PatternsUsingLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int j=1;j<=n;j++) {
for(int k=n;k>=1;k--) {
System.out.print("* ");
}
System.out.println();
}
}
}
please tell me the error
There are many ways to do it also to fix this issue. One of the simplest fixes is to replace the inner loop with the following:
for (int k = (n - j) + 1; k >= 1; k--)
Since you want to print n number of *s in the first line, n - 1 number of *s in the second line and so on, the value of k must be initialized to n when j is 1, n - 1 when j is 2 and so on.
Some of the other ways to do this requirement are as follows:
A.
for (int j = n; j >= 1; j--) {
for (int k = j; k >= 1; k--) {
System.out.print("* ");
}
System.out.println();
}
B.
for (int j = n; j >= 1; j--) {
for (int k = 1; k <= j; k++) {
System.out.print("* ");
}
System.out.println();
}
...and many more
You don't decrease the finish condition in inner loop. This will works:
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int row=0;row<n;row++) {
for (int column = 0; column < n-row; column++) {
System.out.print("* ");
}
System.out.println();
}
The problem is that you haven't tried debugging your code.
To debug your code try printing out the variables that you are using at the beginning of each line to see if they are what you expect. You have (hopefully) thought about the code, and what you think each variable will be on each line, so you've missed something, you have a bug.
For example try this:
package practise;
import java.util.Scanner;
public class PatternsUsingLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int j=1;j<=n;j++) {
System.out.print("j: " + j + " n: " + n);
for(int k=n;k>=1;k--) {
System.out.print("* ");
}
System.out.println();
}
}
}
When you run that you get
j: 1 n: 5* * * * *
j: 2 n: 5* * * * *
j: 3 n: 5* * * * *
j: 4 n: 5* * * * *
j: 5 n: 5* * * * *
Which shows what a couple of the other answers are pointing out, that n doesn't change so the output is always 5 stars.
Now you can figure out the answer. (Hint: start j from 0 and use k=n-j)
Its a very small mistake that you are doing , the first loop is correct but in the inner loop you are printing n stars again (from n to 1).
You need to print (n - i + 1) stars for every loop.
Use this change :
...
...
int n = sc.nextInt();
for(int j=1;j<=n;j++) {
for(int k=n-j+1;k>=1;k--) {
System.out.print("* ");
}
System.out.println();
}
...
...
//... means same code //

java array to print square box of asterisk

I'm trying to write a square shape of asterisk around border using the https://stackoverflow.com/a/34209565 answer.
But I cannot get it working at all.
Here is the code I'm trying.
int _i = 10;
int _j = 10;
String[][] array = new String[_i][_j];
for (int i = 0; i < _i; i++) {
System.out.println();
for (int j = 0; j < _j; j++) {
if(i==0 || j == 0 || i == _i - 1|| j == _j - 1){
array[i][j] = "*";
System.out.print(array[i][j]);
}
}
}
The output I'm getting is:
**********
**
**
**
**
**
**
**
**
**********
I tried running tthe code from the answer but it produces a one line of asterisk. Something from the code in the answer has been omitted.
The code provdied in the answer is:
int _i = 10;
int _j = 10;
String[][] array = new String[_i][_j];
for (int i = 0; i < _i; i++) {
for (int j = 0; j < _j; j++) {
if(i==0 || j == 0 || i == _i-1|| j == _j-1){
array[i][j] = "*";
}
}
}
And the output in the answer is:
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********
You need an else to print a space when the entry is not a star. Also, I would move the println() to the end of the loop (instead of the beginning). Assuming you actually want to fill the array too, populate it with a space as well. Like,
for (int j = 0; j < _j; j++) {
if (i == 0 || j == 0 || i == _i - 1 || j == _j - 1) {
array[i][j] = "*";
} else {
array[i][j] = " ";
}
System.out.print(array[i][j]);
}
System.out.println();
Outputs
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********
You can use the repeat method, so the code is less verbose, also enclosing the generation in a method you can make it dynamic bu setting two arguments: number of lines and number of columns:
class Test
{
public static void main(String[] args)
{
System.out.println(createBox(10, 10));
System.out.println(createBox(10, 5));
}
public static String createBox(int qtaRig, int qtaCol)
{
String result = "";
for(int r = 1; r <= qtaRig; r++)
{
if(r == 1 || r == qtaRig)
{
result += "*".repeat(qtaCol);
}
else
{
result += "*" + " ".repeat(qtaCol-2) + "*";
}
result += "\n";
}
return result;
}
}
Result:
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********
*****
* *
* *
* *
* *
* *
* *
* *
* *
*****

Making a method that creates an image with X in square using stars

I am supposed to complete the inside of a method
public static void printXinSquare(int width) {
}
width being the number of rows so that it would create
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
I've tried making four separate pieces of triangles, and attach them somehow, but that didn't work.
I also tried to create a for loop for spaces and then add a for loop for the stars, but I'm confused and have no idea as to how to do that.
To do this, as I said before, I tried to make separate trianges.
public static void printXinSquare(int width) {
for (int i = 1; i <= width/2+1; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = width/2+2; i <= width; i++) {
for (int j = width+1-i; j >= 1; j--) {
System.out.print("*");
}
System.out.println();
}
System.out.println();
}
This creates a triangle pointing to the right.
public static void printXinSquare(int width) {
for (int line = width/2+1; line >1; line--) {
for (int i = 1; i <= (line - 1); i++) {
System.out.print(" ");
}
for (int i = 1; i <= (width - 2 * line); i++) {
System.out.print("*");
}
System.out.println();
}
This creates a triangle pointing upwards.
This is what I have tried so far, but I don't think this way works.
I think I should create for loops that take into account the spaces,
but I don't know how to do that as the spaces are in diagonal direction.
Any help to complete this method would be greatly appreciated :)
This should print in with an input width of 10 the exact picture you have in your question.
public static void printXinSquare(int width)
{
for (int k = 0; k < width; k++) {
for (int j = 0; j < width; j++) {
if (k == j || k == width - j - 1) {
System.out.print(" ");
}
else {
System.out.print("* ");
}
}
System.out.println();
}
}
It prints * with a blank space every time to ensure it is properly spaced out like that picture, and prints 2 spaces instead in two different cases, each of which account for a single diagonal line. The two cases are as follows:
if the row count is equal to the current column count, there should be a space instead of an asterisk. This will create the first diagonal line going from top left to bottom right.
if the row count is equal to the width minus the current column count minus 1, it also should be a space rather an asterisk. This will create the second diagonal line going the opposite direction.
Try something like this:
for (int i = 0; i < width; i++) {
for (int j = 0; j < width; j++) {
if (i == j || i+j == width) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println("");
}
In this case you would be printing '*' when it is not in the triangle, and ' ' otherwise. The inside triangle is the condition (i==j || i+j==width)

Pattern using looping

I have to make a loop that will read the user's input (let's say his input is 5) so the output should be as the following:-
*
**
***
****
*****
****
***
**
*
int x;
//input size of triangle from 1-20
Scanner scanner = new Scanner (System.in);
System.out.println("Enter size of triangle from 1 to 20: ");
x = scanner.nextInt ();
for( int i = 0; i <= x; i++){
for(int j=0; j < i; j++){
System.out.print( " *");
}
System.out.println(" ");
I just don't know how to finish it by decreasing the pattern so I can form a full triangle.
example using ternary operator .increase j if x grater than i decrease otherwise
int x = 5, j=0;
for (int i = 0; i <= x*2; i++) {
j= (x>i)? ++j:--j; // u can use if else also
for (int y = 0; y < j; y++) {
System.out.print(" *");
}
System.out.println("");
}
output>>
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
The following code prints the triangle, assuming the user enters 10
for (int i = 0; i < 10; i++){
for (int j = 0;j < i;j++ )
System.out.print('x');
System.out.println();
}
for (int i = 10; i > 0; i--){
for (int j = 0; j < i;j++)
System.out.print('x');
System.out.println();
}
In your code, you exclude the second for loop in which i is decremented.
import java.util.Scanner;
public class Triangle {
public static void printLine(int n) {
for(int i = 0; i < n; i++) System.out.print("*");
System.out.print("\n");
}
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
System.out.println("Enter size of triangle");
int x = scanner.nextInt ();
for(int i = 0; i < x; i++) {
printLine(i);
}
for(int i = x; i > 0; i--) {
printLine(i);
}
}
}
The output :
Enter size of triangle
5
*
**
***
****
*****
****
***
**
*

Categories

Resources