How to print this using loops only - java

I just want to print this in simple java program using loops only.
----*----
---*-*---
--*-*-*--
-*-*-*-*-
*-*-*-*-*

Break the problem in small steps.
Loops for printing correct rectangle without patterns
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 9; j++) {
System.out.print("-");
}
System.out.println();
}
This would give following output.
---------
---------
---------
---------
---------
Create pattern using if condition.
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 9; j++) {
if ((j - i) % 2 == 0) { // Condition for alternate stars
System.out.print("*");
} else {
System.out.print("-");
}
}
System.out.println();
}
This would give following output.
*-*-*-*-*
-*-*-*-*-
*-*-*-*-*
-*-*-*-*-
*-*-*-*-*
Restrict pattern inside the triangular shape.
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 9; j++) {
if (i + j > 3 && j - i < 5) { // Equations of straight lines in triangle
if ((j - i) % 2 == 0) { // Condition for alternate stars
System.out.print("*");
} else {
System.out.print("-");
}
} else {
System.out.print("-");
}
}
System.out.println();
}
This would give following output.
----*----
---*-*---
--*-*-*--
-*-*-*-*-
*-*-*-*-*
Refactor for condensed conditions
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 9; j++) {
if (i + j > 3 && j - i < 5 && (j - i) % 2 == 0) {
System.out.print("*");
} else {
System.out.print("-");
}
}
System.out.println();
}
This would give following output.
----*----
---*-*---
--*-*-*--
-*-*-*-*-
*-*-*-*-*
Hope this helps.
Good luck.

try this :D
public static void main(String[] argu) {
int index=8;
for(int i=0;i<5;i++) {
for(int j=index;j>0;j--) {
System.out.print("-");
}
index--;
if(i==4) {
System.out.print("*");
}else {
System.out.print(" ");
}
}
}

public static void main(String[] args){
for(int i=5;i>=1;i--){
int otherSide = 10-i;
int printStar = -1;
for(int j=1;j<=9;j++){
if(j>=i && j<=otherSide){
printStar*=-1;
if(printStar==1) System.out.print("*");
else System.out.print("-");
}
else
System.out.print("-");
}
System.out.println("");
}
}
Just find the start index of the * and the part where it is last printed. Print stars alternatingly inside the range.

Related

Can this problem be solved using nested loops?

