Knowee
Questions
Features
Study Tools

(This is from Programming Exercise P4.1 at the end of Chapter 4 of Java for Everyone.) Write program fragments (i.e., you do not need to write complete programs) with loops that compute:The sum of all even numbers between 2 and 100 (inclusive).The sum of all squares between 1 and 100 (inclusive).All powers of 2 from 20 up to 220 (inclusive).The sum of all odd numbers between a and b (inclusive), where a and b are integer variables with a ≤ b.The sum of all digits at odd positions (right-to-left starting at 1 as the right-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 7 + 6 + 3 = 16.)The sum of all digits at odd positions (left-to-right starting at 1 as the left-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 4 + 2 + 7 = 13.)

Question

(This is from Programming Exercise P4.1 at the end of Chapter 4 of Java for Everyone.) Write program fragments (i.e., you do not need to write complete programs) with loops that compute:The sum of all even numbers between 2 and 100 (inclusive).The sum of all squares between 1 and 100 (inclusive).All powers of 2 from 20 up to 220 (inclusive).The sum of all odd numbers between a and b (inclusive), where a and b are integer variables with a ≤ b.The sum of all digits at odd positions (right-to-left starting at 1 as the right-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 7 + 6 + 3 = 16.)The sum of all digits at odd positions (left-to-right starting at 1 as the left-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 4 + 2 + 7 = 13.)

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here are the Java code fragments for each of the tasks:

  1. The sum of all even numbers between 2 and 100 (inclusive).
int sum = 0;
for (int i = 2; i <= 100; i += 2) {
    sum += i;
}
  1. The sum of all squares between 1 and 100 (inclusive).
int sum = 0;
for (int i = 1; i <= 100; i++) {
    sum += i * i;
}
  1. All powers of 2 from 2^0 up to 2^20 (inclusive).
for (int i = 0; i <= 20; i++) {
    int power = (int) Math.pow(2, i);
    System.out.println(power);
}
  1. The sum of all odd numbers between a and b (inclusive), where a and b are integer variables with a ≤ b.
int sum = 0;
for (int i = a; i <= b; i++) {
    if (i % 2 != 0) {
        sum += i;
    }
}
  1. The sum of all digits at odd positions (right-to-left starting at 1 as the right-most digit) of a numeric input.
int sum = 0;
String numStr = String.valueOf(input);
for (int i = numStr.length() - 1; i >= 0; i -= 2) {
    sum += Character.getNumericValue(numStr.charAt(i));
}
  1. The sum of all digits at odd positions (left-to-right starting at 1 as the left-most digit) of a numeric input.
int sum = 0;
String numStr = String.valueOf(input);
for (int i = 0; i < numStr.length(); i += 2) {
    sum += Character.getNumericValue(numStr.charAt(i));
}

Please replace input with the actual numeric input for the 5th and 6th tasks.

This problem has been solved

Similar Questions

Exercise 1-1: Calculate the sum of square of integers from 1 to 100 using a while loop. Output each integer (from 1 to 100) followed by its sum of squares up to that integer.

Create a program in Java that will accept two integers. If the sum of the integers is odd, display the higher integer, else, display the lower integer.

Write a program to accept an integer N and print the sum of first N odd numbersInput Format:An integer range is given inputOutput Format:Print only calculated sum.Constraints:1 <= N <= 10^16 Sample Input 1:2Sample Output 1:4Sample Input 2:10Sample Output 2:100

Develop a multithreaded Java program with two threads. One threadgenerates even numbers from 1 to 100 and another thread generates oddnumbers from 1 to 100. Ensure that the threads do not interfere.

(This is from Review Exercise R4.1 at the end of Chapter 4 of Java for Everyone.) Assume variables SimpleWriter out and int n are already declared in each case. Write a separate while loop for each of the following tasks:Print all squares less than n. For example, if n is 100, print 0 1 4 9 16 25 36 49 64 81.Print all positive numbers that are divisible by 10 and less than n. For example, if n is 100, print 10 20 30 40 50 60 70 80 90.Print all powers of two less than n. For example, if n is 100, print 1 2 4 8 16 32 64.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.