Skip to content

Instantly share code, notes, and snippets.

@L8RFN
Created April 23, 2023 10:53
Show Gist options
  • Save L8RFN/d48cf746146892ba13de54db865fcde9 to your computer and use it in GitHub Desktop.
Save L8RFN/d48cf746146892ba13de54db865fcde9 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
System.out.println("Please enter n");
int n = cin.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
// If the row number is odd
if (i % 2 == 1) {
// If the column number is odd, print 1, else print 0
if (j % 2 == 1) {
System.out.print(1);
} else {
System.out.print(0);
}
} else {
// If the row number is even
// If the column number is even, print 1, else print 0
if (j % 2 == 0) {
System.out.print(1);
} else {
System.out.print(0);
}
}
} // }فكرة الحل هي اذا كان السطر زوجي اطبع واحد بالعواميد الزوجية واذا كان السطر فردي اطبع واحد في العواميد الفردية
// Print a newline after each row
System.out.println();
}
}
}
/*
another way from chatGPT.
import java.util.Scanner;
public class PatternPrinter {
public static void main(String[] args) {
// Create a scanner to read user input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the number of rows for the pattern
System.out.print("Enter the number of rows for the pattern: ");
int rows = scanner.nextInt();
// Check if the input is a positive integer
if (rows <= 0) {
System.out.println("Error: Invalid input. Please enter a positive integer.");
} else {
System.out.println("Pattern:");
// Iterate through the rows
for (int i = 1; i <= rows; i++) {
// Iterate through the columns, up to the current row number (i)
for (int j = 1; j <= i; j++) {
// Calculate and print the value at the current position (i, j)
// The sum of the row number (i) and column number (j) modulo 2 gives the desired pattern
System.out.print((i + j) % 2);
}
// Print a newline after each row
System.out.println();
}
}
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment