How to create a square with numbers? - java

I'm trying to create a square shape with numbers like this:
1234
2341
3412
4123
How would I go about doing that?
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
}
System.out.println();
}
This prints:
1234
234
34
4
How do I get it to restart at 1 again?

You can do by using modulo
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print((j + i) % 4 + 1);
}
System.out.println();
}
output
1234
2341
3412
4123

You need to update to
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
}
for (int j = 1; j < i; j++) {
System.out.print(j);
}
System.out.println();
}

add one more for loop
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
}
for (int j = 1; j <i; j++) {
System.out.print(j);
}
System.out.println();
}

There's probably a simpler way, but this should work:
for (int i = 1; i <= 4; i++) {
for (int j = 3; j <= 6; j++) {
System.out.print ((i + j) % 4 + 1);
}
System.out.println();
}

try this:
public class MyClass {
public static void main(String args[]) {
int count = 0;
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
count += 1;
}
if(count < 4){
int remain = 4 - count; // to identify how many times it goes again
for(int k=0;k<remain;k++){
System.out.print(k+1);
}
}
count = 0;
System.out.println();
}
}
}
Output:
1234
2341
3412
4123

How about this?
for(int i = 1; i <= 4; i++){
for(int j = 1; j <= 4; j++){
System.out.print(((i + j + 2) % 4) + 1);
}
System.out.println();
}
If you look at the expected output, it has a pattern, which is restricted to a finite and known sequence of numbers. Modulus is the ideal option to handle cases like this.

for (int i = 1; i <= 4; i++) {
for (int j = 0; j <= 3; j++) {
System.out.print(((i + j) % 4) + 1);
}
System.out.println();
}
Modulus (%) gives the remainder of one number divided by another, so (i+j) % 4 always gives a result between 0 and 3.
Add one to that to get the value you want (between 1 and 4).

Related

For loop for increasing pyramid pattern

