Question

Write a section of Java code using while, do-while and for loops Write a section of...

Write a section of Java code using while, do-while and for loops

Write a section of Java code that will print out random integers between 1 and 100 while N is less than or equal to 5.

Sample output

>

random # 1 is: 73

random # 2 is: 68

random # 3 is: 76

random # 4 is: 64

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Random;

//Main.java
public class Main {
  public static void main(String[] args) {
    Random random = new Random();

    //while loop
    int N = 1;
    while(N<5){
      System.out.println("random # "+N+" is: "+(random.nextInt(100)+1));
      N++;
    }
    System.out.println();

    //do-while loop
    N = 1;
    do{
      System.out.println("random # "+N+" is: "+(random.nextInt(100)+1));
      N++;
    }while(N<5);
    System.out.println();

    //for loop
    for(N = 1;N<5;N++){
      System.out.println("random # "+N+" is: "+(random.nextInt(100)+1));
    }while(N<5);
    System.out.println();
  }
}



Add a comment
Know the answer?
Add Answer to:
Write a section of Java code using while, do-while and for loops Write a section of...
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
  • JAVA turn this psuedo code into java code. USING NO LOOPS! ALL LOOPS MUST BE TURNED...

    JAVA turn this psuedo code into java code. USING NO LOOPS! ALL LOOPS MUST BE TURNED INTO RECURSIVE CALLS. English: 1. Prompt for and read a number between 1 and 5. Repeat this step until the input is 1..5. 2. Repeat the following multiple times according to the number read in step 1. a. Read in a list of integers ending with a 0. The 0 marks the end of the input and is not considered part of the list...

  • Please do this in C++ using only for loops, while loops, and do while loops. No...

    Please do this in C++ using only for loops, while loops, and do while loops. No arrays. Use for loop, while loop, and do while loops to generate a table of decimal numbers, as well as the binary, octal, and hexadecimal equivalents of the decimal numbers in the range 1-256 Note: See Appendix for number systems The program should print the results to the Console without using any string formats Design Details: The Main program should prompt the user for...

  • This is java. Please let me know about those code. Please do not use array and...

    This is java. Please let me know about those code. Please do not use array and scanner. Just do by loop. // a) Task: Find the sum of all multiples of 3 and 5 less than 1000. // b) Task: Given two integers k and n, print out every multiple of k less than n // c) Task: Given an integer n, print out k^k for every k=1...n // d) Task: Write a function called "factorProduct" that takes as input...

  • Write a single program in java using only do/while loops for counters(do not use array pls)...

    Write a single program in java using only do/while loops for counters(do not use array pls) for the majority of the program and that asks a user to enter a integer and also asks if you have any more input (yes or no) after each input if yes cycle again, if not the program must do all the following 4 things at the end of the program once the user is finished inputting all inputs... a.The smallest and largest of...

  • .Need code for my java class !! Write a simple java program that uses loops. You...

    .Need code for my java class !! Write a simple java program that uses loops. You may use ‘WHILE’ or ‘FOR’ or ‘DO-WHILE’ – you decide. The program should use a loop to compute the semester average for up to 5 students. In the loop the user will ask for a student name and their 3 test scores and add them up and divide the total by 3 giving the semester average. You will print out the student name and...

  • Java questions Part 1 Express the code fragment shown below using only a for loop. int...

    Java questions Part 1 Express the code fragment shown below using only a for loop. int n = 0; while (n <= 100) { int sum = sum + n; n = n + 1; System.out.println (sum); } Part 2 Use a loop structure to determine and print all positive integers between 1 and 100 that are divisible (evenly) by 5 and 8

  • Note: Write the pseudocode separately and the source code separately in java using the loops method...

    Note: Write the pseudocode separately and the source code separately in java using the loops method according to the question. Please make sure there is no errors. Program 0 (Warm up): Gimme a cookie! In the early days of computing, you likely couldn't afford your own computer. Instead, you had to sit at a terminal - which was literally just a monochrome monitor and keyboard that was connected to a big mainframe machine (hidden somewhere in the building). When you...

  • Java programming: Identify the true statements regarding Java loops. Select ALL that apply. A. A do-while...

    Java programming: Identify the true statements regarding Java loops. Select ALL that apply. A. A do-while loop always runs at least once. B.Loops can be nested in other loops to any depth as desired. C. While loops are best when the number of loop iterations can be predetermined. D. A while loop might not run at all. E. For loops are more susceptible to endless loop coding errors than while loops.

  • in c++ language 1.Re-write the following for loop statement by using a while loop statement: int...

    in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){                 sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() {    int score, highest;             //missing portion of the program             return 0;    } 1(c). Find the missing portion of the following code, so the...

  • How do I write a java code that mimics charAt without using java API just primitives...

    How do I write a java code that mimics charAt without using java API just primitives and no charAt to be used? I know it comes from primitives but I am confused on how to assemble the loops to derive my own charAt code

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