+ 1
Adding the positive numbers
I am to make a program that only add the positive whole numbers. It should ignore the negative numbers and decimal numbers. For example, Input: 1 -3 6.5 4 Expected Output: 5 Please help, below is my code: https://code.sololearn.com/c4A21a196a2A
6 Answers
+ 3
Evan Fernandez You can test your input example in my solution and analize this code. If you can't understand something then feel free to ask me.
https://code.sololearn.com/cETFS88xS3cU/?ref=app
+ 3
Yes we can 🅰🅹 🅐🅝🅐🅝🅣 . It‘s good idea but is it not to much for a beginner?
+ 2
Jayakrishna🇮🇳
JaScript
Evan Fernandez
We can use regular expression to ignore negative and decimal values
https://code.sololearn.com/cIxgLAUi3YKj/?ref=app
+ 1
Using of double type will work here. You can't take input of double type by nextInt(). nextDouble() will work.
n%1==0 will clarify that it don't have decimal point value... Try this way....
+ 1
Evan Fernandez
No need to store input in an array if you are taking input and adding inside same loop.
You can just do like this:
for (int i =0; i <4; i++){
// my_array[i] = input.nextInt();
int num = input.nextInt();
if(num > 0) {
sum += num;
}
}
+ 1
🅰🅹 🅐🅝🅐🅝🅣 input have decimal values..!
also saying it should be ignored.