I have been trying different variations of for loops and have no clue how to make these patterns:
Pattern 1
54321
5432
543
54
5
Pattern
Your inner for loop
for (int j = (rows + 1 - i) ; j > 0 ; j-- ){
System.out.print(j);
}
will always count down to 1, because it keeps going until j is zero. Also, the number that the current row starts on will depend on the current row, because you used i in your assignment to j. To get pattern 1 both of those things will have to change.
Since you posted an attempt for pattern one, I'll tell you a solution for pattern one -
int rows = 5; // <- Start at 5.
for (int i = rows; i > 0; i--) { // <- Use decrementing loop(s).
for (int j = rows; j > rows - i; j--) { // <- Start at 5 (again)
System.out.print(j);
}
System.out.println();
}
Output is pattern 1 in your question,
54321
5432
543
54
5
Try this:
public class NumberPattern {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int k = 2; k >= i; k--) {
System.out.print(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print(j);
}
System.out.println();
}
for (int i = 1; i <= 2; i++) {
for (int k = i; k > 0; k--) {
System.out.print(" ");
}
if (i % 2 != 0) {
for (int j = 1; j <= (2 * i + 1); j++) {
System.out.print(j);
}
} else {
for (int j = 1; j <= (i / 2); j++) {
System.out.print(j);
}
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Triangle {
public static void main(String[ ] args){
Scanner scan = new Scanner(System.in);
System.out.println("enter no.of line you need");
int n = scan.nextInt();
for(int i=1;i<=n;i++){
for(int j=5;j>=i;j--){
System.out.print(j);
}
System.out.println(" ");
}}}
public static void main(String[] args) {
int rows = 5;
System.out.println("------ PATTERN 1 ------");
for (int i = 1 ; i <= rows ; i++){
for (int j = rows; j >= i ; j--){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 2 ------");
for (int i = 1 ; i <= rows ; i++){
int k;
for (k = rows ; k > i; k--){
System.out.print(" ");
}
for (int j = 1; j <= k ; j++){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 3 ------");
for (int i = rows ; i >= 1 ; i--){
int k;
for (k = rows ; k > i; k--){
System.out.print(" ");
}
for (int j = 1; j <= k ; j++){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 4 ------");
int whitespaces = rows/2;
for (int i = 1 ; i <= rows; i++){
// absolute value of whitespaces
int abs_whitespaces =
(whitespaces < 0 ? -whitespaces : whitespaces);
for (int j = 0 ; j < abs_whitespaces ; j++){
System.out.print(" ");
}
for (int j = 1 ; j <= rows - 2 * abs_whitespaces ; j++){
System.out.print(j);
}
whitespaces-=1;
System.out.println();
}
}
The first program is:
class timepass {
public static void main() {
for (int a = -1;a<=5;a++) {
for(int b = 5; b >= a;b--) {
System.out.print("*");
}
System.out.println();
// enter code here
}
}
}
The second program is:
class timepass {
public static void main() {
for(int i = 1;i<= 6;i++) {
for(int j = 1;j<= i ;j++) {
System.out.print("*");
}
System.out.println();
}
}
}