Question

1. Create the following patterns in the terminal using nested for loops (each pattern is 9...

1. Create the following patterns in the terminal using nested for loops (each pattern is 9 x 9 fields)

Your Code should have a main function which asks the user to select which pattern to print (A, B, C, or D)

The rest of the code should be in one function called printPattern, which does not return anything (is void ), but takes one letter as parameter (from the user selection). This letter determines which pattern the function prints to console. (i.e. if someone passes the letter 'C' to the function, this function prints the pattern C to the console)

Name your main class Patterns

any 4 patterns is fine just need to get an idea on how to code it in Java

pattern1 output.

* * * * * * * * 1   

* * * * * * * 2 2.   

* * * * * * 3 3 3.   

* * * * * 4 4 4 4.

* * * * 5 5 5 5 5.

* * * 6 6 6 6 6 6.

* * 7 7 7 7 7 7 7

* 8 8 8 8 8 8 8 8.

9 9 9 9 9 9 9 9 9.   

pattern 2.   

* * * * * * * * 1

* * * * * * * 2 *.

* * * * * * 3 * *.   

* * * * * 4 * * *.

* * * * 5 * * * *.   

* * * 6 * * * * *.   

* * 7 * * * * * *.

* 8 * * * * * * *.

9 * * * * * * * *.

pattern 3.

* * * * * * * * 10.

. * * * * * * * 1 *   

* * * * * * 30 * *.   

* * * * * 2 * * *.

* * * * 50 * * * *.   

* * * 3 * * * * *.   

* * 70 * * * * * *.   

* 4 * * * * * * *.   

  90 * * * * * * * *.

pattern 4

**********

********

. *****

***

*

***

  *****

*******

*********

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

Coding

import java.util.*;
import java.util.Scanner;
class first{
static void function(int i)
{
if (i==1)
{
for(int j=1;j<=9;j++)
{
   for(int k=1;k<=9;k++)
   {
   if(k<=9-j)
   {
       System.out.print("*");
   }
       else
       {
       System.out.print(j);
      
       }
   }
   System.out.println();
}
}
else if(i==2)
for(int j=1;j<=9;j++)
{
   for(int k=1;k<=9;k++)
   {
   if(k<10-j)
   {
       System.out.print("*");
   }
       else if(k==10-j)
       {
       System.out.print(j);
       }
       else
       {
       System.out.print("*");
       }
   }
   System.out.println();
}
else if(i==3)
{
System.out.println("* * * * * * * * 10");
System.out.println(". * * * * * * * 1 * ");
System.out.println("* * * * * * 30 * *");
System.out.println("* * * * * 2 * * *.");
System.out.println("* * * * 50 * * * *. ");
System.out.println("* * * 3 * * * * *. ");
System.out.println("* * 70 * * * * * * ");
System.out.println("* 4 * * * * * * *. ");
System.out.println(" 90 * * * * * * * *.");
}
else if(i==4)
{
       for (int j = 5; j >= 0; j--)
{
for (int k = 0; k <= j; k++)
{
System.out.print("*");
}
System.out.println();
}
for (int j = 0; j<= 5; j++)
{
for (int k = 0; k <= j; k++)
{
System.out.print("*");
}
System.out.println();
}
}
  
else
System.out.println("You have Enter No Pattern");

}

public static void main(String[] args)
{
   Scanner sc = new Scanner(System.in);
   int input;
   System.out.println("Press Enter 1::pattern1");//
   System.out.println("Press Enter 2::pattern2");//
   System.out.println("Press Enter 3::pattern3");//
   System.out.println("Press Enter 4::pattern4");//
   System.out.println("Press Enter Anything Else For Close");//
   input = sc.nextInt();
   function(input);
}
}

output:

if you still have any Problem regarding this question please comment and if you like my code please appreciate me by thumbs up thank you.........

Add a comment
Know the answer?
Add Answer to:
1. Create the following patterns in the terminal using nested for loops (each pattern is 9...
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
  • answer 8 using matlab Using nested loops, traverse through the matrix for the following. a) Replace...

    answer 8 using matlab Using nested loops, traverse through the matrix for the following. a) Replace all elements divisible by 3 with 100. b) Replace all elements divisible by 2 with 50. c) Replace the first element of each row with 10. d) Display the all elements of the 2D matrix as a 10 array (9 elements in 1 row) 8) Write a function file that determines the maximum element of an input matrix A using nested loops and counts...

  • C++ Write code using 2 nested loops, that produces the following output. Write code using 2...

    C++ Write code using 2 nested loops, that produces the following output. Write code using 2 nested loops, that produces the following output. 1 5 4 3 2 1 4 3 2 1 3 2 1 code.cpp New #include <iostream> 2 using namespace std; 3 4 int main() { 5 6 // TODO: your code go 7 8 9 2 1 1 }

  • Design (pseudocode)(name it Pattern) that uses nested loops to print out the following pattern. Document your...

    Design (pseudocode)(name it Pattern) that uses nested loops to print out the following pattern. Document your code properly. 1 2 3 4 5 6 1 2 3 4 5     1 2 3 4       1 2 3         1 2           1

  • MATLAB Write segment of code using nested for loops to print out the following pattern. Your...

    MATLAB Write segment of code using nested for loops to print out the following pattern. Your segments of code should prompt the user for a positive integer, n, and print the following star pattern depending on n. An equilateral triangle. Pictured below is case n = 6. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *...

  • ANSWER IN C PLEASE (: ew History Bookmarks Window Help 13.16. Loops E ry> ECE 270...

    ANSWER IN C PLEASE (: ew History Bookmarks Window Help 13.16. Loops E ry> ECE 270 home > 13.16 Loops-Ex4 zyBooks catalog TO.TU LOOo Write a program that prints a pattern like the ones shown below Ask the user to input the letter that goes in the middle of the last row in the pyramid (uppercase or lowercase). The pattern should extend to the character entered. In the Example 1, the pattern is the result from an input value of...

  • LABORATORY EXERCISES: Using nested for loops, write a java program to print the pattern as shown...

    LABORATORY EXERCISES: Using nested for loops, write a java program to print the pattern as shown in the sample output. The program does the following: It prompts the user to enter an integer between 1 and 10. It outputs the pattern shown in the sample output based on the user input. Sample output: Enter an integer from 1 to 10: 8 The pattern is: x x x x x x x x x x x x x x x x...

  • C++ please 27.5 Loops (nested)**: Sum a given number of integers A user will enter an...

    C++ please 27.5 Loops (nested)**: Sum a given number of integers A user will enter an initial number, followed by that number of integers. Output those integers' sum. Repeat until the initial number is O or negative. Ex: If the user enters 3 96 1 0, the output is 16 Ex: If the user enters 396125 3 0, the output is 16 Hints: Use a while loop as an outer loop. Get the user's initial number of ints before the...

  • Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program...

    Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program will calculate and display the total of all numbers up to a specific number (entered by the user). Only positive numbers are allowed. Part B: User-controlled loop Part A Input Validation loop Part A: Summation loop Examples: When 5 is entered the program will calculate the total as 1+2+...+5 and display 15. When 2 is enterered the program will calculate the total as 1+2...

  • Using java Sample Outputs Write a program which asks the user for an integer and then...

    Using java Sample Outputs Write a program which asks the user for an integer and then prints the following patterns based on that integer. Input Validation For Pattern 1, the input must be a value between 1 and 999 For Pattern 2, the input must be a value between 1 and 26. Requirements: For Pattern 1: must be able to work with up to three digit numbers. You will want to use printf to get the spacing correct. For Pattern...

  • PA 9-1 (25 points) Use nested loops to create a 11x11 grid like the one below....

    PA 9-1 (25 points) Use nested loops to create a 11x11 grid like the one below. Tab in to start each row. Each cell is one row tall and three spaces wide. Dash (-) characters (45) were used to construct the horizontal lines. Using the rand function (hint: rand()%15) fill in all the cells in the grid with characters from ASCII number 33 (!) to 47(/). For full credit, use nested loops and center the digits in cells. Hint: for...

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