Question

In: Computer Science

Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579...

Print the following two patterns using nested loops. Using java.

Pattern 1

13579

13579

13579

13579

Pattern 2

#####1

### #12

###123

##1234

#12345

Solutions

Expert Solution

import java.util.*;
import java.lang.*;
import java.io.*;

class Patterns
{
   public static void main (String[] args)
   {
   Scanner sc = new Scanner(System.in);
   int n = sc.nextInt();
  
   //Pattern 1
   for(int i = 1; i <= n; i++) // n is the number of rows
   {
   for(int j = 1; j <= 5; j++)
   {
   System.out.print(2 * j - 1); // printing the row
   }
   System.out.println();    //change line
   }
   System.out.println();
   System.out.println();
  
   //Pattern 2
   int p = n;
   for(int i = 1; i <= n; i++) // n is the number of rows
   {
   for(int j = 1; j <= p; j++) // print # decreasing in number
   {
   System.out.print('#');
   }
   p--;
  
   for(int j = 1; j <= i; j++) // every time the number of numbers in row increases
   {
   System.out.print(j);
   }
   System.out.println(); //change line
   }
   }
}


Related Solutions

Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
C++ In this lab you will be using nested for loops to print out stars in...
C++ In this lab you will be using nested for loops to print out stars in a Diamond pattern such as this: * *** ***** ******* ********* *********** ************* *************** ************* *********** ********* ******* ***** *** * For example , if number of rows=8 then the above pattern is derived. You are to take the input for the number of lines(rows) from a file named "input_diamond" and output the pattern into both the terminal and an output file named "output_diamond".
create a function in matlab that sums two m x n matrices using nested loops, then...
create a function in matlab that sums two m x n matrices using nested loops, then returns result into a new matrix. Use nesed for loops to add matrices piece by piece. Basically means, dont program simply A+B Function should perform error check to make sure both matrices have same number of rows/ columns.
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops,...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops, if loops, while loops, arrays and conditional execution, as well as creating an image of the game board. The list below must be considered in the code. - Selecting a word from a dictionary (a text file) - Reading a single letter from the user - Building a character array showing the letters matched so far - Keeping count of the number of guessed...
In this exercise we will practice using nested loops. You will write s program that prompts...
In this exercise we will practice using nested loops. You will write s program that prompts the user to enter an integer between 1 and 9 and displays a pyramid of numbers, as shown in the example below. The program must validate the input given by the user and make sure that: if the user enters a non-integer the program issues an error message and requests the user provides a valid input. if the user does not enter a number...
In C 1- Use nested for loops to create an addition lookup table. Fill in an...
In C 1- Use nested for loops to create an addition lookup table. Fill in an array and print out the following table showing the sum of the row and column numbers 0-9. 0 1 2 3 4.. <- column numbers 1 2 3 4 5.. 2 3 4 5 6.. <- These nos. = Row + Column 3 4 5 6 7.. <- for example 7 = 3 + 4 4 5 6 7 8.. ... ^Row numbers 2-...
. Consider the following sorting procedure. This procedure uses nested loops to make several passes through...
. Consider the following sorting procedure. This procedure uses nested loops to make several passes through the array. Each pass compares successive pairs of elements. If a pair is in increasing order (or the values are equal), the sorting procedure leaves the values as they are. If a pair is in decreasing order, the sorting procedure swaps their values in the array.  The first pass compares the first two elements of the array and swaps their values if necessary....
1. Are there any general patterns to the distribution of the Ensatina salamanders? Describe the pattern...
1. Are there any general patterns to the distribution of the Ensatina salamanders? Describe the pattern that you see. Does the pattern make sense given the information on these salamanders in the background information? Expand on your previous answer. 2. How many distinct populations can you observe based on the location of the specimens (disregard the colors for the time being)? Which colored populations appear to be single population? Explain. Stebbins decided that the Ensatina salamanders were not four separate...
Java Code. Using Decorator pattern make class BurgerToppings: -Each topping is an extra $1 and the...
Java Code. Using Decorator pattern make class BurgerToppings: -Each topping is an extra $1 and the name provided in the constructor -getTotalPrice: returns total price of burger + toppings -getDetails: returns the burger name + name of topping with the word (extra) Code: public interface Burger{ double C(); String getDetails(); } public class BigMac implements Burger{ private String order; private double price ; public BigMac() { this.order= "Big Mac"; this.price = 4.00; } @Override public double getPrice() { return price;...
Java Code. Using Decorator pattern make class BurgerToppings: -Each topping is an extra $1 and the...
Java Code. Using Decorator pattern make class BurgerToppings: -Each topping is an extra $1 and the name provided in the constructor -getTotalPrice: returns total price of burger + toppings -getDetails: returns the burger name + name of topping with the word (extra) Code: public interface Burger{ double C(); String getDetails(); } public class BigMac implements Burger{ private String order; private double price ; public BigMac() { this.order= "Big Mac"; this.price = 4.00; } @Override public double getPrice() { return price;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT