Computer Science, asked by vasanthij97, 1 year ago

JAVA PROGRAM

NESTED LOOP

1
10
101
1010
10101


harshit438: What is to be done bro ?
vasanthij97: WRITE A PROGRAM FOR THE PATTERN

Answers

Answered by harshit438
39

Explanation:

Java Programs to print pattern 1 10 101 1010 10101

The outer loop counter i ranges from 1 to n. i is used to keep track of line number. Line i contains i numbers. So, the inner loop counter j ranges from 1 to i. If j is odd, we print 1, else we print 0.

import java.util.Scanner;

public class Pattern {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter n: ");

int n = scanner.nextInt();

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= i; j++) {

if (j % 2 == 1) {

System.out.print("1 ");

} else {

System.out.print("0 ");

}

}

System.out.println();

}

}

}

Enter n: 5

1

1 0

1 0 1

1 0 1 0

1 0 1 0 1


vasanthij97: thank you so much
harshit438: It's ok
vasanthij97: and i am not 'bro'
harshit438: then who are you
harshit438: ???
vasanthij97: i said i am not a boy
vasanthij97: and can u answer my other question??
harshit438: sure
harshit438: It's my duty
vasanthij97: thanks
Answered by tanvigupta426
0

Answer:

JAVA PROGRAM

Explanation:

Java exists as a programming language and computing platform first released by Sun Microsystems in 1995. It has developed from modest origins to power a big share of today's digital world, by providing the reliable medium upon which many services and applications are made.

public class Pattern {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter n: ");

int n = scanner.nextInt();

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= i; j++) {

if (j % 2 == 1) {

System.out.print("1 ");

} else {

System.out.print("0 ");

}

}

System.out.println();

}

}

}

Enter n: 5

1

1 0

1 0 1

1 0 1 0

1 0 1 0 1

#SPJ2

Similar questions