Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment lets you get familiar with divide-and-conquer algorithm design.

ID: 3889412 • Letter: T

Question

This assignment lets you get familiar with divide-and-conquer algorithm design. It is worth 5% of your total course marks. Which digit? A single positive integer i is given. Write a program to find the digit located in position i in the following infinite sequence of digits created by juxtaposing the increasing larger sequences of incremented integers 1, 2, 3, .... For example, the first 80 digits of the sequence are as follows: 11212312341234512345612345671234567812345678912345678910123456789101112345678910 The first line of the input (st din/keyboard) contains a single integer n (1 lessthanorequalto n lessthanorequalto 100), the number of test cases, followed by one line for each test case. The line for a test case contains the single integer i (1 lessthanorequalto i

Explanation / Answer

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


public class DemoNumber {

   public static void main(String[] args){
        String s;
        int pos = 0;
        Scanner sc = new Scanner(System.in);
        s = "1";
        int count = 2;
        while (s.length() < 80) { //Making the sequence
           for (int i = 1; i<=count; i++)
               s = s+ String.valueOf(i);
           count++;
        }
        System .out.println("Enter number of test cases :");
        int n = sc.nextInt();
        for (int i = 0; i<n; i++){
            System .out.println("Enter position in the sequence :");
            pos = sc.nextInt();
            if (pos <= s.length()){
               System.out.println(s.charAt(pos-1));
            }
        }
   }
}