Answer to Question #269297 in C for Neil

Question #269297

Instructions:

  1. Input four float numbers; they could be positive or negative.
  2. Only add all inputted negative numbers, and print its sum, up to 2 decimal place.
  3. Hint: The previous/latest topic is about if-else-elseif statements, but do we need one here?


Input


1. First decimal number

2. Second decimal number

3. Third decimal number

4. Fourth decimal number

Output


The first four lines will contain message prompts to input the four decimal numbers.

The last line contains the sum of all the inputted negative numbers, with 2 decimal places.

Enter the first number: -30.22
Enter the second number: 10.5
Enter the third number: -2.2
Enter the fourth number: -1.8
Sum of all negatives = -34.22


Enter the first number: 50.23
Enter the second number: 100.23
Enter the third number: -123.4
Enter the fourth number: 0.0
Sum of all negatives = -123.40
1
Expert's answer
2021-11-21T17:37:45-0500
#include<stdio.h>
int main()
{
float N1, N2, N3, N4;
printf("Enter first number: ");
scanf("%f",&N1);
printf("Enter second number: ");
scanf("%f",&N2);
printf("Enter third number: ");
scanf("%f",&N3);
printf("Enter fourth number: ");
scanf("%f",&N4);
float total = 0;
if (N1 < 0) {
    total += N1;
}
if (N2 < 0) {
    total += N2;
}
if (N3 < 0) {
    total += N3;
}
if (N4 < 0) {
    total += N4;
}
printf("%.2f\n", total);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS