How do i put the other java number triangle side by side - java

So i was trying this code i found on the internet that let me make number triangles, the codes are like so
public class StarsAndDraws {
public static void main(String[] args) {
for (int i = 0; i <= 4; i++) {
for (int j = 4; j >= 1; j--){
if (j > i){
System.out.print(" ");
} else {
System.out.print(i - j + 1);
}
}
System.out.println();
}
}
}
the output looks like this
1
1 2
1 2 3
1 2 3 4
but this is the output im looking for
1
1 2
1 1 2 3
1 2 1 2 3 4
i have no idea how, help and explanation is appreciated because id like to do this to other kinds of stuff aswell

To print first 1/ 1 2, it is needed to define another loop for that one too.
class Main {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
if (i < 3) {
System.out.print(" ".repeat(9));
} else {
System.out.print(" ".repeat((4 - i) * 2));
for (int j = 1; j <= i - 2; j ++) {
System.out.print(j);
System.out.print(" ");
}
System.out.print(" ".repeat(6));
}
System.out.print(" ".repeat(2 * (4 - i)));
for (int j = 1; j <= i; j ++) {
System.out.print(j);
System.out.print(" ");
}
System.out.println();
}
}
}

Related

half pyramid inverted with even number

half the pyramid is inverted with an even number, and each line omits the starting and ending numbers, so that the output expectation are as shown below.
Expected output
2 4 6 8 10
4 6 8
6
but I have tried my code below with the results that do not match my expectations.
My code
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >=1 ; i--) {
for (int j = 1; j <=2*i ; j++) {
if (j % 2 == 0){
System.out.print(j + " ");
}
}
System.out.println();
}
}
My output
2 4 6 8 10
2 4 6 8
2 4 6
2 4
2
Question:
how to solve the problem is?
You need to change the starting value of j as well and limit how often your first loop runs depending on rows:
int rows = 5;
for (int i = rows; i >= rows / 2; i--) {
for (int j = 2 + 2 * (rows - i); j <= 2 * i; j += 2) {
System.out.print(j + " ");
}
System.out.println();
}
Currently, you are starting the inner loop as int j = 1. Instead of a fixed start, it should be variable.
Replace
int j = 1;
with
int j = 2 * (rows - i + 1) - 1;
The starting condition of j should depend on i, also you can remove the if using j += 2 as increment statement
int rows = 5;
for (int i = rows; i >= 1; i--) {
for (int j = 2 * (rows - i + 1); j <= 2 * i; j += 2) {
System.out.print(j + " ");
}
System.out.println();
}
'rows' is a bad name. Can you see why?
here's the code:
public static void main(String[] args) {
int columns = 5;
for (int currentColumn = 0; currentColumn < columns ; currentColumn++) {
for (int j = currentColumn; j < columns-currentColumn ; j++) {
System.out.print((2*j+2) + " ");
}
System.out.println();
}
}
First, try to understand the pattern. For each iteration, the number of elements in a column is decreasing by 2. So consider the below code
public static void main(String[] args) {
int input = 5, multiplier = 2;
for(int numberOfRows = input; numberOfRows >= 1; numberOfRows -= 2) {
for(int columns = 1; columns <= numberOfRows; columns += 1) {
System.out.print(columns * multiplier + "\t");
}
System.out.println();
}
}

How do I put spaces in between individual characters when creating the triangle using nested for loops?

My apologies if this individual question is a duplicate, but I have not seen any specific answers to this particular problem.
I am trying to put spaces in between each individual character in a triangle I created using nested for loops in the code below.
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= 1; j--) {
if (j > i)
System.out.print(" ");
else
System.out.print(j);
}
System.out.println();
}
System.out.println();
for (int i = 1; i <= 6; i++) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= (5 - i + 1); j++) {
System.out.print(j);
}
System.out.println();
}
This outputs:
1
21
321
4321
54321
12345
1234
123
12
1
However, it is required to be:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
I've tried putting " " on either as well as both sides of the print statement, as I did with the reverse of these two triangles, to no avail. Am I missing something?
You can also try something like this.
int spaces=10;
int z;
for (int i = 1; i <=5 ; i++) {
spaces=spaces-2;
for (int j = 0; j<=spaces ; j++) {
if(j!=spaces)
System.out.print(" ");
else {
z=i;
while (z >= 1) {
System.out.print(z + " ");
z--;
}
}
}
System.out.println("\n");
}
System.out.println("\n");
spaces=-2;
int m;
for (int i = 1; i <=5 ; i++) {
spaces=spaces+2;
for (int j = 0; j <=spaces ; j++) {
if(j!=spaces)
System.out.print(" ");
else{
z=i;
m=1;
while (m<=6-z) {
System.out.print(m + " ");
m++;
}
}
}
System.out.println("\n");
}
Just change System.out.print(j); to System.out.print(j + " ");

Can't print the pattern

