#️⃣ C# C language help ulit

Rojen G Embate

Honorary Poster
C language

4. The Final Total​

by CodeChum Admin
An integer variable, total, is initialized to 10 in the code editor. Ask the user to input an integer and check if it is positive, negative, or zero.

if positive: check if it is even or odd. if it is even, add its square to total, otherwise add its cube to total
if negative: ask the user to input another integer as an exponent to this negative number and add the result to total.
if zero: print "Nothing happened."

Lastly, only print the value of total if the inputted integer is positive or negative.
Sample Output 1
Enter an integer: 2
14
Sample Output 2
Enter an integer: 3
37
Sample Output 3
Enter an integer: -2
Enter another integer: 3
2
 
Here's a possible solution to the problem described:

C:
#include <stdio.h>

int main() {
    int total = 10;
    int num;

    printf("Enter an integer: ");
    scanf("%d", &num);

    if (num > 0) {
        if (num % 2 == 0) {
            total += num * num;
        } else {
            total += num * num * num;
        }
    } else if (num < 0) {
        int exponent;
        printf("Enter another integer: ");
        scanf("%d", &exponent);
        total += num * exponent;
    } else {
        printf("Nothing happened.\n");
    }

    if (num > 0 || num < 0) {
        printf("%d\n", total);
    }

    return 0;
}

Explanation:

1. We start by initializing the variable total to 10.
2. We ask the user to input an integer and store it in the variable num.
3. We check if num is positive. If it is, we proceed to check if it's even or odd.
4. If num is even, we add its square (num * num) to total. If it's odd, we add its cube (num * num * num) to total.
5. If num is negative, we ask the user to input another integer (exponent) and store it in the variable exponent. We then add the result of num * exponent to total.
6. If num is zero, we print "Nothing happened."
7. Finally, we only print the value of total if num is positive or negative.
8. The program ends by returning 0.

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

About this Thread

  • 1
    Replies
  • 756
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
364
Guests online
1,070
Total visitors
1,434

Forum statistics

Threads
2,273,591
Posts
28,950,479
Members
1,235,846
Latest member
viscahm16
Back
Top