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

1) Suppose you take the number 200, and you do repeated doubling: 200-400-800-16

ID: 3539599 • Letter: 1

Question

1) Suppose you take the number 200, and you do repeated doubling: 200-400-800-1600-3200 and so forth. With a start value of 200, you need 4 doubling steps to exceed 2000.

Write a code fragment that counts and then prints the number of doubling steps you need, starting from 200, in order to exceed 2000000 (two million).

You must use a while loop in your solution.

Note: you only need to write a code fragment here (including a while loop) - you don't need to place the loop inside a method or class.



2) Suppose s is any String, which you should assume has been declared and initialized. Write a loop that prints the characters in s in a row, but with each character preceded by its position in the string. For example:

if s = "meek", with m at string position 0, e at 1, e at 2, and k at 3, your code should print 0m1e2e3k

if s = "Hip-hop", your code should print 0H1i2p3-4h5o6p

Note: you only need to write the loop itself - you don't need to place the loop inside a method or class.

Explanation / Answer

Just the loop part is here.



String str = "Hip-hop";

String modifiedString = null;

int counter = 0;

char[] charArray = str.toCharArray();

for (int i = 0; i< charArray.length; i++)

{ modifiedString += counter;

modifiedString += charArray[i];

counter++;

}

modifiedString = modifiedString.substring(4);







Here is the sample output


0H1i2p3-4h5o6p





You can change and put any String. You ll get perfect output.






In case you want the full code. Wanna run and check for output, here it is




public class string {

public static void main(String[] args) {

String str = "Hip-hop";

String modifiedString = null;

int counter = 0;

char[] charArray = str.toCharArray();

for (int i = 0; i< charArray.length; i++)

{ modifiedString += counter;

modifiedString += charArray[i];

counter++;

}

modifiedString = modifiedString.substring(4);

System.out.println(modifiedString);

}

}