I created the code that should print a pattern like
12345
2345
345
45
5
I have the code written below, the logic works fine in python but in java the output is different.
class Testing{
public static void main(String args[])
{
for (int i = 1; i<6;i++)
{
for (int j =0; j<i-1;j++)
{
System.out.print(" ");
}
while (i < 6){
System.out.print(k);
System.out.println();
i++;
}
}
}
}
The output is just 12345. I don't understand why does it iterate over first for loop for only once.
Use another variable for while control.
public class Testing {
public static void main(String args[]) {
int k;
for (int i = 1; i < 6; i++) {
for (int j = 0; j < i - 1; j++) {
System.out.print(" ");
}
k = i;
while (k < 6) {
System.out.print(k);
k++;
}
System.out.println();
}
}
}
You can see this in this link
this will show you :
12345
2345
345
45
5
Note: When 'while loop' increases. That increases i value bigger than 6. So next time it terminates the outer loop. That was your mistake.
package com.appointment.api;
class Testing {
public static void main(String args[]) {
for (int i = 1; i < 6; i++) {
System.out.println();
for (int j = 0; j < i - 1; j++) {
System.out.print(" ");
}
int x = i;
while (x < 5) {
System.out.print(i);
x++;
}
}
}
}
Following is a java-8 implementation of the problem:
IntStream.rangeClosed(1, MAX)
.forEach(i -> IntStream.rangeClosed(1, MAX)
.mapToObj(j -> j == MAX ? j + "\n" : j >= i ? j : " ")
.forEach(System.out::print)
);
Set MAX = 5 and it will print your pattern.
Output:
12345
2345
345
45
5

Making a pattern with java using nested loops

I have to make a pattern that is of a diamond shape such as this:
1
1 2
1 2 3
1 2 3 4 5
1 2 3
1 2
1
I wrote this ( sorry if for the variable name, this is just a small part of my program and I'm trying to get the pattern right so I didn't mind my variables at the moment):
public class NewFile{
public static void main(String []args){
int k = 0;
for (int i=1 ; i<=5 ; i++)
{
{ for (int h=2 ; h >= i ; h--)
System.out.print(" ");
for (int j=1 ; j<= i + k ; j++)
System.out.print(j);
for (int w=2 ; w>= i; w--)
System.out.print(" ");
}
k++;
System.out.println();}
}
}
My output is the following:
1
123
12345
1234567
123456789
I realize I should divide the code into a lower and upper triangle using two loops. However, I don't know how to break the first part. I did find the "trend" but I don't see how to implement it.
The following code will display a diamond shape that is made up of asterisks:
int i = 0, j, k, n;
n = 7; // 7 characters high. Change as needed.
for (k = 1; k <= (n + 1) / 2; k++) {
for (i = 0; i < n - k; i++) {
System.out.print(" ");
}
for (j = 0; j < k; j++) {
System.out.print("* ");
}
System.out.println("");
}
for (k = ((n + 1) / 2); k < n; k++) {
for (i = 1; i < k; i++) {
System.out.print(" ");
}
for (j = 0; j < n - k; j++) {
System.out.print(" *");
}
System.out.println("");
}
The integer n represents the height of the diamond in characters and can be changed as needed.

What is the logic to print the below mentioned number pattern

class Num {
public static void main(String[] args) {
for(int i=1;i<=5;i++) {
for(int j=1;j<=i;j++) {
if(j==1) {
System.out.print(i);
}
else if(j==2) {
System.out.print(" "+(i+j+2));
}
else {
System.out.print(" "+(i+j+4));
}
}
System.out.println(" ");
}
}
}
Output:
1
2 6
3 7 10
4 8 11 12
5 9 12 13 14
Expected:
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
I tried so much and came up with this logic
when j=1 then i
when j=2 then i+j+2
when j=3 then i+j+4
when j>=4 then i+j+5
there are totally 4 conditions here, how do i get do this in nested for loop. Any other logic is also appreciable.
int lineCount = 5;
for (int i = 1; i <= lineCount; i++) {
int value = i;
for (int j = 1; j <= i; j++) {
System.out.print(value + " ");
value += lineCount -j;
}
System.out.println("");
}
you forgot your j = 3 case
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1) {
System.out.print(" " + i);
} else if (j == 2) {
System.out.print(" " + (i + j + 2));
}
else if(j == 3){
System.out.print(" " + (i + j + 4));
}
else {
System.out.print(" " + (i + j + 5));
}
}
}
I am not a java developer, so sending you solution in C#. You will have to replace "using System;" with some import statement and Console.WriteLine statements with appropriate System.out.print or println.
using System;
class Program
{
static void Main()
{
PrintNumbers(1, 5);
}
static void PrintNumbers(int level, int MaxLevel)
{
if( MaxLevel < level )
{
return;
}
int num = 0;
for(int i = 0, diff=MaxLevel; i < level; i++)
{
num += (i == 0)?level:(diff-i);
Console.Write("{0} ",num);
}
Console.WriteLine("");
PrintNumbers(level+1, MaxLevel);
}
}
I advise and believe that it would be better that our codes be dynamic, meaning it would work on any input of user.Suppose if the question was to change from 5 rows to 6 rows,you have to change the for constructs as well add an additional if construct.
Secondly it would not be a better idea to keep as many if constructs w.r.t the columns you need.
Take the no. of rows the user wants to a variable say 'noofrows'. Then its just the same as your code with little changes:
int noofrows;
Scanner s=new Scanner(System.in);
System.out.println("\nGive the no. of rows needed:\n");
noofrows=(int)s.nextInt();
System.out.println("Number give is:"+noofrows);
int emptyspaces;
for(int i = 1; i <= noofrows; i++)
{
for (int j = 1; j <= i; j++)
{
emptyspaces=(j)*(j-1)/2; //counting the no. of emptspace which is an arithmetic progression
System.out.print(((j-1)*noofrows+i)-emptyspaces +" ");
}
System.out.print("\n");
s.close(); //edited version
Please let me know incase anything went wrong
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n=5;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
if(j==1)
cout<<i<<" ";
else
cout<<i+j+j<<" ";
}
cout<<endl;
}
}
/*OUTPUT :-
1
2 6
3 7 9
4 8 10 12
5 9 11 13 15
*/

Categories

Resources