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)
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();
}
public class Xmas{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("program.");
System.out.print("Height: ");
int n = sc.nextInt();
if (n <= 0) {
System.out.println("Height can only be positive.");
}
else {
System.out.print("Levels: ");
int sz = sc.nextInt();
if (sz <= 0) {
System.out.println("Levels can only be positive.");
}
else
for (int s = 0; s < sz; s++) {
for(int i=0; i<n; i++) {
for(int j=0; j<n-i-1; j++) {
System.out.print(" ");
}
for(int k=0; k<=2*i; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
}
}
I need this for the last tree:
I have to do a 3*3 strut in the middle of the tree.
*
***
*****
*******
*********
***
***
***
I have no idea how to do that.
please help me and give me a code! Thanks!
The code below is maybe that what you are searching for. The additional loop prints the asterisks depending on the height. Three lines with 3 asterisks.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("program.");
System.out.print("Height: ");
int n = sc.nextInt();
if (n <= 0) {
System.out.println("Height can only be positive.");
} else {
System.out.print("Levels: ");
int sz = sc.nextInt();
if (sz <= 0) {
System.out.println("Levels can only be positive.");
} else {
for (int s = 0; s < sz; s++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
for (int k = 0; k <= 2 * i; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < n - 1; j++) {
if (j == n - 2) {
System.out.println("***");
continue;
}
System.out.print(" ");
}
}
}
}
}
I feel like the code I wrote is wrong but I don't know why. looks very unprofessional to me.
/* 1234
2341
3412
4123
*/
public class pattern{
public static void main(String args[]){
for(i=1; i<=4; i++)
{for(j=1; j<=4; j++)
{System.out.print(i);
}
System.out.println();
while(i>4)
{ int i= 1;
i++;
System.out.print(i);}
System.out.println();
}
It's hard to tell what your asking for exactly, but—judging from the comment above your class—you're probably looking for something like this:
for (int i = 0; i < 4; i++) {
for (int j = i; j < i + 4; j++) {
System.out.print((j % 4) + 1);
}
System.out.println();
}
It goes without saying, but I have to; you should always try to follow the Java naming/formatting standards.
Take care at these:
for(int i=1; i<=4; i++)
{for(int j=1; j<=4; j++)
System.out.println();
You should initialize the variables i and j and System not system
And the parameter in main method should be like String args:
public static void main(String args[]){
Here is your code in nice format:
public static void main(String args[]) {
for (int i = 0; i < 4; i++) {
for (int j = i; j < i + 4; j++) {
System.out.print((j % 4) + 1);
}
System.out.println();
}
}
for(int i=1;i<=4;i++) {
for(int j=1;j<=4;j++) {
int k=i+j-1;
if(k>4) {
System.out.print(k-4 + " ");
}
else {
System.out.print(k + " ");
}
}
System.out.print(" ");
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();
}
I have to code a program to print this output:
1
212
32123
4321234
543212345
I have successfully coded this portion of the pattern:
1
12
123
1234
12345
However, I am not reaching the second portion. Here's my code:
for(int i=1; i<=5; i++) {
for(int j=1; j<=i; j++) {
System.out.print(j);
}
System.out.println();
}
Why not recursive? Just because it's fun ;)
public static void main(String args[]) {
System.out.println(pyramid(5));
}
public static String pyramid(int rank) {
if (rank == 1) {
return "1\n";
}
return pyramid(rank - 1) + mirror(rank) + "\n";
}
public static String mirror(int rank) {
if (rank == 1) {
return "1";
} else {
return rank + mirror(rank - 1) + rank;
}
}
You just need another j for loop before your current j for loop that counts down from 5 to (but not including) 1. Decide whether to print a space or j itself, depending on if j is greater than i.
for (int j=5; j > 1; j--) {
if (j > i)
System.out.print(' ');
else
System.out.print(j);
}
package recAaA;
public class testA {
static void rec(int startVal, int endVal)
{
if(startVal==0)startVal=-2;
if(startVal<-endVal) return;
System.out.print(Math.abs(startVal));
rec(startVal-1,endVal);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int imax=5;
for(int i=1;i<imax+1;i++)
{
for(int j=0;j<imax+1-i;j++)
{
System.out.print(" ");
}
rec(i,i);
System.out.println();
}
}
}
Output:
1
212
32123
4321234
543212345
if you give i to startVal and i+1 to endVal, the output becomes
12
2123
321234
43212345
5432123456
As someone already noted, you can accomplish this easily with a double for-loop, one counting down from i, one counting up:
for(int i=1; i<=5; i++) {
for(int j=i; j>=2; j--) {
System.out.print(j);
}
for(int j=1; j<=i; j++) {
System.out.print(j);
}
System.out.println();
}
You can also accomplish the same thing in a single for loop if you don't print right away. For example you can build an StringBuffer with one, then add the numbers on either side until i==j, and then print outside of the inner loop.
for(int i=1; i<=5; i++) {
StringBuffer buffer = new StringBuffer();
buffer.append(1);
for(int j=2; j<=i; j++) {
buffer.insert(0, j);
buffer.append(j);
}
System.out.println(buffer);
}
Here's code that outputs the numbers; all you have to do is fix the spacing.
for (int i = 1; i <= 9; i++) {
BigInteger b =
BigInteger.TEN.pow(2*i-1)
.subtract(BigInteger.ONE)
.divide(BigInteger.valueOf(9))
.multiply(BigInteger.valueOf(i+1))
.subtract(BigInteger.TEN.pow(i)
.subtract(BigInteger.ONE)
.divide(BigInteger.valueOf(9))
.pow(2));
System.out.println(b);
}
That is, for each integer in the range 1-9, it prints ((102n-1-1)/9)(n+1)-((10n-1)/9)2. Very simple.
/* Basically logic is that in a horizontal line upto the mid ,the number is decrementing and after the mid, the number starts incrementing
*/
public class java {
public static void main(String[] args) throws IOException {
int n;
InputStreamReader io = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(io);
System.out.println("Enter the height in the number of lines vertically");
n = Integer.parseInt(br.readLine());
for (int i = 1; i <= n; i++) {
int k = i;
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print(k);
if (j >= (((2 * i - 1) / 2) + 1))
k++;
else if(j<(2*i-1)/2+1) k--;
}
System.out.println();
}
}
}
I hope this will help !
import java.util.*;
public class test {
public static void main(String[] args) {
int i,j,k,l,n,a;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
a = n;
for(i=1 ; i<=n ; i++){
for(j=a ; j>1 ; j--){
System.out.print(" ");
}
for(k=i ; k!=0; k--){
System.out.print(k);
}
a--;
for(l=2; l<=i ; l++){
System.out.print(l);
}
System.out.println(" ");
}
}
}
for(int i=1;i<=100; i++) {
for(int j=100;j>i;j--)
System.out.print(" ");
for(int k=i;k>1;k--)
System.out.print(k+" ");
for(int j=1; j<=i; j++)
System.out.print(j+" ");
System.out.println();
}
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.print("Enter no ");
int n=in.nextInt();
for(int i=1;i<=n;i++)
{
int l=n-i;
while(l!=0)
{
System.out.print(" ");
l--;
}
for(int j=i;j>=2;j--)
{
System.out.print(j);
}
for(int k=1;k<=i;k++)
{
System.out.print(k);
}
System.out.println();
}
}
}