Question

Write a java program that will print out the following patters. Use nested for loop.101101010110101

Write a java program that will print out the following patters. Use nested for loop.
1
01
101
0101
10101

0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

The below java program with appropriate comments explaining the logic within the for loops will print the given pattern (here n=5, the number of lines to be printed)

PROGRAM :

public class Solution
{
public static void main(String[] args) {
int n = 5; // number of lines
for (int i = 1; i <= n; i++) { // loop for controlling number of lines (5 in this case)
for (int j = 1; j <= i; j++) { // loop for controlling number of digits per line (vary with each line)
if ((i + j) % 2 == 0) { // condition check, if i+j is divisible by two - print 1 otherwise print 0
System.out.print("1 ");
} else {
System.out.print("0 ");
}
}
System.out.println();
}
}
}

Add a comment
Know the answer?
Add Answer to:
Write a java program that will print out the following patters. Use nested for loop.101101010110101
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT