Question

Decode the logic and print the Pattern that corresponds to given input. If N= 3 then...

Decode the logic and print the Pattern that corresponds to given input. If N= 3 then pattern will be : 10203010011012 **4050809 ****607 If N= 4 then pattern will be: 1020304017018019020 **50607014015016 ****809012013 ******10011

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

Explanation of the pattern:
For example, if N=4, there will be 4 sections in the output separated by a space and asteriks.
The first section contains 2*4 numbers, ie., 1 to 4 and 17 to 20, separated by zeroes (1020304017018019020), the second section contains 2*(N-1),ie., 2*3 numbers (5 to 7 and 14 to 16) separated by zeroes (50607014015016), the third sections contains 2*2 numbers (8 to 9 and 12 to 13) separated by zeroes, ie., (809012013), the last section contains 2*1 numbers (10, 11) separated by zero ie., (10011)

Every two sections are separated by a space followed by 2*i asteriks where i starts from 1 and increases by 1 for every gap between two sections

A representation of numbers present in each section in this example

1,2,3,4   17,18,19,20 (Section 1)
5,6,7 14,15,16   (Section 2)
8,9 12,13 (Section 3)
10 11 (Section 4)


Here is the code written in Java.


package com.HomeworkLib.decodepattern;

import java.util.Scanner;

public class DecodePattern {

   public static void main(String[] args) {

       try(Scanner scanner = new Scanner(System.in)){
           System.out.println("Please enter the input value, N: ");
           int N = scanner.nextInt();
           StringBuffer buffer = new StringBuffer();
//To get the maximum value in the pattern
           int maxCount = getMaxCount(N);
           int increasingValue = 1;
           int decreasingValue=maxCount;
           int starCount=1;
          
           for (int i=N; i>=1; i--) {
              
               buffer.append(getSection(increasingValue, i));
               buffer.append("0");
               buffer.append(getSection(decreasingValue-i+1,i));
              
               if (i!=1) {
                   buffer.append(" ");
                   buffer.append(getStars(2*starCount));
               }
              
               starCount++;
               decreasingValue-=i;
               increasingValue+=i;
              
           }
          
           System.out.println("Output: " + buffer.toString());
       }
   }

   private static int getMaxCount(int N) {
       int maxCount = 0;
       for (int i=1; i<=N; i++) {
           maxCount+=i;
       }
      
       maxCount = 2*maxCount;
       return maxCount;
   }
  
   private static String getStars(int n) {
       String nStars="";
       for (int i=1; i<=n; i++) {
           nStars+= "*";
       }
       return nStars;
   }
  
   private static StringBuffer getSection(int start, int length) {
       StringBuffer buffer = new StringBuffer();
       buffer.append(Integer.toString(start));
       for (int i=1; i<length; i++) {
           buffer.append("0");
           buffer.append(Integer.toString(start+i));
       }
       return buffer;
   }

}

Add a comment
Know the answer?
Add Answer to:
Decode the logic and print the Pattern that corresponds to given input. If N= 3 then...
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 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.

  • Which option will print all the elements of rown where n is given as input and...

    Which option will print all the elements of rown where n is given as input and n is less than or equal to 100, from a 100 x 200 two-dimensional list/table? O for i in range (len(a)): print(a[i]) O for tin range (len(a[0])): print(a[n[i]) for i in range (len(a)): print(a[n]) for i in range (len(a[@])) print(a[[n])

  • 3) (3 pts) Given three input switches (A, B and C) and one 7432 logic IC...

    3) (3 pts) Given three input switches (A, B and C) and one 7432 logic IC (a quad 2-input OR shown below). How many ways can the input switches and the IC be wired to physically realize the following: F = A + B + C. Show your work. 1 GND

  • LZW CODING LZW coding the initial dictionary is given: Index Symbol space 3 R. Decode the...

    LZW CODING LZW coding the initial dictionary is given: Index Symbol space 3 R. Decode the received sequence: 3,1,4,6,8,4,2,1,2,5,10. Continue building the dictionary while decoding. There is one place you could be confused. The appearance of 8 in the above sequence is not a mistake: remember that in the dictionary before adding 3-character pattern, you add 2-character pattern. That is, while decoding and before referring to index 8, you will add entries 7 and 8.

  • please write c programming for this code below. Write a program to print a pattern of...

    please write c programming for this code below. Write a program to print a pattern of numbers separated by spaces for the given number of rows. At the time of execution, the program should print the message on the console as: Enter number of rows For example, if the user gives the input as: Enter number of rows : 4 then the program should print the result as: 232 34543 4567654

  • 1. 2. 3. N-bit LED display, which continuously displays a light pattern using a clock syn-...

    1. 2. 3. N-bit LED display, which continuously displays a light pattern using a clock syn- chronous sequential circuit. For example, a 4-bit LED pattern will be 1000 → 0100 → 0010 → 0001 → 0010 → 0100 → 1000 → 0100 ··· Design of the LED display. Design a synchronous sequential circuit that can output the 4-bit LED pattern 1000 → 0100 → 0010 → 0001 → 0010 → 0100 → 1000 → 0100 ··· Briefly justify your design...

  • In Java We want to print the character “*” as the following pattern. For example, for...

    In Java We want to print the character “*” as the following pattern. For example, for n = 5, we have a 5*5 square in the middle and then for 5 rows and columns no character is printed and then then boundary is created using the 5 asterisks. Write a function that gets the odd number input argument n and creates the pattern above. Test your program with n = 3 and 5. Your code for this problem -- Copy...

  • PLEASE READ THE FULL INSTRUCTION. Write a Java programme to print the pattern of asterisks shown...

    PLEASE READ THE FULL INSTRUCTION. Write a Java programme to print the pattern of asterisks shown below. For i=1 For i=2 * * For i=3 For i=n ** * ** ** ......... * (n asterisks) Your program should prompt the user to enter the value of n and then draw the corresponding star pattern. Remember if n is 5, you will have to print five patterns for i=1, i=2, i=3, i=4, and i=5. If n is 3, you will have...

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

  • D Question 16 Given the logic circuit below, determine the output for the given input combination.

    D Question 16 Given the logic circuit below, determine the output for the given input combination.

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