nstructions:
Input four float numbers; they could be positive or negative.
Only add all inputted negative numbers, and print its sum, up to 2 decimal place.
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
#include <stdio.h>
int main()
{
float a, b, c, d, sum = 0;
printf("Enter the first number: ");
scanf("%f", &a);
if(a < 0)
{
sum += a;
}
printf("Enter the second number: ");
scanf("%f", &b);
if(b < 0)
{
sum += b;
}
printf("Enter the third number: ");
scanf("%f", &c);
if(c < 0)
{
sum += c;
}
printf("Enter the fourth number: ");
scanf("%f", &d);
if(d < 0)
{
sum += d;
}
printf("Sum of all negatives = %.2f \n", sum);
return 0;
}
Comments
Leave a comment