Given four positive integers a, b, c, and d, compute the sum of those among them that are odd. Because you have not yet learned how to make decisions, use the fact that for a positive integer n, the expression n % 2 is either 0 or 1. (not allowed to use an if statement or conditonal)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Given four positive integers a, b, c, and d, compute the sum of those among them that are odd.

Because you have not yet learned how to make decisions, use the fact that for a positive integer n, the expression n % 2 is either 0 or 1. (not allowed to use an if statement or conditonal) 

 

## Practice 2.2: Arithmetic

### Objective:
1. Given four positive integers \(a\), \(b\), \(c\), and \(d\), compute the sum of those among them that are odd.

### Concept:
Because you have not yet learned how to make decisions, use the fact that for a positive integer \(n\), the expression \(n \% 2\) is either 0 or 1.

### Code Example: OddSum.java

```java
public class OddSum
{
    public static void main(String[] args)
    {
        // These values will be changed during testing
        int a = 13;
        int b = 10;
        int c = 9;
        int d = 2;

        int oddSum = 0;
        // Checking for odd number
        if(a % 2 != 0)
            // If number is odd then add it to oddSum
            oddSum += a;
        if(b % 2 != 0)
            oddSum += b;
        if(c % 2 != 0)
            oddSum += c;
        if(d % 2 != 0)
            oddSum += d;

        System.out.print("Odd sum: ");
        System.out.println(oddSum);
    }
}
```

### Explanation:

1. **Initialization**:
   - Four integers \(a\), \(b\), \(c\), and \(d\) are initialized with values 13, 10, 9, and 2 respectively.

2. **OddSum Calculation**:
   - A variable `oddSum` is initialized to 0.
   - The `if` statements check if the given integer is odd by using the modulus operator `%`.
   - For each odd number (i.e., where \(n \% 2\) is not equal to 0), the value is added to `oddSum`.

3. **Output**:
   - The sum of all odd integers among \(a\), \(b\), \(c\), and \(d\) is printed.

This program provides a simple method to sum up odd integers from a set of four numbers using basic arithmetic and modulus operator for determining odd numbers.
Transcribed Image Text:## Practice 2.2: Arithmetic ### Objective: 1. Given four positive integers \(a\), \(b\), \(c\), and \(d\), compute the sum of those among them that are odd. ### Concept: Because you have not yet learned how to make decisions, use the fact that for a positive integer \(n\), the expression \(n \% 2\) is either 0 or 1. ### Code Example: OddSum.java ```java public class OddSum { public static void main(String[] args) { // These values will be changed during testing int a = 13; int b = 10; int c = 9; int d = 2; int oddSum = 0; // Checking for odd number if(a % 2 != 0) // If number is odd then add it to oddSum oddSum += a; if(b % 2 != 0) oddSum += b; if(c % 2 != 0) oddSum += c; if(d % 2 != 0) oddSum += d; System.out.print("Odd sum: "); System.out.println(oddSum); } } ``` ### Explanation: 1. **Initialization**: - Four integers \(a\), \(b\), \(c\), and \(d\) are initialized with values 13, 10, 9, and 2 respectively. 2. **OddSum Calculation**: - A variable `oddSum` is initialized to 0. - The `if` statements check if the given integer is odd by using the modulus operator `%`. - For each odd number (i.e., where \(n \% 2\) is not equal to 0), the value is added to `oddSum`. 3. **Output**: - The sum of all odd integers among \(a\), \(b\), \(c\), and \(d\) is printed. This program provides a simple method to sum up odd integers from a set of four numbers using basic arithmetic and modulus operator for determining odd numbers.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Constants and Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education