Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/* 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building Database Driven Catalogs

Authors: Sherif Danish

1st Edition

0070153078, 978-0070153073

More Books

Students also viewed these Databases questions

Question

Define TRA, or total rewards package.

Answered: 1 week ago

Question

Understand how to design effective service guarantees.

Answered: 1 week ago