Answer to Question #326246 in C for nile2020

Question #326246

6. Negative Decimal Tally

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:

  1. Input four float numbers; they could be positive or negative.
  2. Only add all inputted negative numbers, and print its sum, up to 2 decimal place.
  3. 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
1
Expert's answer
2022-04-11T07:44:13-0400




#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;
}

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
New on Blog
APPROVED BY CLIENTS