Would it be possible to produce this layout with nested loops? I'm still new to nested to Java/loops and cannot solve this issue.
*****====+
*****===++
*****==+++
*****=++++
*****+++++
====++++++
===+++++++
==++++++++
=+++++++++
I'm having trouble looping through five times with the "*" character without allowing the "+" to increment.
Here is my code:
class Main {
public static void main(String[] args) {
for (int k = 4; k > 0; k--) {
System.out.print("*****");
for (int l = 0; l < k; l++) {
System.out.print("=");
}
for (int m = 0; m < 1; m++) {
for (int n = 0; n < m; n++) {
System.out.print("+");
}
}
System.out.println();
}
System.out.print("*****+++++");
}
}
there are could be multiple approaches to this problem, here is how I would think about it:
you need to display 9 lines of "something", so lets have top level loop:
for (int i=0; i<9; i++) {...}
now, each iteration of this loop you need to display X stars, Y equal signs, Z plus signs:
for (int i=0; i<9; i++) {
for (int j=0; j< #X# ; j++) System.out.print("*");
for (int j=0; j< #Y# ; j++) System.out.print("=");
for (int j=0; j< #Z# ; j++) System.out.print("+");
System.out.println();
}
now you need to determine rules how X,Y,Z are changed, here is the logic I came up with:
if (stars > 0 && equals == 0) {
stars = 0;
equals = 5;
}
equals--;
pluses++;
so, final code will look like:
public static void main(String[] args) throws InterruptedException {
int stars = 5; // initial state
int equals = 4;
int pluses = 1;
for (int i=0; i<9; i++) {
for (int j=0; j<stars; j++) System.out.print("*");
for (int j=0; j<equals; j++) System.out.print("=");
for (int j=0; j<pluses; j++) System.out.print("+");
System.out.println();
if (stars > 0 && equals == 0) {
stars = 0;
equals = 5;
}
equals--;
pluses++;
}
}
If you notice there is a pattern.
It prints a * starting from left to right.
It prints a + for each increasing number starting from right to left.
It prints a = instead of * if you are past mid point (first the mid point of left to right, then the mid point of top to bottom).
The logic can be applied as following,
class Main {
public static void main(String[] args) {
int max = 10;
int switchPoint = max - 1;
for(int i = 1; i <= max -1; i++) {
for(int j = 1; j <= max; j++) {
if(j > switchPoint)
System.out.print("+");
else if( i > max/2 || j > max / 2)
System.out.print("=");
else
System.out.print("*");
}
switchPoint--;
System.out.println();
}
}
}
/* Output:
*****====+
*****===++
*****==+++
*****=++++
*****+++++
====++++++
===+++++++
==++++++++
=+++++++++
*/
As you may notice the behavior of the "*" and "=" is different in the first five lines than in the next five lines, so you may either divide the loop in two loops or make a single one and check wether you are printing the first five lines or the last ones, that check may be done either by an if statement or in the loops conditions.
So the code will look like:
class Main {
public static void main(String[] args) {
for (int i = 1; i < 10; i++){
for (int j = 1; i <= 5 && j <= 5; j++){
System.out.print("*");
}
for (int k = 5; (i <= 5 && k > i) || (i > 5 && k > i-5); k--){
System.out.print("=");
}
for (int l = 1; l <= i; l++){
System.out.print("+");
}
System.out.println();
}
}
}
public class A {
public static void main(String[] args) {
for (int k = 4; k > 0; k--) {
System.out.print("*****");
for (int l = 0; l < k; l++) {
System.out.print("=");
}
for (int m = 0; m < 5 - k; m++) {
System.out.print("+");
}
System.out.println();
}
System.out.print("*****+++++");
System.out.println();
for (int s = 4; s > 0; s--) {
for (int k = 0; k < s; k++) {
System.out.print("=");
}
for (int l = 0; l < 5; l++) {
System.out.print("+");
}
for (int m = 0; m < 5 - s; m++) {
System.out.print("+");
}
System.out.println();
}
}
}

Print pyramid of * in Java

I'm wondering if you could help me out. I'm trying to write a nested for loop in java that displays a number pyramid triangle that looks like
___________*#
__________*_*#
_________*___*#
________*_____*#
_______*_______*#
______*_________*#
_____*___________*#
____*_____________*#
___*_______________*#
__*_________________*#
_*___________________*#
***********************#
This is what I have so far:
class Triagle {
public static void printTriagle(int n) {
for (int i = 0; i < n; i++) {
for (int j = n - i; j > 1; j--) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
// printing stars
System.out.print("* ");
}
System.out.println();
}
}
public static void main(String[] args) {
printTriagle(12);//I want to set the triangle to be height of 12
}
}
My result is not equal to the expected output:
___________*#
__________*_*#
_________*_*_*#
________*_*_*_*#
_______*_*_*_*_*#
______*_*_*_*_*_*#
_____*_*_*_*_*_*_*#
____*_*_*_*_*_*_*_*#
___*_*_*_*_*_*_*_*_*#
__*_*_*_*_*_*_*_*_*_*#
_*_*_*_*_*_*_*_*_*_*_*#
*_*_*_*_*_*_*_*_*_*_*_*#
I have updated your code and added comments so that you can understand. Refer to the code below:
public static void printTriagle(int n) {
for (int i = 0; i < n; i++) {
for (int j = n - i; j > 1; j--) {
System.out.print("_");
}
String s = "_";
if (i + 1 >= n) // check if it is the last line
s = "*"; // change to print * instead of _
for (int j = 0; j <= i; j++) {
// printing stars
if (j == i)
System.out.print("*#"); // check if the last print of the line
else if (j == 0)
System.out.print("*" + s); // check if the first print of the line
else
System.out.print(s + s);
}
System.out.println();
}
}
Result:
___________*#
__________*_*#
_________*___*#
________*_____*#
_______*_______*#
______*_________*#
_____*___________*#
____*_____________*#
___*_______________*#
__*_________________*#
_*___________________*#
***********************#
Try this
public static void printTriagle(int n) {
for (int i = 0; i < n; i++) {
for (int j = n - i; j > 1; j--) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
// printing stars
if(i == (n-1)){
System.out.print("**");
}
else{
System.out.print((j == 0 || j == i) ? "* " : " ");
}
}
System.out.println();
}
}
Your issue is here:
for (int j=0; j<=i; j++){
// printing stars
System.out.print("* ");
}
Here, it prints a star for each number between 0 and i, but it only should print a star if it is exactly 0 or i.
Try something like this:
for (int j=0; j<=i; j++){
if ( i == n ) {
System.out.print("* ");
} else {
System.out.print(j == 0 || j == i ? "* " : " ");
}
}
EDIT: You may still have to adapt your code to get the bottom line printed correctly, in case that line has to be all stars
This is what you need to do:
public static void printTriagle(int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < 2*n; j++) {
if(i == n-1) {
System.out.print((j != 2*n-1) ? "*" : "#");
}
else {
if(i+j == n-1) {
if(i == 0) {
System.out.print("*#");
break;
}
else {
System.out.print("*");
}
}
else if(j-i == n-1) {
System.out.print("*#"); break;
}
else {
System.out.print("_");
}
}
}
System.out.println();
}

