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