by CodeChum Admin
We've been giving positive numbers too much attention lately, it's time to let negative numbers take the spotlight this time!
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
#include <stdio.h>
int main(){
float number1,number2,number3,number4,sum=0;
printf("Enter the first number: ");
scanf("%f",&number1);
printf("Enter the second number: ");
scanf("%f",&number2);
printf("Enter the third number: ");
scanf("%f",&number3);
printf("Enter the fourth number: ");
scanf("%f",&number4);
if(number1<0){
sum+=number1;
}
if(number2<0){
sum+=number2;
}
if(number3<0){
sum+=number3;
}
if(number4<0){
sum+=number4;
}
printf("Sum of all negatives = %.2f\n\n",sum);
getchar();
getchar();
return 0;
}
Comments
Leave a comment