replace every second line of my triangle with a blank line for loops

instead of getting a full triangle with 5 lines of "*" how do I replace every second line with a blank line, e.g. to look like this
5
555
55555
This is my code:
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print("5");
}
System.out.println();
}
you need small modification in your code and you can get what you want.
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
if(i%2==0)
System.out.print("*");
}
System.out.println();
}
check it out.
Just test for i % 2
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print(i % 2 == 0 ? "*" : " ");
}
System.out.println();
}
edit
If you want to print 5 instead or * then modify the code to use 5
Check this:
int num= 5; // 'num' denotes the number of lines
int numOfSpaces;
for(int i = 1; i <= num; i++) {
if( i%2 != 0) // checks whether the line number is odd. If odd, prints '*'
{
numOfSpaces = (num-i) / 2; // find the number of blank spaces
for(int j = 1; j <= numOfSpaces; j++) {
System.out.print(" ");
}
for(int k = 1; k <= i; k++) { // number of '*'s to be printed = current line number = i
System.out.print("*");
}
System.out.println();
}
else{
System.out.println(); // leaves every second line blank (ie.,if line number is even.)
}
}
//try this code. It may help you to achieve your output
[Check screen shot for the output of the below code][1]
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5 - i; j++)
{
System.out.print(" ");
}
for (int k = 0; k <= i; k++)
{
if(i%2!=0)
{
System.out.print(" ");
}
else{
System.out.print("* ");
}
}
System.out.println();
}
The Code below is Working now.
A simple if condition will solve your problem. Just check if the
value of i is divisible by 2. If it is true then print a
blank line. Else Print * and you are done!
* will be printed on 1st, 3rd and 5th lines.
Blank Space will be printed on 2nd and 4th lines.
public class PrintPattern{
public static void main(String []args){
int lineNo=0; // New Variable for Line Number (Can Use variable 'i' also)
for(int i = 0; i < 5; i++) {
lineNo++;
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
if(lineNo%2!=0){
for(int k = 0; k <= (i*2); k++)
System.out.print('*'); // Will print '*' (or '5') Sequence for Odd Lines - 1st, 3rd, 5th Lines
System.out.println();
}
else
System.out.println();// Will print Blank Line for Even Lines - 2nd and 4th Lines
}
}
}
Try this
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print("5"); //Use 5 instaed of * to draw
}
System.out.println();
System.out.println(); //This one will solve your problem
}
Output will be:
5
555
55555
5555555
555555555
I think that you can just enter one more print statement inside the loop:
for(int i = 0; i < 5; i++) {
System.out.println();
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print("*");
}
System.out.println();
}
Output
*
***
*****
*******
*********

