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
#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;
}
Comments
Leave a comment