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.

  • 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.

  • 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...

  • 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...

  • 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...

  • #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 = "...

  • Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called...

    Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called hw1 Create a project called hw1 (make sure you select the “Use project folder as root for sources and class files”) Create a class called Hw1 in the hw1 package (make sure you check the box that auto creates the main method). Add a comment to the main that includes your name Write code that demonstrates the use of each of the following basic...

  • For JAVA Please post code in bold. Thank you. Print numbers 0, 1, 2, ..., userNum...

    For JAVA Please post code in bold. Thank you. Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex: userNum = 3 prints: 0 1 2 3 Challenge 4.6.1: Nested loops ndent...

  • write a java program that does the following Part one Use a For loop to compute...

    write a java program that does the following Part one Use a For loop to compute the sum of all the odd numbers from 1 through 99. Your result should be labeled, and the value should be 2500. Print your name 7 times using a While loop. String dogNames[ ] = {"Sam","Buster","Fido","Patches","Gromit","Flicka"}; Using the array defined here, print the values of the array vertically and horizontally using one For-Each loop. Reverse the logic (Nested loops: Indent text) so that the...

  • I know how to do number one just not number 2. Please answer number 2 only...

    I know how to do number one just not number 2. Please answer number 2 only Write a java program that will print out following pattern. Use nested for loop. 2. Add a method to the above program that generates takes two numbers as parameters and prints out all the numbers from number 1 to number 2 separated by using a while loop. (Do not print the last ,'). For example: Calling method1(9,51) should print: 9, 10, 11, 12, 13,...

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