+ 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

16th Jun 2021, 5:20 PM
Eka
Eka - avatar
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
16th Jun 2021, 6:30 PM
JaScript
JaScript - avatar
+ 3
Yes we can 🅰🅹 🅐🅝🅐🅝🅣 . It‘s good idea but is it not to much for a beginner?
16th Jun 2021, 9:17 PM
JaScript
JaScript - avatar
+ 2
Jayakrishna🇮🇳 JaScript Evan Fernandez We can use regular expression to ignore negative and decimal values https://code.sololearn.com/cIxgLAUi3YKj/?ref=app
16th Jun 2021, 8:17 PM
A͢J
A͢J - avatar
+ 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....
16th Jun 2021, 5:52 PM
Jayakrishna 🇮🇳
+ 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; } }
16th Jun 2021, 7:56 PM
A͢J
A͢J - avatar
+ 1
🅰🅹 🅐🅝🅐🅝🅣 input have decimal values..! also saying it should be ignored.
16th Jun 2021, 7:59 PM
Jayakrishna 🇮🇳