Question
/* Sum from a lowerbound to an upperbound using a while-loop */ public class RunningNumberSum { // Save as RunningNumberSum.java public static void main(String[] args)
/*
Sum from a lowerbound to an upperbound using a while-loop
*/
public class RunningNumberSum { // Save as "RunningNumberSum.java"
public static void main(String[] args) {
int lowerbound = 1; // Store the lowerbound
int upperbound = 1000; // Store the upperbound
int sum = 0; // Declare an int variable "sum" to accumulate the numbers
// Set the initial sum to 0
// Use a while-loop to repeatedly sum from the lowerbound to the upperbound
int number = lowerbound;
while (number <= upperbound) {
sum = sum + number; // Accumulate number into sum
++number; // Next number
}
// Print the result
System.out.println("The sum from " + lowerbound + " to " + upperbound + " is " + sum);
}
}
a)Modify the above program to sum all the numbers from 9 to 888.
b)Modify the above program to sum all the odd numbers between 1 to 1000. (Hint: Change the post-processing statement to "number = number + 2".)
c)Modify the above program to sum all the numbers between 1 to 1000 that are divisible by 7. (Hint: Modify the initialization and post-processing statements.)
d)Modify the above program to find the sum of the square of all the numbers from 1 to 100, i.e. 1*1 + 2*2 + 3*3 +...
e)Modify the above program (called RunningNumberProduct) to compute the product of all the numbers from 1 to 10. (Hint: Use a variable called product instead of sum and initialize product to 1.
Use java
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started