Question

Use a nested For loop in Java to print the following below: 10000 01000 00100 00010...

Use a nested For loop in Java to print the following below:

10000

01000

00100

00010

00001

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

JAVA CODE:

public class PrintOne

{

    // main method begins
     public static void main(String []args)
   
     {
       
    // counter variable for loop
      int i=0,j=0;
    
      //outer for loop
      for(i=0;i<5;i++)
      {
          //inner for loop
          for(j=0;j<5;j++)
          {
            
              // if i and j are equal print 1
            
              if(i==j)
              {
                  System.out.print("1");
              }
            
              //else print 0
              else
              {
                  System.out.print("0");
              }
          }
        
          //moving to the next line
          System.out.println();
        
      }
     }
}

SCREENSHOT FOR OUTPUT:

$javac Printone.java $java PrintOne 10000 01000 00100 00010 00001

Add a comment
Know the answer?
Add Answer to:
Use a nested For loop in Java to print the following below: 10000 01000 00100 00010...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
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