Question

Create in Java: 1. Create a (nested) for loop that given a number prints a line with the number repeated as many times as the
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

import java.util.Scanner;

public class NestedLoops {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a positive integers: ");
        int n = in.nextInt();

        // 1)
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print(i);
            }
            System.out.println();
        }
        System.out.println();

        // 2)
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print(i);
            }
            if (i % 2 == 1)
                System.out.println(" (odd)");
            else
                System.out.println(" (even)");
        }
        System.out.println();

        // 3)
        for (int i = 1; 2 * i - 1 <= n; i++) {
            for (int j = 0; j < 2 * i - 1; j++) {
                System.out.print(2 * i - 1);
            }
            System.out.println();
        }
        System.out.println();
    }
}

Enter a positive integers: 5 22 333 4444 55555 1 (odd) 22 (even) 333 (odd) 4444 (even) 55555 (odd) 333 55555


Please let me know if you have any doubts Please upvote this answer. Thanks!!

Add a comment
Know the answer?
Add Answer to:
Create in Java: 1. Create a (nested) for loop that given a number prints a line...
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
  • in java Use a for loop and nested if statements to determine if a number is...

    in java Use a for loop and nested if statements to determine if a number is even or prime Use the numbers 30 to 60 Hint: you may need to review what is a prime number Output: 30 . . . even 31 . . . prime number 32 . . . even 33 . . . 34 . . . even 35 . . . 36 . . . even 37 . . . prime number etc.

  • 2. Create a while loop which starts x at 1, and prints x: “Are we there...

    2. Create a while loop which starts x at 1, and prints x: “Are we there yet?” until x reaches 10. (Don’t forget to Increment x by 1 each while loop). To make things interesting, include the line readline(prompt = "Press [enter] to continue") after the print statement stated above. If x is an even number, print x: “Even Number Encountered” And go to the next iteration of the while loop (i.e. No readline – as above)

  • Create a while loop that prints the EVEN numbers between 2-50. Format these numbers such that...

    Create a while loop that prints the EVEN numbers between 2-50. Format these numbers such that one number per line.

  • Using R, Create a while loop which starts x at 1, and prints x: “Are we...

    Using R, Create a while loop which starts x at 1, and prints x: “Are we there yet?” until x reaches 10. (Don’t forget to Increment x by 1 each while loop). To make things interesting, include the line readline(prompt = "Press [enter] to continue") after the print statement stated above. If x is an even number, print x: “Even Number Encountered” And go to the next iteration of the while loop (i.e. No readline – as above)

  • 5. Create a java application that uses nested loops to collect data and calculate the average...

    5. Create a java application that uses nested loops to collect data and calculate the average rainfall over a peniod of years First the program should ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month, the program should display the number of months, the total...

  • Javascript: Create a loop that counts from 1 to 20. // If the current number is...

    Javascript: Create a loop that counts from 1 to 20. // If the current number is even, add it to this empty array: var myArray = []; // If the current number is odd, log an odd number warning to // the console. // However, if the number equals 19, set a debug breakpoint. // After the loop completes, print the array to the console.

  • Java use for loop (nested for loop) if/else possible on blue J. (row *col)%2 for 0's and 1's.

    Java use for loop (nested for loop) if/else possible on blue J. (row *col)%2 for 0's and 1's. Write a program to generate the below table: . The table should be able to vary in size based on a class constant called SIZE; minimum of 1, maximum of 9. . The O's and 1's can and should be generated by evaluating a mathematical expression using the row and column numbers Think about the operators: +-* / % . The output...

  • Write a script that works in vim: We are given a Java program RandomTest.java that generates...

    Write a script that works in vim: We are given a Java program RandomTest.java that generates specified number of random integers in the range [0...99]. The program prints FAIL if either 0 or 99 is generated (along with a count of how many times). Otherwise it prints PASS. The program takes the number of random integers to generate as a command line argument as shown below. Note that the given results each time is is run. [an@localhost ~] java Random...

  • [loops] Write a java program that accepts a number, as command line argument and prints out...

    [loops] Write a java program that accepts a number, as command line argument and prints out “Hello, World!” that many times using a for loop. E.g. if the input number is 3, the program outputs: Hello, World! Hello, World! Hello, World! Keep it simple, please use the argument, not interact with the console. Thanks

  • #1. Operators and values, expressions Please correct the following code to make it print the given...

    #1. Operators and values, expressions Please correct the following code to make it print the given expressions and their answers well (3pts). public class DataTypes {    public static void main(String[] args) {       System.out.println("5*8/6 = " + 5*8/6);       System.out.println("(2*6)+(4*4)+10 = " + (2*6)+(4*4)+10);       System.out.println("6 / 2 + 7 / 3 = " + 6 / 2 + 7 / 3);       System.out.println("6 * 7 % 4 = " + 6 * 7 % 4 );       System.out.println("22 + 4 * 2 = "...

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