Printing a nested for-loop pattern in java - java

Goal:
1234
2345
3456
4567
5678
i have the pattern down but it doesnt println after length(4):
int i;
int a;
for (i = 1; i <= 5; i++)
{
for (a = i;a<=i+3;a++)
{
System.out.print(a);
}
}
My output is: 12342345345645675678

Just add it after the second loop:
int i;
int a;
for (i = 1; i <= 5; i++) {
for (a = i;a<=i+3;a++) {
System.out.print(a);
}
System.out.println();
}

int i;
int a;
for (i = 1; i <= 5; i++)
{
for (a = i;a<=i+3;a++)
{
System.out.print(a);
}
System.out.println(); // add this code
{

No need to have two for loops, try :
for (i = 1; i <= 5; i++) {
int j = i;
System.out.println(j++ + "" + j++ + "" + j++ + "" + j);
}
EDIT : I know this will limit the flexibility, but this is just a toy problem.

int i;
int a;
for (i = 1; i <= 5; i++)
{
for (a = i;a<=i+3;a++)
{
System.out.print(a);
}
System.out.println();
}

Add System.out.println() after the inner loop.

Try:
int i;
int a;
for (i = 1; i <= 5; i++) {
for (a = i;a<=i+3;a++) {
System.out.print(a);
}
System.out.println(); // this will print a new line.
}

Add System.out.Println() after the inner loop. This will move the cursor to next line

Java Solution
int f, g, h,T;
f = 12345;
h = 11111;
for (g = 1; g <= 5; g++)
{
T = f + ((g - 1) * h);
System.out.print( T + "\n")
}

for(int i =1;i<=4;i++) {
System.out.print(i);
}
System.out.println();
for(int i =2;i<=5;i++) {
System.out.print(i);
}
System.out.println();
for(int i =3;i<=6;i++) {
System.out.print(i);
}
System.out.println();
for(int i =4;i<=7;i++) {
System.out.print(i);
}
System.out.println();

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

Write a program that produces images of Christmas trees as output. I don't know what the error is

My code is resulting in an infinite loop when i run it. Don't know whats wrong.
!(https://d2vlcm61l7u1fs.cloudfront.net/media%2F1aa%2F1aa1af9f-41ff-48d8-89e9-7b093a909045%2Fphpbrt5Fy.png)
I have tried the code below. It compiles properly.
public class Project3_1 {
//declare SEGMENTS and HEIGHT
public static final int SEGMENTS = 4;
public static final int HEIGHT = 4;
public static final int TOTAL = (2*(SEGMENTS)) + (2*(HEIGHT)) - 3;
public static void TopTree(int SEGMENTS, int HEIGHT) {
for (int s = 1; s <= SEGMENTS; s++) {
for (int h = 1; h <= HEIGHT; h++) {
int ASTERISKS = ((2*s) + (2*h) - 3);
int SPACES = ((TOTAL - ASTERISKS)/2);
//spaces
for (int b = 1; b <= SPACES; b++) {
System.out.print(" ");
}
//Asterisks
for (int a = 1; a <= ASTERISKS; a++) {
System.out.print("*");
}
System.out.println();
}
}
}
public static void TreeBase() {
int x = (TOTAL - 7)/2;
for (int i = 1; i <= (TOTAL-1)/2; i++){
System.out.print(" ");
}
System.out.println("*");
for (int i = 1; i <= (TOTAL-1)/2; i++){
System.out.print(" ");
}
System.out.println("*");
for (int i = 1; i <= x; x++){
System.out.print(" ");
}
for (int i = 1; i <= 7; i++) {
System.out.print("*");
}
for (int i = 1; i <= (TOTAL - (x + 7)); i++) {
System.out.print(" ");
}
System.out.println("");
}
public static void main (String[] args){
TopTree(SEGMENTS, HEIGHT);
TreeBase();
}
}
Check your third for loop in TreeBase(), you increment the wrong variable. The code you posted runs fine for me, it just doesn't make that bottom line of asterisks due to that loop I mentioned not being correct. Once you fix that, you should be good

Java: Array with loop(matches)

I need to find the first match in this task. Probably i am just missing something. As you can see, i found the last match.I am not copied the first half of the code. Thank you.
for (int i = 0; i <= n - 1; i++) {
if (iv[i] == a) {
hely = i;
}
}
if (hely == -1) {
System.out.println("text");
} else {
System.out.println("text " + a + " text " + (hely + 1) + "text");
}
break the loop when you find first match.
for (int i = 0; i <= n - 1; i++) {
if (iv[i] == a) {
hely = i;
break;
}
}
You need to exit the for loop after finding the first match:
for (int i = 0; i <= n - 1; i++) {
if (iv[i] == a) {
hely = i;
break;
}
}

Java printing patterns using for loops

I need to get the following pattern
have developed following code.
public static void main(String[] args) {
int number = 9;
for(int i=0;i<9;i++){
for(int j=0;j<18;j++){
if(number==6)
continue;
System.out.print(number);
}
if(number != 6)
System.out.println();
number--;
}
}
But I cant think about the logic to get the curved part of the pattern. Can Anyone give an opinion?
if (j < number || j >= 18 - number)
System.out.print(number);
else
System.out.print(" ");
You could try this:
public class CurveOutput {
public static void main(String args[]) {
int startNumber = 9;
for (int currentNum = startNumber; currentNum >= 0; currentNum--) {
StringBuilder line = new StringBuilder();
for (int i = 0; i < currentNum; i++) {
line.append(currentNum);
}
for (int i = 0; i < startNumber - currentNum; i++) {
line.append(" ");
}
System.out.println(line.toString() + line.reverse().toString());
}
}
}
this snippet produce:
999999999999999999
88888888 88888888
7777777 7777777
666666 666666
55555 55555
4444 4444
333 333
22 22
1 1
From my perspective it is really about programming on a correct level of abstraction.
The requirement is not to put as many spaces in the start or at the end, but rather to align the numbers to left or right. If there would be such functionality, it would be better and most likely more readable. And there is such:
public static void main(String args[]) {
int startNumber = 9;
for (int i = startNumber; i > 0; i--) {
String numberToPrint = Strings.repeat("" + i, i); // from Google Guava
String leftHalf = String.format("%-" + startNumber + "s", numberToPrint);
String rightHalf = String.format("%" + startNumber + "s", numberToPrint);
System.out.printf("%s%s%n", leftHalf, rightHalf);
}
}
Try this...
int num=9,save=9;
for(int i=1;i<10;i++)
{
for(int j=1;j<=18&&num!=6;j++)
{
int t=save-num;
if(((j<=(9-t)) || (j>(9+t))))
System.out.print(num);
else
System.out.print(" ");
}
num=num-1;
System.out.println("\n");
}
package q17;
public class Q17 {
public static void main(String[] args) {
int x = 9, y = 10;
for (int i = x; i >= 1; i--) {
if (i == 6) {
x--;
y++;
continue;
}
for (int j = 1; j <= 18; j++) {
if ((i != 9) && ((j >= x) && (j <= y))) {
System.out.print(" ");
} else {
System.out.print(i);
}
}
if (i != 9) {
x--;
y++;
}
System.out.println();
}
}
}

How can a multiplication table be displayed using only nested for loops and System.out.println in Java?

public static void main (String [] args)
{
int q, r, s, t, u, v, w, x, y, z;
for (q=1; q<=10; q++)
{
System.out.print("\t" + q);
}
System.out.println();
for (r=2; r<=20; r += 2)
{
System.out.print("\t" + r);
}
System.out.println();
for (s=3; s<=30; s += 3)
{
System.out.print("\t" + s);
}
System.out.println();
for (t=4; t<=40; t += 4)
{
System.out.print("\t" + t);
}
System.out.println();
for (u=5; u<=50; u += 5)
{
System.out.print("\t" + u);
}
System.out.println();
for (v=6; v<=60; v += 6)
{
System.out.print("\t" + v);
}
System.out.println();
for (w=7; w<=70; w += 7)
{
System.out.print("\t" + w);
}
System.out.println();
for (x=8; x<=80; x += 8)
{
System.out.print("\t" + x);
}
System.out.println();
for (y=9; y<=90; y += 9)
{
System.out.print("\t" + y);
}
System.out.println();
for (z=10; z<=100; z += 10)
{
System.out.print("\t" + z);
}
}
Despite how ridiculous this program looks, it displays a multiplication table in the desired format. Being a noob (as you can see) and trying to learn these nested loops has been very confusing, especially when the tutorial says that this multiplication table, in the same exact format, can be written using just nested for loops and System.out.println. The tutorial is not the least bit helpful and gives a rather simple use of nested for loops and right now I cannot see how it's applicable in simplifying this program....but it says it can be done so it can.
This is something that you will learn in any beginners book:
for (int i=1;i<=10;i++){
for (int j=1;j<=10;j++)
System.out.print("\t"+i*j);
System.out.println();
}
This works as you expected using array
public static void main(String[] args) {
int[][] array = new int[11][11];
for (int i=1; i<array.length; i++) {
for (int j=1; j<array[i].length; j++) {
array[i][j] = i*j;
System.out.print(" " + array[i][j]);
}
System.out.println("");
}
}
You mean something like this: -
for (int i = 1; i <= 10; i++) {
for (int j = i; j <= 10 * i; j += i) {
System.out.print(j + " ");
}
System.out.println();
}
Here is my take on this with pretty printing (individual cells right/left justified):
private static void printMultiplicationTable(int m, int n) {
int[][] arr = new int[m][n];
int[] maxes = new int[n];
for (int i = 1; i <= m; i++) {
for (int j = i, k = 1; j <= n * i; j += i) {
arr[i-1][k-1] = j;
if ((maxes[k-1]+"").length() < (j+"").length()) {
maxes[k-1] = (j+"").length();
}
k++;
}
}
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr[i].length; j++)
System.out.print(adjust(arr[i][j], maxes[j], true) + " ");
System.out.println();
}
}
private static String adjust(int n, int tot, boolean rightJustify) {
StringBuffer buff = new StringBuffer();
for(int i=1; i<=tot - (n + "").length(); i++) {
buff.append(" ");
}
if (rightJustify) {
buff.append(n);
return buff.toString();
} else {
return n + buff.toString();
}
}

Categories

Resources