For assignment 5 you will gain some experience writing simple for, while, and do
ID: 672708 • Letter: F
Question
For assignment 5 you will gain some experience writing simple for, while, and dowhile loops. The program you will write will contain a series of loops. Each loop will perform a specific task based on the instructions below. These will increase in difficulty as you work your way down the list. Please write all loops in one java program in the main method.
– Loop 1 (5 points) Write a for-loop that starts from the number 0 and counts up to 110 by intergers of 2, printing the value on each iteration. (i.e 0, 2, 4, 6, 8, 10... 110)
– Loop 2 (5 points) Write a while-loop that starts from the number 100 and counts down to zero, halving that number on each iteration. Print the value on each iteration. (i.e 100, 50, 25... 0)
– Loop 3 (5 points) Write a do-while loop that prints the first 15 powers of 2 (i.e 1 2 4 8 16 32 ....). Print each power of two. (Cannot use Math.pow or any function that computes powers)
– Loop 4 (6 points) Write a for-loop that starts at the number 100 and ends at the number 250, printing only numbers that are divisible by both 3 and 4.
– Loop 5 (6 points) Write a while-loop that prints the multiples of 9 between 0 and 150. Print each multiple of 9.
– Loop 6 (8 points) Write a for-loop that computes 2^30. Print the resulting value. (You cannot use Math.pow or any function that computes powers)
– Loop 7 (9 points) Write a do-while loop that computes the sum of the following series. Print the resulting value. Let
(n=1 5000) 1/n = 1 + 1/2 + 1/3 + 1/4 + 1/5 + ... + 1/n
– Loop 8 (11 points) Write a while loop that takes the number 574837 and reverses it, making it 738475. Print the reversed number. (HINT : use the modulus and division operators.)
Explanation / Answer
package chegg;
public class Sample {
public static void main(String[] args) {
int i;
System.out.print("LOOP 1: ");
for(i=0; i<=100; i=i+2) {
System.out.print(i+", ");
}
System.out.print(" LOOP 2: ");
i=100;
while(i >= 0) {
if(i==0) {
System.out.print(i+", ");
break;
}
System.out.print(i+", ");
i=i/2;
}
System.out.print(" LOOP 3: ");
i=0;
do {
int power=1;
for(int j=0; j<i;j++){
power = power * 2;
}
System.out.print(power+", ");
i++;
}while(i<15);
System.out.print(" LOOP 4: ");
for(i=100; i<=250; i++) {
if(i%3 == 0 && i%4 == 0) {
System.out.print(i+", ");
}
}
System.out.print(" LOOP 5: ");
i=1;
while(i <= 150) {
if(i%9 == 0) {
System.out.print(i+", ");
}
i++;
}
System.out.print(" LOOP 6: ");
int power = 1;
for(i=1; i<=30; i++) {
power = power * 2;
}
System.out.print("2^30 is = "+power);
System.out.print(" LOOP 7: ");
i=1;
double sum=0;
do {
sum = sum + (1.0/i);
i++;
}while(i<=5000);
System.out.print("Sum of the summation is: "+sum);
System.out.print(" LOOP 8: ");
i=574837;
int rev=0;
while(i != 0) {
rev = rev*10 + (i%10);
i=i/10;
}
System.out.print("Reverse of 574837 is: "+rev);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.