I am trying to make this pattern in java:
*
* * *
* * * * *
* * *
*
Here is what I have right now:
public static void main(String[] args) {
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(i<j){
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
}
}
Any help would be appreciated....
public static void main(String[] args) {
int size = 5;
for (int i = 0; i < size; i++) {
int width = Math.min(i, size - 1 - i) * 2 + 1;
for (int j = 0; j < width; j++) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) {
for(int i=0;i<5;i++){
int n = 1 + 2 * (2 - Math.abs(2 - i));
for (int j = 0; j < n; j++)
System.out.print("* ");
System.out.println();
}
}
You have done very well so far.
Now all you have to do is calculate the number of "needed" spaces to put in front of each string. After adding them, you will have the "full" solution.
It may work in this way also.
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int b=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print("*");
if(j!=i){
System.out.print(" ");
}
}
System.out.println();
}
for(int i=n-1;i>=1;i--){
for(int j=1;j<=i;j++){
System.out.print("*");
if(j!=i){
System.out.print(" ");
}
}
System.out.println();
}
Related
I am working on a simple star pattern program in Java. I have the code running but it is not doing what it's supposed to. My code is:
public class q3 {
public static void main(String[] args) {
for (int i = 10; i >= 1; i--){
for (int j = i; j >= 1; j--){
System.out.print("*");
}
System.out.println("");
}
}
}
Output:
$$$$$$$$$$
$$$$$$$$$
$$$$$$$$
$$$$$$$
$$$$$$
$$$$$
$$$$
$$$
$$
$
What I want is something like this below:
$
$$
$$$
$$$$
$$$$$
..........
$$$$$$$$$$
Can someone please help me figure out how I would get the above pattern. Thank You.
Try this :
public static void main(String[] args) {
final int length = 10;
for (int i = 1; i < length; i++) {
//Print spaces first
for (int j = length - 1; j > i; j--) {
System.out.print(" ");
}
//Then print "*"
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
Output is for length = 10 :
*
**
***
****
*****
******
*******
********
We can achieve this pattern using a single for loop:
char [] mirrorascval = new char[5];
for (int i = 4; i >= 0; i--) {
mirrorascval[i] ='*';
System.out.println(mirrorascval);
}
class StarTriangle {
public static void main(String[] args) {
int i, j, k;
for (i = 8; i >= 1; i--) {
for (j = 1; j < i; j++) {
System.out.print(" ");
}
for (k = 8; k >= i; k--) {
System.out.print("*");
}
System.out.println();
}
}
}
public void starPrint(){
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++) {
System.out.print("*");
}
System.out.println();
}
}
import java.util.*;
public class firstStar {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner S=new Scanner(System.in);
int n=S.nextInt();
int i=1;
while(i<=n) {
int j=1;
while(j<=i) {
System.out.print("*");
j=j+1;
}
System.out.println();
i=i+1;
}
}
}
package star.pattern;
import java.util.Scanner;
public class StarPattern {
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
int l;
System.out.print("Enter No Of Line You Want:- ");
l=scn.nextInt();
int x=1;
while(x<=l){
int j=x;
while(j<l){
System.out.print(" ");
j++;
}
int y=1;
while(y<=x){
System.out.print("*");
y++;
}
System.out.println("");
x++;
}
}
}
public class q3 {
public static void main(String[] args) {
for (int i = 1; i <=10; i++){
for (int j = 10; j >= i; j--){
System.out.print("*");
}
System.out.println("");
}
}
}
Try this...
int i, j, k=8;
for(i=0; i<5; i++)
{
for(j=0; j<k; j++)
{
System.out.print(" ");
}
k = k - 2;
for(j=0; j<=i; j++)
{
System.out.print("* ");
}
System.out.println();
}
I am trying to write code using a for loop to print a pyramid of asterisks. The height of the pyramid should be determined by the user input and should look like:
ex: input of 3
*
**
***
**
*
import java.util.Scanner;
public class Homework6_Project2 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int triangleHeight = keyboard.nextInt();
int i;
int j;
for (i = triangleHeight; i >= 1; i--) {
for (j = i; j >= 1; j--) {
System.out.print("*");
}
System.out.println();
}
}
}
edit: I was forgetting the code for the bottom half where I needed to use the ++ increment operator.
You need another looping to print the upper pyramids.
Something like:
for(i = 1; i < triangleHeight; i++) {
for (j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
You can check this code which gave me the following output for input 3:
*
**
***
**
*
public class PyramidOfAsterisk {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
java.util.Scanner keyboard = new java.util.Scanner(System.in);
int pyramidHeight = keyboard.nextInt();
for(int i = 1; i <= pyramidHeight; i++){
for(int j=1; j <= i; j++){
System.out.print('*');
}
System.out.println();
}
for(int i = pyramidHeight - 1; i > 0; i--){
for(int j=i; j > 0; j--){
System.out.print('*');
}
System.out.println();
}
}
I am trying to make a christmas tree using for loops and nested for loops. For me to do that I need to be able to make a pyramids with *. I have tried countless times and I am having problems making one. Here is my code:
for(int i=1;i<=10;i++){
for(int j=10;j>i;j--){
System.out.println(" ");
}
for(int k=1;k<=i;k++){
System.out.print("*");
}
for(int l=10;l<=1;l++){
for(int h=1;h<=10;h++){
System.out.print(" ");
}
}
System.out.println();
}
What I am trying to do is:
*
***
*****
*******
Try this much simpler code:
public class ChristmasTree {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10 - i; j++)
System.out.print(" ");
for (int k = 0; k < (2 * i + 1); k++)
System.out.print("*");
System.out.println();
}
}
}
It uses 3 loops:
first one for the number of rows,
second one for printing the spaces,
third one for printing the asterisks.
You can do it with simple logic
for (int i = 0; i < 4; i++)
System.out.println(" *******".substring(i, 4 + 2*i));
import java.util.Scanner;
public class cmastree{
public static void main (String[]args){
Scanner keyboard=new Scanner (System.in);
int j;
System.out.println ("Enter a number");
j=keyboard.nextInt();
/*take the above part out and change the j variable if you want to set
the size*/
for(int i=1; i<=j; i+=2){
int numSpaces = (j-i)/2;
for (int k=0; k<numSpaces; k++){
System.out.print(" ");
}
for(int k=0; k<numSpaces; k++){
System.out.print("*");
}
System.out.println();
}
}
}
public class ChrismasTree {
public static void main(String[] args) {
int sizeOfTree = 9;
double remainderVal = sizeOfTree % 2 ;
double ans = sizeOfTree / 2 ;
if (remainderVal == 0) {
System.out.println("Invalid number enter 9,19 calculat rest yourself u looser ..");
System.exit(0);
}
int middlePos = (int) Math.round(ans + .5);
for (int i = 0; i <= sizeOfTree; i++) {
int lStar = middlePos - i;
int rStar = middlePos + i;
if (lStar < 1) {
break;
}
printleaves(lStar, rStar, sizeOfTree);
}
}
public static void printleaves(int a,int b, int size){
System.out.println();
for (int i = 1; i <= size; i++) {
if (i > a && i < b ){
System.out.print("*");
}else System.out.print(" ");
}
}
}
public class Stars {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Enter Row/Column Value::");
int i,j,k,n;
n=s.nextInt();
for(i=1; i<n; i++){
for(j=n+(n/2); j>i; j--){
System.out.print(" ");}
for(k=1; k<=2*i-1; k++){
System.out.print("*");}
System.out.println("");
}
for(i=1; i<n+(n/2); i++){
for(j=n+(n/2); j>i; j--){
System.out.print(" ");}
for(k=1; k<=2*i-1; k++){
System.out.print("*");}
System.out.println("");
}
for(i=1; i<n-(n/2); i++){
for(j=n+(n/2); j>1; j--){
System.out.print(" ");}
for(k=n/2; k<=(n/2)+1; k++){
System.out.print("*");}
System.out.println("");
}
}
}
public class ChristmasTree {
public static void printStars(int number) {
for (int i = 1; i <= number; i++) {
System.out.print("*");
}
System.out.println("");
}
public static void printSpaces(int number) {
for (int i = 0; i < number; i++) {
System.out.print(" ");
}
}
public static void christmasTree(int height) {
for (int i = 1; i <= height; i++) {
printSpaces(height - i);
printStars(i + (i - 1));
}
}
public static void main(String[] args) {
// int x = pick some number, but not TOO big )))
christmasTree(x);
}
}
def fist(n)
k=2*n-2
for i in range(0,n):
for j in range(0,k):
k=k-1
print(end=" ')
for j in range(0,i+1):
print("*",end=" ")
print()
def second(n)
k=2*n-2
for i in range(0,n):
for j in range(0,k):
k=k-1
print(end=" ')
for j in range(0,i+1):
print("*",end=" ")
print()
def stem(m)
k=11
for i in range(0,5):
for j in range(0,k):
print(end=" ")
for j in range(0,3):
print("*",end=" ")
print()
first(7)
second(7)
steam(3)
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.
//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("*");
}
}
}