Question

In: Computer Science

Write a loop for each of the following: The sum of all even numbers between 2...

  1. Write a loop for each of the following:
    1. The sum of all even numbers between 2 and 100 (inclusive).
  1. The sum of all squares between 1 and 100 (inclusive). (If you use the pow function the first parameter must be a float or double).
  1. The sum of all odd numbers between a and b (inclusive). Where a and b are read in by the user.
  1. How often do the following loops execute? Assume that i is not changed in the loop body.
    1. for (int i = 1; i <= 10; i++) . . .
    2. for (int i = 0; i < 10; i++) . . .
    3. for (int i = 10; i > 0; i--) . . .
    4. for (int i = -10; i <= 10; i++) . . .
    5. for (int i = -10; i <= 10; i = i + 2) . . .

Help, please!

Solutions

Expert Solution

a)

int sum = 0;

for(int i =2;i<=100;i+=2) {
           sum+=i;
       }

b)

int sum = 0;

for (int i = 1; i <= 100; i += 1) {
           sum += i * i;
       }

c)

Scanner scan = new Scanner(System.in);
       int sum = 0;

// Take input
       int a = scan.nextInt();
       int b = scan.nextInt();
      
       // Iterate loop from a to b
       for (int i = a; i <= b; i++) {
           // If number is not divisible by 2 means it's odd
           // Add it to sum
           if (i % 2 != 0) {
               sum += i;
           }
       }

a)

This loop will run 10 times from 1 to 10

b)

This loop will run 10 times from 0 to 9

c)

This loop will run 10 times from 10 to 1

d)

This loop will run 21 times from -10, -9,-8......,-1,0,1,2.....,10

e)

This loop will run 11 times from -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10


Related Solutions

Write a loop that will calculate the sum of all even numbers from 2 to 30...
Write a loop that will calculate the sum of all even numbers from 2 to 30 ( including 30) store the result in the variable called thirtySum. Declare and initialize all variables. Answer using programming in c.
Write assembly instructions that compute the following: The sum of all even numbers between 2 and...
Write assembly instructions that compute the following: The sum of all even numbers between 2 and 100 (inclusive) -- the answer should be 2550 Prompt the user for 2 values, put them in variables a and b, then find the sum of all odd numbers between a and b. The sum of all the squares between 1 and 100 (inclusive) . -- the answer should be 338350 All of the statements above should print the answers using print_int MUST BE...
Q1) Write programs with iterators that computes: • The sum of all even numbers between 2...
Q1) Write programs with iterators that computes: • The sum of all even numbers between 2 and 100 (inclusive). • The sum of all squares between 1 and 100 (inclusive). • The product of all powers of 2 from 20 up to 220 . • The product of all odd numbers between a and b (inclusive), where a and b are inputs. sum would be 3 + 7 + 7 = 17.) Q2) Recall the Fibonacci sequence: 1, 1, 2,...
Use the following two loop strategies to write a function that sums all even numbers before...
Use the following two loop strategies to write a function that sums all even numbers before an odd number is observed in any given numeric vector, and test your code in R. For example, if the input vector is 2, 4, -2, 3, 4, 5, then the first odd number appears in the fourth position, which is 3. The output should be the sum of the first to the third numbers, which will be 2 + 4 − 2 =...
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
IN C++: Using a single for loop, output the even numbers between 2 and 1004 (inclusive)...
IN C++: Using a single for loop, output the even numbers between 2 and 1004 (inclusive) that iterates (loops) exactly 502 times. The outputted numbers be aligned in a table with 10 numbers per row. Each column in the table should be 5 characters wide. Do not nest a loop inside of another loop.
Input 10 integers and display the following: a. the sum of even numbers. b. the sum...
Input 10 integers and display the following: a. the sum of even numbers. b. the sum of odd numbers. c. the largest integer d. the smallest integer e. the total count of even numbers f. the total count of odd numbers. Using C++ program with for loop..
Use a For loop to compute the sum of all the odd numbers from 1 through...
Use a For loop to compute the sum of all the odd numbers from 1 through 99. Your result should be labeled, and the value should be 2500. Print your name 7 times using a While loop. String dogNames[ ] = {"Sam","Buster","Fido","Patches","Gromit","Flicka"}; Using the array defined here, print the values of the array vertically and horizontally using one For-Each loop. Reverse the logic for zyBook Challenge activity 4.6.1 (Nested loops: Indent text) so that the first line is indented userNum...
Write a Java program using using WHILE loop to find the sum of all integers between...
Write a Java program using using WHILE loop to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
Write a complete program to sum the following series: (hint: use the for loop) 1 /2...
Write a complete program to sum the following series: (hint: use the for loop) 1 /2 + 1/ 4 + 1 /6 + ⋯ + 1 /100 PS: use C++ language please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT