Question

I need to print the pattern with numbers from 1 to n. The constraints are between...

I need to print the pattern with numbers from 1 to n.

The constraints are between 1 and 500.

User has to input an integer n.

Example:

input:3

output:

33333

32223

32123

32223

33333

I have not learned functions or arrays at this point in class but if you can help explain how to go about doing this, that would be very helpful.

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

import java.util.Scanner;

class Main{
    public static void main(String[] args) {
        Scanner obj = new Scanner(System.in);
        int n = obj.nextInt();
        //Size of array is 2n-1
        int s = 2*n-1;
        //Find mid index
        int mid = s/2;
        //Array to store ans
        int ans[][] = new int[s][s];
        for(int i=0;i<s;i++){
            for(int j=0;j<s;j++){
                //Fill the array, minimum distance from center
                ans[i][j] = 1+Math.max(Math.abs(i-mid),Math.abs(j-mid));
            }
        }
        //Print the ans
        for(int i=0;i<s;i++){
            for(int j=0;j<s;j++){
                System.out.print(ans[i][j]+" ");
            }
            System.out.println();
        }
    }
}

OUTPUT :

Run Main C:\Program Files \Java\jdk1.8.0_1311binljava .. 54 4 4 4 4 4 4 5 回| | ODI 5 4 3 3 3334 5 543222345 543212345 5 4 3

Add a comment
Know the answer?
Add Answer to:
I need to print the pattern with numbers from 1 to n. The constraints are between...
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
  • I need help with this C++ assignment. Create a program which will implement the sum of...

    I need help with this C++ assignment. Create a program which will implement the sum of the numbers between 1 and n where n is an integer. When completed, the program will allow the user to input a positive integer (n) then the program will add all numbers between 1 and n (including n) and output the sum. For example, if the user enters 8 then the program will sum the numbers 1 through 8 and will output the sum....

  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • In Java: Get an integer n from an input dialog or the console window (with user...

    In Java: Get an integer n from an input dialog or the console window (with user input), print out the following pattern on the console window (you may refer to displaying number-pyramid example shown in class). For example, when n is 3, there are 5 rows in total.               * ***            *****              ***                * When n is 4, there are 7 rows in total.               *             ***           *****         *******           *****             ***

  • I must create a Loop in C++ that will print all prime numbers less than the...

    I must create a Loop in C++ that will print all prime numbers less than the number entertered by the user. 1. Accept the upper limit from the user (as an integer). (5 points) 2. Make sure the number is positive. If it is not, terminate the program. (5 points). 3. Go from 1 to the number. If you happen to find a number that is prime, print it. (35 points) example: Enter the upper limit: 25 The prime numbers...

  • NEED HELP THE PROBLEM-SOLVING PROCESS WITH THE UML DIAGRAM + ACTIVITY DIAGRAMS TO RUN THE CODE...

    NEED HELP THE PROBLEM-SOLVING PROCESS WITH THE UML DIAGRAM + ACTIVITY DIAGRAMS TO RUN THE CODE PROGRAM PLEASE!! Problem1. (20 points) Recursive Coding Problem. Using the Problem Solving Process. Write a Java application that implements these two functions using recursive methods (recursion must be used). First Recursive Function. When one passes an integer as input (n for example), the return should return (output) the sum as follows: 1 + 1/2 + 1/3 + 1/4 + ... 1/(n-1) + 1/n Example:...

  • Need help programing this in C. rinteivsors Print the proper divisors of an integer value The...

    Need help programing this in C. rinteivsors Print the proper divisors of an integer value The program should read a single integer input value, which you can assume will be positive. It should then print a single line of output with all of the proper divisors of the input value in order from least to greatest. A proper divisor d of an integer n is an integer that evenly divides n: i.e., nld is an integer For example, if the...

  • Please help, Array does not print properly. I need to print out the 8 class numbers...

    Please help, Array does not print properly. I need to print out the 8 class numbers entered by the user ! Java only using J option pane, you cannot use ulti or anything else only J option pane Program compiles but does not print the array correctly! import javax.swing.JOptionPane; public class programtrial {    public static void main(String[] args) {    int newclass = 0;    int countclass = 0;    final int class_Max = 8;    int[] classarray =...

  • E COMP 1224 Sec. 1 Spring 2017 Lab 1. I need the solution for question 2...

    E COMP 1224 Sec. 1 Spring 2017 Lab 1. I need the solution for question 2 only please. Thank you I need the solution for question 2 only please. Thank you COMP 1224 Sec. 1 Spring 2017 Lab 1 Your first lab will be similar to the examples discussed in class, covering programming hanction, and recursion. It includes the following tasks Using CdC write loop(s) to control and output a star pattern as follows: giving a integer N (use cin...

  • I need help with this python programming exercise, please! thanks in advance Create a Python script...

    I need help with this python programming exercise, please! thanks in advance Create a Python script file called hw4.py. Add your name at the top as a comment, along with the class name and date. Both exercises should be in this file, with a comment before each of them to mark it. Ex. 1. Write a program that inputs an integer number from the user, then prints a letter "O" in ASCII art using a width of 5 and the...

  • Please, I need help debuuging my code. Sample output is below MINN = 1 MAXX =...

    Please, I need help debuuging my code. Sample output is below MINN = 1 MAXX = 100 #gets an integer def getValidInt(MINN, MAXX):     message = "Enter an integer between" + str(MINN) + "and" + str(MAXX)+ "(inclusive): "#message to ask the user     num = int(input(message))     while num < MINN or num > MAXX:         print("Invalid choice!")         num = int(input(message))     #return result     return num #counts dupes def twoInARow(numbers):     ans = "No duplicates next to each...

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