JAVA Numberline errors

I am trying to make the following number line is Java
2,3,5,7,11,13,17 (Prime Numbers)
I tried this code
for(int i =0; i <= 100; i++) {
if(i < 2) {
continue;
}
for(int j = 2; j < 1; j++) {
if(i % j == 0) {
break;
} else {
System.out.print(i + ",");
}
}
}
But it doesn't work
Anyone help please?
Your code is quite poor, but the minimal amount of changes needed to make it work yields this code:
outerLoop:
for(int i = 0; i <= 100; i++) {
if(i < 2) {
continue;
}
for(int j = 2; j < i; j++) {
if(i % j == 0) {
continue outerLoop;
}
}
System.out.print(i + ",");
}
But a further improvement would be to start the first loop at 2 right away:
outerLoop:
for(int i = 2; i <= 100; i++) {
for(int j = 2; j < i; j++) {
// and so on...
EDITED
There are a lot of errors in that code, first of all that second loop is a infinite loop and second one that the System.out.println line should not be in second loop it should be at end of first loop! If you place it in second it will print numbers hundreds of time.
This is the correct code :
for(int i = 2; i <= 100; i++)//begin loop from 2 instead of 0
{
boolean flag = true;
for(int j = 2; j < i; j++)
{
if(i % j == 0)
{
flag = false;
break;
}
}
if(flag)System.out.print(i + ",");
}
You need to set a flag to check if a factor was found outside the loop.

Creating a double mirrored triangle

I need help making a mirrored triangle like this:
* *
** **
*** ***
********
I can get each one seperatly, but I can't combine them.
public static void main(String[] args){
for( int i = 1; i <= 5; i++ ){
for( int j = 0; j < i; j++ ){
System.out.print("*");
}
System.out.println();
}
for(int i = 0; i < 6; i++)
{
for(int j = 5; j > 0; j--)
{
if(i < j)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
}
You need to track the position from both sides to be able to show * at correct location. Here is the solution:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 10; j++) {
if (j <= i || j > 10 - i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
You can do it using this code.
public class Test {
public void sizeOfTri(int triSize) { //Number of lines
int line = 1;
for(int i = 0; i < triSize; i++) { //Handles Each line
int temp = triSize * 2;
for(int j = 1; j < temp; j++) { //Handles Each space on the line
if(j <= line && j == triSize || j >= temp - line && j == triSize) { //For the very last line because it needs an extra *
System.out.print("**");
} else if(j <= line || j >= temp - line) { //For normal lines
System.out.print("*");
} else if(j == triSize) {
System.out.print(" "); //For the second to last line because it needs an extra space to make it look mirrored
} else { //For normal lines
System.out.print(" ");
}
}
System.out.println();
line++;
}
}
public static void main(String[] args) {
new Test().sizeOfTri(4);
}
}
I commented on next to if statements on which part does what. This will produce an output which looks like the below when run
* *
** **
*** ***
********
Although, all the above implementations are really good ones. I thought of doing it bit differently and make use of 2-D arrays.
package algorithms;
public class DrawMirror {
static void initialize(String[][] array){
for (int i=0; i<MAX_X; i++){
for (int j=0; j < MAX_Y; j++){
array[i][j] = " ";
}
}
}
static void draw(String[][] array, int x, int y){
for (int i=0; i < y; i++){
array[x][i] = "*";
array[x][MAX_Y-i-1] = "*";
}
}
final static int MAX_X = 4;
final static int MAX_Y = 8;
public static void main(String[] args) {
String[][] array = new String[MAX_X][MAX_Y];
initialize(array);
for (int i=0; i < MAX_X ; i++){
draw(array,i,i+1);
}
for( int i = 0; i < MAX_X; i++ ){
for( int j = 0; j < MAX_Y; j++ ){
System.out.print(array[i][j]);
}
System.out.println();
}
}
}
The following code is a function with variable height.
public static void printDoubleTriangle(int height) {
for(int i = 0; i < height; i++) {
for(int j = 0; j < 2*height; j++) {
if(j <= i || (2*height - j - 1) <= i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}

Categories

Resources