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
Related
I want to make a java pattern like this using while loops
* * * *
* * *
* *
*
and here is my code
int i = 4;
int j = 0;
while (i >= 0) {
while (j < i) {
System.out.print(" * ");
j++;
}
System.out.print("\n");
i--;
}
but its giving output like this:
* * * *
Does anyone knows what to do....?
TRY CONSIDERING THIS ONE MIGHT HELP YOU !!
Using FOR Loop
public class DownwardTrianglePattern {
public static void main(String[] args) {
int rows = 4;
//outer loop
for (int i = rows-1; i >= 0 ; i--) {
//inner loop
for (int j = 0; j <= i; j++) {
//prints star and space
System.out.print("*" + " ");
}
//throws the cursor in the next line after printing each line
System.out.println();
}
}
}
Using While Loop
public class Itriangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter N : ");
int n = sc.nextInt();
System.out.print("Enter Symbol : ");
char c = sc.next().charAt(0);
int i = n, j;
while(i > 0) {
j = 0;
while(j++ < i) {
System.out.print(c);
}
System.out.println();
i--;
}
}
}
After i--;
you can j=0;
The problem in your code is that in first iteration after completing both the while loops the value of variables is i=3 and j=4. So for the next iteration the first while loop will run as the condition is true i.e. 3>=0 but for the next while loop the condition will be false as j i.e. 3 not less than i i.e. 3. So that's why the next loop does not executes and you are getting that output.
The j needs to be initialized inside loop.
I changed the code as below. it's giving correct output
int i = 4;
while (i >= 0) {
int j = 0;
while (j < i) {
System.out.print(" * ");
j++;
}
System.out.print("\n");
i--;
}
please reset j after j loop
int i = 4;
int j = 0;
while (i >= 0) {
while (j < i) {
System.out.print(" * ");
j++;
}
j = 0;
System.out.print("\n");
i--;
}
when you're finished with inner loop you have to reset j value else it's value will remain 4 which is not less then i value. you can reset the value before starting inner while
int i = 4;
int j = 0;
while (i >= 0) {
j = 0;
while (j < i) {
System.out.print(" * ");
j++;
}
System.out.println(""); // println will work same as System.out.print("\n") in this scenario
i--;
}
By the way your problem can be nicely solved using recursion
public static void main(String[] args) {
printLine(4);
}
private static void printLine(int numberOfStars) {
if(numberOfStars == 0) {
return;
}
System.out.println("* ".repeat(numberOfStars));
printLine(numberOfStars - 1);
}
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
}
}
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();
}
}
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("*");
}
}
}
Hi there i want to create a triangle using java loops. But what i need is only the borders. I've tried using this syntax but it always shows error. any advice?
int lines = 5;
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
{
if (k == 0 || k == lines - 1) {
System.out.print("*");
}
}
}
System.out.print("\n");
c -= 2;
}
can you kindly help me . thanks
There's a lot of things wrong with your code...
for (int k = 1; k <= c; k++)
// and then:
if (k == 0 || k == lines - 1) {
k will never be 0
if (k % 2 == 0)
// and then
else
{
if (k == 0 || k == lines - 1) {
a even if k was 0, then 0%2 == 0, so k==0 can never occur
As of right now, your app only prints spaces.
If you want to draw a triangle, the (possibly) simplest way would be to do
public static void main(String[] args) {
int lines = 6;
int cols = lines+1;
for (int line =0; line < lines-1; line++) {
for (int col = 0; col<cols; col++) {
if (col == 0 || col == line) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for (int col=0; col<cols; col++) {
System.out.print("*");
}
System.out.println();
}
Triangle.java
public class Triangle {
private int lines;
public Triangle(int lines) {
this.lines = lines;
display();
}
public void display() {
int spaces = lines - 1;
int last;
int min = 1;
int max;
for (int i = 0; i < lines; i++) {
last = 2 * i + 1;
max = spaces + last;
for (int j = min; j <= max; j++) {
if (j == spaces + 1 || j == max || i == lines - 1)
System.out.print('*');
else
System.out.print(' ');
}
System.out.println();
spaces--;
}
}
public static void main(String[] args) {
new Triangle(3);
new Triangle(4);
new Triangle(5);
new Triangle(6);
}
}
Output
*
* *
*****
*
* *
* *
*******
*
* *
* *
* *
*********
*
* *
* *
* *
* *
***********
Ex:
*
* *
* *
* *
*********
Code:
int numberOfRows = 5;
for(int i=0; i<numberOfRows; i++)
{
for(int a=0; a<=numberOfRows; a++)
{
int x = numberOfRows - i;
if (a > x) {
if (x==(a-1) || i==(numberOfRows-1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
} else {
System.out.print(" ");
}
}
for(int b=0; b<=i; b++)
{
if(b==i || i==(numberOfRows-1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.print("\n");
}
I have used two methods and repeated those two methods using for loop.
public class OpenWord {
int i;
String k = " ";
public void space(){
System.out.println("*"+k+"*");
}
public void star(){
System.out.println("*");
}
public void trainagle(){
star();
for ( i=1; i<=10;i++){
space();
k =k+" ";
if(i==10){
k=k.replace(" ", "*");
space();
}
}
}
public static void main(String args[]) {
OpenWord a = new OpenWord();
a.trainagle();
}
}
Output:
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
******************