Nested Loops Patterns - java

So yeah, I'm now working on nested loops but I think I'm stuck on somewhere because my desired output is:
*
**
***
**
*
And here's my code:
//intro here
int x = 0;
System.out.print("Enter: ");
x = var.nextInt();
for(int a = 1; a <= x; a++){
for(int b = 0; b <= x - a; b++){
System.out.print("");
}
for(int c = 0; c < a; c++){
System.out.print("*");
}
System.out.println();
}
}
}
but the output happening was:
*
**
***
I don't know now what to do, does it need another for with a d-- or something?

Yes. You need another for loop for print decreasing number of stars. This should work well!
import java.util.Scanner;
public class Nested {
public static void main(String[] args) {
Scanner var = new Scanner(System.in);
int x = 0;
System.out.print("Enter: ");
x = var.nextInt();
//create star rows from 1 to 6
for (int a = 1; a <= x; a++) {
for (int b = 1; b <= a; b++) {
System.out.print("*");
}
System.out.println();
}
//create start rows from 5 to 1
for (int a = x - 1; a > 0; a--) {
for (int b = a; b > 0; b--) {
System.out.print("*");
}
System.out.println();
}
}
}

int x = 0;
System.out.print("Enter: ");
x = var.nextInt();
for(int a = 1; a <= x; a++){
for(int b = 0; b <= x - a; b++){
System.out.print(""); //this code block do nothing in ur case
}
for(int c = 0; c < a; c++){
System.out.print("*");
}
System.out.println();
}
}
you can achieve the desired output by:
int x = 0;
System.out.print("Enter: ");
x = var.nextInt();
for(int a = 1; a <= x; a++){
for(int c = 0; c < a; c++){
System.out.print("*");
}
System.out.println();
}
for (int a = x - 1; a > 0; a--){
for(int c = a; c > 0; c--)
System.out.print("*");
System.out.print(""\n");
}

Related

How to Flip the triangle?

How to flip this triangle?
So i was making aritmethic sequance triangle. It was upside down.
How do I turn it 180 degree?
for example:
1=1
1+2=3
1+2+3=6
etc...
my code:
package javaapplication4;
public class NewClass5 {
public static void main(String[] args) {
int i=5,a;
for(int j=i; j>=1; j--) {
for(a=1; a<=i; a++)
System.out.print(a +" + ");
int n = 0;
for(a = 1; a<=i; a++) {
n = n + a;
}
System.out.print(" = "+ n);
System.out.println();
i--;
}
}
}
You can do it for any n, by getting input from the user
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
int add = 0;
for (int j = 1; j <= i; j++) {
System.out.print(j);
if (j == i)
System.out.print("=");
else
System.out.print("+");
add += j;
}
System.out.println(add);
}
I think you just need to change the sequence of loop from descending to ascending, rest of the things should be the same:
for (int i = 1; i <= 5; i++) {
int sum = 0;
for (int j = 1; j <= i; j++) {
System.out.print(j);
if (j == i)
System.out.print("=");
else
System.out.print("+");
sum += j;
}
System.out.println(sum);
}
When I run this, I'm able to see expected output:
src : $ java NewClass5
1=1
1+2=3
1+2+3=6
1+2+3+4=10
1+2+3+4+5=15

Creating this pattern in Java? (My loop isn't working)

I'm trying to create a program that depends on user input for integers between 2 and 10.
If the user entered four, this should be the output:
****
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
****
I want an input of 4 to output four stars in the first line to make a horizontal edge, and then a 4-star diagonal. Then four stars along a vertical “edge”, before repeating the diagonal and horizontal edge.
So I can draw the first line, last and middle lines right but my diagonals won't even show the spaces for some reason!
****
**
**
**
**
* *
* *
* *
* *
**
**
**
**
****
This is my code (I'm a beginner but I've been trying to fix this a lot and I really need some help):
int num = 0;
System.out.println("Enter a value between 2 and 10.");
num = keyNum.nextInt();
while (num < 2 || num > 10) {
System.out.println("Enter a valid number please.");
num = keyNum.nextInt();
}
for (int a = 0; a < num + 1; a++)
{
System.out.print(" ");
}
for (int b = 0; b < num; b++)
{
System.out.print("*");
}
for (int c = 0; c < num; c++)
{
System.out.println("");
for (int d = num; d < 1; d--)
{
System.out.print(" ");
}
System.out.print("*");
for (int e = (num * 3) - 2; e < num; e++)
{
System.out.print(" ");
}
System.out.print("*");
}
for (int f = 0; f < num; f++)
{
System.out.println("");
System.out.print("*");
for (int g = 0; g < num * 3; g++)
{
System.out.print(" ");
}
System.out.print("*");
}
for (int h = 0; h < num; h++)
{
System.out.println("");
for (int i = num; i < 1; i--)
{
System.out.print(" ");
}
System.out.print("*");
for (int j = (num * 3) - 2; j < num; j++)
{
System.out.print(" ");
}
System.out.print("*");
}
System.out.println("");
for (int k = 0; k < num + 1; k++)
{
System.out.print(" ");
}
for (int l = 0; l < num; l++)
{
System.out.print("*");
}
Any kind of help would be appreciated! Thank you.
This should work
Just changed couple of for loop conditions
while (num < 2 || num > 10) {
System.out.println("Enter a valid number please.");
num = keyNum.nextInt();
}
for (int a = 0; a < num + 1; a++)
{
System.out.print(" ");
}
for (int b = 0; b < num; b++)
{
System.out.print("*");
}
for (int c = 0; c < num; c++)
{
System.out.println("");
for (int d = num; d > c; d--)
{
System.out.print(" ");
}
System.out.print("*");
for (int e = num * 2; e < (num * 3) + (c *2); e++)
{
System.out.print(" ");
}
System.out.print("*");
}
for (int f = 0; f < num; f++)
{
System.out.println("");
System.out.print("*");
for (int g = 0; g < num * 3; g++)
{
System.out.print(" ");
}
System.out.print("*");
}
for (int h = 1; h <= num; h++)
{
System.out.println("");
for (int i = 0; i < h; i++)
{
System.out.print(" ");
}
System.out.print("*");
for (int j = 0; j < (num * 3) - (h * 2) ; j++)
{
System.out.print(" ");
}
System.out.print("*");
}
System.out.println("");
for (int k = 0; k < num + 1; k++)
{
System.out.print(" ");
}
for (int l = 0; l < num; l++)
{
System.out.print("*");
}

Making Tree with Nested Loops

So, for my programming class, our teacher tasked us with making a tree out of *'s and other random characters. There has to be a star at the top of the tree that increases in size every so often, depending how large the user wants the tree. For some reason, if the number the user enters is greater than 15, the bottom half is too far to the right. I tried changing my code, but then everything less than 15 is too far the right. How can I get that to work?
Scanner scan = new Scanner(System.in);
System.out.println("Enter the size of the tree you would like");
int a = scan.nextInt();
int b = 0;
int c = 0;
int d = 0;
if ( a >= 12){
d = 1;
} else {
d = 0;
}
//Top Half of Star
for (int i = 0; i < a / 4; i++) {
for (int j = i; j < a; j++){
System.out.print(" ");
}
for (int j = 0; j < 2 * i + 1; j++){
System.out.print("*");
b = b + 1;
}
System.out.println("");
}
//Bottom Half of Star
for (int i = 1; i < a/4; i++){
for (int j = d; j < a; j++){
System.out.print(" ");
}
for (int j = c; j < b/3; j++){
System.out.print("*");
}
c = c + 2;
d = d - 1;
System.out.println("");
I think this is what you're looking for, if you're defining the size as the number of rows.
import java.util.Scanner;
public class NestedTree
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter the size of the tree you would like");
int size = scan.nextInt(); // Get the size of the tree
for (int i = 0; i < size; i++) {
int spaces = size - i;
for (int s = 0; s < spaces; s++) { // Print spaces
System.out.print(" ");
}
for (int r = 0; r <= i; r++) { // Print stars
System.out.print("* ");
}
System.out.print("\n"); // new line
}
}
}

