i am trying to make a program which display the sum of numbers only if user write a positive number using only while loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

i am trying to make a program which display the sum of numbers only if user write a positive number using only while loop

the program take any number of numbers from the user and store the sum of them and display the sum only if the user enter a negative number in my case the output is the sum but added to the negative number https://onlinegdb.com/G6FMvUN70

30th May 2021, 4:25 PM
Mahmoud Hamed
Mahmoud Hamed - avatar
2 Answers
+ 1
int sum = 0; Scanner sc = new Scanner(System.in); input = sc.nextInt(); while (input>=0) { sum += input; input = sc.nextInt(); } System.out.println(sum);
30th May 2021, 4:40 PM
Eashan Morajkar
Eashan Morajkar - avatar
+ 1
i found the bug and here's the right code https://onlinegdb.com/PqU6Vu4Zb
30th May 2021, 4:36 PM
Mahmoud Hamed
Mahmoud Hamed - avatar