What is the logic to print the below mentioned number pattern - java

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
*/

Related

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

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();
}
}
}

For loop java making the output into 2 columns

Hi I'm a beginner and I'm still learning and if someone could help me in this one thx in advance my code is:
for(int i = 0; i < 10; i++) {
System.out.println(i);
}
and the output should be like this:
0 1
2 3
4 5
6 7
8 9
Just use next line only for odd numbers:
for (int i = 0; i < 10; i++) {
if (i % 2 == 0)
System.out.format("%2d", i);
else
System.out.format(" %2d\n", i);
}
Output:
0 1
2 3
4 5
6 7
8 9
P.S. For more general usage, I would extract it into separate method
public static void printTwoColumns(int min, int max) {
int width = String.valueOf(max).length();
for (int i = min; i < max; i++) {
String str = String.format("%" + width + 'd', i);
if (i % 2 == 0)
System.out.print(str);
else {
System.out.print(' ');
System.out.println(str);
}
}
}
You can do something like this:
for (int i = 0; i < 10; i++) {
System.out.print(i);
System.out.println(++i);
}
The first print uses the current i value and using System.out.print.
The second
print is using println so it goes one line down and also takes ++i so it will advance i by 1 and will also print the new value.
Each loop iteration is printing two values and advances i by total of 2.
To do what you want you can either do something like this:
for(int i = 0; i < 10; i ++){
if(i % 2 == 0){
System.out.print(i); //Only for evens
}else{
System.out.println(i); //Only for odds
}
}
Or you could simplify it even more with something like this:
for(int i = 0; i < 5; i += 2){
System.out.println(i + " " + (i + 1));
}

Java print integer in 4 rows and 4 columns in below format without using array

I am suppose to create a program that prints out following:
16 15 14 13
9 10 11 12
8 7 6 5
1 2 3 4
Hers's my current code:
public class Ideone {
public static void main (String[] args) {
int n=16;
int rows = 4;
int cols = 4;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
System.out.print(n+" ");
n--;
}
if(i != rows) {
System.out.println();
}
}
}
}
The output is as below
16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1
Can someone please help to figure out the solution for this one?
The easiest (but most likely not the desired) solution to the problem as stated is this:
System.out.println("16 15 14 13");
System.out.println(" 9 10 11 12");
System.out.println(" 8 7 6 5");
System.out.println(" 1 2 3 4");
Try this.
public class Ideone {
public static void main (String[] args) {
int n=16;
int rows = 4;
int cols = 4;
int flag = 0;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
System.out.print(n -cols*flag +(2*j -1)*flag +" ");
n--;
}
if(i != rows) {
System.out.println();
flag = 1 - flag;
}
}
}
}
Keep track of when the numbers need to be printed in reverse in a boolean. Then use one of two loops depending on that boolean.
public class Main {
public static void main(String[] args) {
int n = 16;
final int rows = 4;
final int cols = 4;
boolean reverse = false;
for (int r = 1; r <= rows; ++r) {
if (reverse) {
for (int c = n - cols + 1; c <= n; ++c) {
System.out.print(c + " ");
}
n -= cols;
} else {
for (int c = 1; c <= cols; ++c, --n) {
System.out.print(n + " ");
}
}
System.out.println();
reverse = !reverse;
}
}
}
You could change the outer loop so it iterates row/2 times and have two inner loops, one doing a forward iteration and one backward.

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 find how to program this number pattern

Number Pattern
I am asked to enter a number rc, and based on rc construct this pattern. I am able to initialize the table but without the highlighted numbers:
int [][] num2 = new int [rc][rc];
counter = 1;
for(int i = 0; i < rc; i++){
if(i!=0)
counter--;
for(int j =0; j < rc; j++){
num2 [i] [j] = counter;
counter ++;
}
}
Any hints or ideas?
You got it partially right. The numbers printed on each row are the same but the start point is incremented by 1 each time. Thus, you can use variable i again to shift it.
int [][] num2 = new int [rc][rc];
int counter = 1;
for (int i = 0; i < rc; i++) {
for (int j = 0; j < rc; j++) {
num2[i][(j + i) % rc] = counter++;
}
}
The following code is working fine for your problem.
int rc=5;
int [][] num2 = new int [rc][rc];
int counter = 1;
for(int i = 0; i < rc; i++){
for(int j =i; j < rc; j++){
num2 [i] [j] = counter;
counter ++;
}
for(int k =0; k < rc; k++){
if(num2[i][k]==0){
num2 [i] [k] = counter;
counter++;
}
System.out.print(num2[i][k]+"\t");
}
System.out.println();
}
The logic behind my solution is:
First fill an array from 1 - N, where N is the user input (or
rc in this case):
Then, we check if it's not the first line, if it is, we simply print
the numbers in order.
Now, we have to know which numbers go first:
In the line 1 (remember it starts from 0), it must print the number at [1][4] in [1][0], so our loop substracts rc - i + j, this gives: 5 - 1 + 0, which in fact is index [4].
We know that after we've printed the last numbers first, we must continue the sequence, so we print index: [1][0] at [1][1] (Why 1, 2? Because otherwise we would get something like the example below, that's why we need to substract 1 to it
1 2 3 4 5
10 7 8 9 10
And that's it:
public class StrangePattern {
public static void main(String[] args) {
int rc = 5;
int number = 1;
int spaces = 0;
int[][] numbers = new int[rc][rc];
for (int i = 0; i < rc; i++) {
for (int j = 0; j < rc; j++) {
numbers[i][j] = number;
number++;
}
}
for (int i = 0; i < rc; i++) {
for (int j = 0; j < rc; j++) {
if (i != 0) {
if (j < i) {
System.out.print(numbers[i][rc - i + j] + "\t");
} else {
System.out.print(numbers[i][j - spaces] + "\t");
}
} else {
System.out.print(numbers[i][j] + "\t");
}
}
spaces++;
System.out.println();
}
}
}
Which provides this output:
1 2 3 4 5
10 6 7 8 9
14 15 11 12 13
18 19 20 16 17
22 23 24 25 21
And this one for rc = 3:
1 2 3
6 4 5
8 9 7

Categories

Resources