0

How do I add the negative float numbers only in C? Using only if statements?

The problem goes like this; 1. Ask 4 inputs (float) 2. Only add all inputted negative numbers and print the sum in 2 decimal place. The code I have tried but get a 6/10 score. https://code.sololearn.com/c0jYcJpGuFz9/?ref=app

11th Dec 2021, 7:40 AM
Tairitsu
Tairitsu - avatar
6 Respostas
+ 2
The code is bloated. You could substitute all your if statements for only 4 if-statements. Here is an example for one of them: if(num1<0) { sum += num1; } Ipang it sounds like a restriction to not use any loops. Maybe an if-lesson?
11th Dec 2021, 7:58 AM
Alex
Alex - avatar
+ 2
Have you learned how to use an array to store multiple inputs? this would've been easier using an array, with a loop and an `if` conditional. You can also do this using a loop, without array. You just need to set the array to run 4 times, read the input in the loop, and use `if` conditional to add only negative inputs.
11th Dec 2021, 7:54 AM
Ipang
+ 2
Alex, I don't know for sure, but the scoring 6/10 mentioned made it look less like a lesson quiz ...
11th Dec 2021, 8:04 AM
Ipang
+ 2
Ipang yes its a lesson quiz. I was still wondering how to do it in if statements. since my teacher just skip teaching us about it for switch cases...
11th Dec 2021, 8:17 AM
Tairitsu
Tairitsu - avatar
+ 2
Oh okay, in that case, you can follow Alex's suggestion 👍
11th Dec 2021, 8:19 AM
Ipang
+ 2
thanks!! I think I understand it now
11th Dec 2021, 8:28 AM
Tairitsu
Tairitsu - avatar