for loops dual-wedge shape

So for an assignment i need to make a dual-wedge figure with for loops,so far I have had no luck, can anyone help?
Here is a sample of the outcome:
*******
*** ***
** **
* *
Here's my code
int dual_wedge_length=9;
int half_length = dual_wedge_length/2;
int space=1;
int height2 = (dual_wedge_length/2) +1;
for (int line1 = 1; line1 <= dual_wedge_length; line1++)
{
System.out.print("*");
}
System.out.println();
for (int height = 1; height <= (dual_wedge_length+1)/2; height++)
{
for (int half1 = 1; half1 <= half_length; half1++)
{
System.out.print("*");
//half_length--;
space+=2;
}
for (int space_counter = 0; space_counter == space;space_counter++)
{
System.out.print(".");
}
for (int half1 = 1; half1 >= half_length; half1++)
{
System.out.print("*");
half_length--;
}
System.out.println();`
int dual_wedge_length=9;
int height2 = (dual_wedge_length+1)/2;
for(int i = 0;i < dual_wedge_length; i++)System.out.print("*");
System.out.println();
for(int i = 1; i < height2;i++){
int num = height2 - i;
for(int j = 0; j < num; j++){
System.out.print("*");
}
for(int k = 0; k < 2*i -1; k++){
System.out.print(" ");
}
for(int m = 0; m < num; m++){
System.out.print("*");
}
System.out.println();
}

printing numbers flipped in pyramid style

I am trying to print something like this using for loops:
1
121
12321
1234321
123454321
int mid = 1;
System.out.println(" " + mid + " ");
mid++;
for(int i = 1; i <=4; i++){
//left spaces
for(int x = 4; x > i; x--){
System.out.print(" ");
}
//left diguts
for(int z = 1; z <= i; z++){
System.out.print(z);
}
//middle digit
System.out.print(mid);
mid++;
//right diguts
for(int b = 1; b <= i; b++){
System.out.print(b);
}
//right spaces
for(int y = 4; y > i; y--){
System.out.print(" ");
}
System.out.println();
}
But i kept on getting:
1
121
12312
1234123
123451234
For the right digits you will have to print the digits in reverse order so make the change as below:
//right diguts
for(int b = 1; b <= i; b++){
System.out.print(b);
}
change this to
//right diguts
for(int b = i; b > 0; b--){
System.out.print(b);
}
Actually there is no need to separate 1 out. You can include it in common logic:
public class Pyramid {
public static void main(String[] args) {
int mid = 1;
for (int i = 0; i <= 4; i++) {
// left spaces
for (int x = 4; x > i; x--) {
System.out.print(" ");
}
// left digits
for (int z = 1; z <= i; z++) {
System.out.print(z);
}
// middle digit
System.out.print(mid);
mid++;
// right digits
for(int b = i; b > 0; b--){
System.out.print(b);
}
// right spaces
for (int y = 4; y > i; y--) {
System.out.print(" ");
}
System.out.println();
}
}
}
This is actually nice golf puzzle!
I did this only using two loops! :)
int n = 5;
for(int i=0; i < n; i++){
for(int j=0; j < 2*n; j++){
int abs = Math.abs(n-j);
System.out.print(abs>i ? " " : i-abs+1);
}
System.out.println();
}
Output:
1
121
12321
1234321
123454321

Categories

Resources