Write the code necessary to accomplish the following tasks in Java using a for l
ID: 3807018 • Letter: W
Question
Write the code necessary to accomplish the following tasks in Java using a for loop control structure.
Create an int called sum and initialize it to 5000.
Create an int called reps and initialize it to 0.
Use a for loop control structure that runs if num is greater than or equal to 250, subtracting 139 from num each iteration and counting how many iterations it takes to reduce num to be less than 250.
Output the final value of reps.
Output the final value of num.
SAMPLE RUN:
"The number of reps was 35."
"The final value of num is 135"
Explanation / Answer
File: LoopReps
public class LoopReps {
public static void main(String[] args){
int sum = 5000; //initialize sum
int reps = 0; //initialize reps
int num;
// in the for loop, initialize num as sum
// check if num is greater than or equal to 250
// assign new value for num as num - 139
for (num = sum; num >= 250; num-=139){
reps++; //increment reps
}
System.out.println("The number of reps was " + reps); //Display reps
System.out.println("The final value of num is " + num); //Display num
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.