I am trying to figure out how to increment a pyramid based on input from the user.\
If a user enters the number 3, my program will print a pyramid of height 3, three times.\
What I would like it to do instead, is to print 3 pyramids, but the first pyramid should have a height of 1, and the second a height of 2, and the third a height of 3.
Here is my code:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int l = 0; l < n; l++) {
System.out.println("Pyramid " + n);
for (int i = 1; i <= n; i++) {
for (int j = 0; j < n - i; j++) {
System.out.print(".");
}
for (int j = 0; j < i; j++) {
System.out.print("#");
}
for (int j = 1; j < i; j++) {
System.out.print("#");
}
for (int k = 0; k < n - i; k++) {
System.out.print(".");
}
System.out.println();
}
}
You use l to keep track of the number of pyramid you're working on, so you could just use it in the loop instead of n. Note that l starts with 0, not 1, so you may want to amend the loop accordingly and run from 1 to n, not from 0 to n-1
for (int l = 1; l <= n; l++) { // Note the loop starts at 1
System.out.println("Pyramid " + l);
for (int i = 1; i <= l; i++) { // Note the usage of l instead on n
for (int j = 0; j < l - i; j++) {
System.out.print(".");
}
for (int j = 0; j < i; j++) {
System.out.print("#");
}
for (int j = 1; j < i; j++) {
System.out.print("#");
}
for (int k = 0; k < l - i; k++) { // Note the usage of l instead on n
System.out.print(".");
}
System.out.println();
}
}
All you have to change is this part of code
System.out.println("Pyramid " + (l+1));
for (int i = 1; i <= l+1; i++) {

How to make diamond pattern using java?

How to make this pattern
if input N = 5
Output :
Mine is like this, it become 2N
if input N = 5
Output
Here's my code
int i,j;
for(i = 0; i <= n; i++)
{
for(j = 1; j <= n - i; j++)
System.out.print(" ");
for(j = 1; j <= 2 * i - 1; j++)
System.out.print("*");
System.out.print("\n");
}
for(i = n - 1; i >= 1; i--)
{
for(j = 1; j <= n - i; j++)
System.out.print(" ");
for(j = 1; j <= 2 * i - 1; j++)
System.out.print("*");
System.out.print("\n");
}
What should i fix??
You can check odd numbers in your loop. Please see the following example:
public static void main(String[] args) {
printPattern(5);
}
private static void printPattern(int n) {
int i, j;
for (i = 0; i <= n; i++) {
if (i % 2 != 0) {
for (j = 1; j <= (n - i)/2; j++) {
System.out.print(" ");
}
for (j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
for (i = n - 1; i >= 1; i--) {
if (i % 2 != 0) {
for (j = 1; j <= (n - i)/2; j++) {
System.out.print(" ");
}
for (j = 0; j <i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Instead of running these two loops from 0 to N twice. Just run half N/2 in each loop.
Example:
public static void main(String[] args) {
int n = 10;
for (int i = 0; i <= (n / 2 + 1); i++) {
for (int j = 1; j <= n - i; j++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.print("\n");
}
// N/2
for (int i = n / 2 - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.print("\n");
}
}

how to print the below pattern

for the given input I need to print the pattern. For example for input = 6 I have to print:
MMMMMMSDDDDDD
MMMMMSSSDDDDD
MMMMSSSSSDDDD
MMMSSSSSSSDDD
MMSSSSSSSSSDD
MSSSSSSSSSSSD
CSSSSSSSSSSSK
CCSSSSSSSSSKK
CCCSSSSSSSKKK
CCCCSSSSSKKKK
CCCCCSSSKKKKK
CCCCCCSKKKKKK
I have tried but couldn't go further than this could anyone help
public class tgk {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int half = ((2*size)+1)/2;
for (int i = 0; i < size ; i++)
{
for (int j = size; j > i; j--)
{
System.out.print("M");
}
for (int k = half+1 ; k > half - i; k--)
{
System.out.print("S");
}
System.out.println();
}
for(int i = size; i > 0; i--)
{
for (int j = size; j >= i; j--) {
System.out.print("C");
}
for (int k = 0; k < (i * 2 - 1); k++) {
System.out.print("S");
}
System.out.println();
}
}
}
if input = 3 it should be
MMMSDDD
MMSSSDD
MSSSSSD
CSSSSSK
CCSSSKK
CCCSKKK
You can use two sets of for loops to print each half of the pattern. Assuming input variable holds the size of the problem
int input = 3;
for (int i = 0; i < input; i++) {
for (int j = 0; j < input - i; j++) {
System.out.print('M');
}
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print('S');
}
for (int j = 0; j < input - i; j++) {
System.out.print('D');
}
System.out.println();
}
for (int i = input - 1; i >= 0; i--) {
for (int j = 0; j < input - i; j++) {
System.out.print('C');
}
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print('S');
}
for (int j = 0; j < input - i; j++) {
System.out.print('K');
}
System.out.println();
}
will print for input = 3:
MMMSDDD
MMSSSDD
MSSSSSD
CSSSSSK
CCSSSKK
CCCSKKK
and for input = 6:
MMMMMMSDDDDDD
MMMMMSSSDDDDD
MMMMSSSSSDDDD
MMMSSSSSSSDDD
MMSSSSSSSSSDD
MSSSSSSSSSSSD
CSSSSSSSSSSSK
CCSSSSSSSSSKK
CCCSSSSSSSKKK
CCCCSSSSSKKKK
CCCCCSSSKKKKK
CCCCCCSKKKKKK
I don't know why, but I really wanted it to work with only one set of for-loops:
int number = 8;
for (int i = 0; i < number * 2; i++) {
for (int j = 0; j < (number * 2) + 1; j++) {
System.out.print(
i < number && j+i < number ? 'M' :
i < number && j-i > number ? 'D' :
i < number ? 'S' :
i >= number && i-j >= number ? 'C' :
i >= number && j+i >= number*3 ? 'K' :
'S'
);
}
System.out.println();
}
So for 8 (like in the code) it prints:
MMMMMMMMSDDDDDDDD
MMMMMMMSSSDDDDDDD
MMMMMMSSSSSDDDDDD
MMMMMSSSSSSSDDDDD
MMMMSSSSSSSSSDDDD
MMMSSSSSSSSSSSDDD
MMSSSSSSSSSSSSSDD
MSSSSSSSSSSSSSSSD
CSSSSSSSSSSSSSSSK
CCSSSSSSSSSSSSSKK
CCCSSSSSSSSSSSKKK
CCCCSSSSSSSSSKKKK
CCCCCSSSSSSSKKKKK
CCCCCCSSSSSKKKKKK
CCCCCCCSSSKKKKKKK
CCCCCCCCSKKKKKKKK
...or for 3:
MMMSDDD
MMSSSDD
MSSSSSD
CSSSSSK
CCSSSKK
CCCSKKK

replace every second line of my triangle with a blank line for loops

instead of getting a full triangle with 5 lines of "*" how do I replace every second line with a blank line, e.g. to look like this
5
555
55555
This is my code:
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print("5");
}
System.out.println();
}
you need small modification in your code and you can get what you want.
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
if(i%2==0)
System.out.print("*");
}
System.out.println();
}
check it out.
Just test for i % 2
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print(i % 2 == 0 ? "*" : " ");
}
System.out.println();
}
edit
If you want to print 5 instead or * then modify the code to use 5
Check this:
int num= 5; // 'num' denotes the number of lines
int numOfSpaces;
for(int i = 1; i <= num; i++) {
if( i%2 != 0) // checks whether the line number is odd. If odd, prints '*'
{
numOfSpaces = (num-i) / 2; // find the number of blank spaces
for(int j = 1; j <= numOfSpaces; j++) {
System.out.print(" ");
}
for(int k = 1; k <= i; k++) { // number of '*'s to be printed = current line number = i
System.out.print("*");
}
System.out.println();
}
else{
System.out.println(); // leaves every second line blank (ie.,if line number is even.)
}
}
//try this code. It may help you to achieve your output
[Check screen shot for the output of the below code][1]
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++)
{
if(i%2!=0)
{
System.out.print(" ");
}
else{
System.out.print("* ");
}
}
System.out.println();
}
The Code below is Working now.
A simple if condition will solve your problem. Just check if the
value of i is divisible by 2. If it is true then print a
blank line. Else Print * and you are done!
* will be printed on 1st, 3rd and 5th lines.
Blank Space will be printed on 2nd and 4th lines.
public class PrintPattern{
public static void main(String []args){
int lineNo=0; // New Variable for Line Number (Can Use variable 'i' also)
for(int i = 0; i < 5; i++) {
lineNo++;
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
if(lineNo%2!=0){
for(int k = 0; k <= (i*2); k++)
System.out.print('*'); // Will print '*' (or '5') Sequence for Odd Lines - 1st, 3rd, 5th Lines
System.out.println();
}
else
System.out.println();// Will print Blank Line for Even Lines - 2nd and 4th Lines
}
}
}
Try this
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print("5"); //Use 5 instaed of * to draw
}
System.out.println();
System.out.println(); //This one will solve your problem
}
Output will be:
5
555
55555
5555555
555555555
I think that you can just enter one more print statement inside the loop:
for(int i = 0; i < 5; i++) {
System.out.println();
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print("*");
}
System.out.println();
}
Output
*
***
*****
*******
*********

How to execute a print x amount of times?

What I have:
public static void nestedForLoops() {
int k = 5;
for (int i = 1; i <= 5; i++) {
for (int j = 5; j > i; j--) {
System.out.print("*");
}
System.out.println(i);
}
}
Output:
****1
***2
**3
*4
5
Trying to achieve:
****1
***22
**333
*4444
55555
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
if (5 - j >= i) {
System.out.print("*");
} else {
System.out.print(i);
}
}
System.out.println();
}
Here you are:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
if (i <= 5 - j) {
System.out.print("*");
} else {
System.out.print(i);
}
}
System.out.println();
}
for i in range(1,6,1):
print "*" *(5-i),str(i) *i

Categories

Resources