Question

C programming! Write a program that reads integers until 0 and prints the sum of values...

C programming!

Write a program that reads integers until 0 and prints the sum of values on odd positions minus the sum of values on even positions. Let ?1, ?2, … , ??, 0 be the input sequence. Then the program prints the value of ?1 − ?2 + ?3 − ?4 + ⋯ ??. The input is a sequence of integers that always contains at least 0 and the output will be a single number. For example, for input 2 5 7 0, the program will print 4 because 2-5+7=4. If the input is only 0, the program will print 0.

Example of input: 5 19 12 0

Corresponding output: -2

Example of input: 10 8 9 11 24 0

Corresponding output: 24

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

int main()
{
    int sum = 0, n, count = 0;
    
    printf("Enter input: ");
    scanf("%d",&n);
    
    while(n!=0){
        if(count%2==0){
            sum += n;
        }
        else{
            sum -= n;
        }
        scanf("%d",&n);
        count++;
    }
    
    printf("Sum: %d\n",sum);
    return 0;
}

Output:

Enter input: 5 19 12 0 Sum: -2

Note: Please comment below if you have any doubts. upuote the solution if it helped. Thanks!

Add a comment
Know the answer?
Add Answer to:
C programming! Write a program that reads integers until 0 and prints the sum of values...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT