What's new

C# Pa help po nito

Rojen G Embate

Honorary Poster
Joined
Mar 12, 2018
Posts
275
Reaction
217
Points
209
Age
19

3. Add if Even, Subtract if Odd​

An integer total is initialized in the code editor.

Ask the user to input two positive integers and for each values, add it to total if it is even, otherwise subtract.
Print the final value of total on the next line.
Sample Output 1
Enter first positive integer: 10
Enter second positive integer: 20
80
Sample Output 2
Enter first positive integer: 5
Enter second positive integer: 6
51
Sample Output 3
Enter first positive integer: 15
Enter second positive integer: 25
10
 
Sure! Here's a Python code that will accomplish the task:

Python:
total = 0

first_integer = int(input("Enter first positive integer: "))
second_integer = int(input("Enter second positive integer: "))

if first_integer % 2 == 0:
    total += first_integer
else:
    total -= first_integer

if second_integer % 2 == 0:
    total += second_integer
else:
    total -= second_integer

print(total)

In this code, we first initialize the variable total to 0. Then, we ask the user to input two positive integers and store them in the variables first_integer and second_integer.

Next, we use if statements to check if each input integer is even or odd. If an integer is even, we add it to total using the += operator. If it's odd, we subtract it from total using the -= operator.

Finally, we print the value of total, which represents the sum or difference of the two input integers based on their evenness or oddness.

I hope this helps! Let me know if you have any further questions.
 
Sure! Here's a Python code that will accomplish the task:

Python:
total = 0

first_integer = int(input("Enter first positive integer: "))
second_integer = int(input("Enter second positive integer: "))

if first_integer % 2 == 0:
    total += first_integer
else:
    total -= first_integer

if second_integer % 2 == 0:
    total += second_integer
else:
    total -= second_integer

print(total)

In this code, we first initialize the variable total to 0. Then, we ask the user to input two positive integers and store them in the variables first_integer and second_integer.

Next, we use if statements to check if each input integer is even or odd. If an integer is even, we add it to total using the += operator. If it's odd, we subtract it from total using the -= operator.

Finally, we print the value of total, which represents the sum or difference of the two input integers based on their evenness or oddness.

I hope this helps! Let me know if you have any further questions.
c language code
 
c language code
Certainly! Here's an equivalent code in the C programming language:

C:
#include <stdio.h>

int main() {
    int total = 0;
    int first_integer, second_integer;

    printf("Enter first positive integer: ");
    scanf("%d", &first_integer);

    printf("Enter second positive integer: ");
    scanf("%d", &second_integer);

    if (first_integer % 2 == 0) {
        total += first_integer;
    } else {
        total -= first_integer;
    }

    if (second_integer % 2 == 0) {
        total += second_integer;
    } else {
        total -= second_integer;
    }

    printf("%d\n", total);

    return 0;
}

In this C code, we start by declaring and initializing the total variable to 0. Then, we ask the user to input two positive integers using printf() and scanf() functions.

Next, we use if statements to check if each input integer is even or odd. If an integer is even, we add it to total using the += operator. If it's odd, we subtract it from total using the -= operator.

Finally, we use printf() to print the value of total, which represents the sum or difference of the two input integers based on their evenness or oddness.

I hope this helps! Let me know if you have any further questions.
 

C language​

1. Odd, Even, or Zero?​


Ask the user to input a single integer.
Check and print on the next line if the input is even, odd, or zero.
Sample Output 1
Enter an integer: 5
Odd
Sample Output 2
Enter an integer: 0
Zero
Sample Output 3
Enter an integer: 6
Even
 

Similar threads

Back
Top