Answer to Question #340186 in C for AsdBro

Question #340186

4. Negative Fusion

by CodeChum Admin

As I've said before, there are three types of numbers: the positive, the zero, and the negative ones. Today, we shall pay attention only to the negative ones now. Make a loop that will accept random decimal/float numbers. When the user inputs 0, the loop will terminate and then output the sum of all negative numbers inputted in 3 decimal places.


Let's code this.


Input


1. A series of decimal numbers

Output


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

The last line contains the sum of all negative numbers with 3 decimal places.



Enter·a·number:·2.4434
Enter·a·number:·-1.3433
Enter·a·number:·-2.444
Enter·a·number:·6.432
Enter·a·number:·0
Sum·of·all·negatives·=·-3.787
1
Expert's answer
2022-05-14T16:42:13-0400
#include <stdio.h>

int main()
{
	float n, sum = 0;
	for (;;)
	{
		printf("Enter a number: ");
		scanf("%f", &n);
		if (n < 0)
			sum += n;
		if (n == 0)
			break;
	}

	printf("Sum of all negatives = %.3f \n", sum);

	return 0;
}

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
APPROVED BY CLIENTS