Wrong code, bug or interpretation fails? (Help me --> Java While test) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Wrong code, bug or interpretation fails? (Help me --> Java While test)

It's the instruction: ============ Your math teacher asked you to calculate the sum of the numbers 1 to N, where N is a given number. -- Task: Take an integer N from input and output the sum of the numbers 1 to N, inclusive. Sample Input: 10 Sample Output: 55 It's my code: ============= import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int x = 0; do{ x++; } while(x < n); System.out.println(x); } } It's the result: ============ Input: 4 Your Output: 4 Expected Output: 10 I really have no idea about how to receive the "Expected output", it's not a VAR... It's an aleatory number.

8th Sep 2023, 2:40 AM
Sidnei Souza
Sidnei Souza - avatar
14 Answers
+ 2
// Bro this will work import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int x=0; int sum =0; do{ sum += x; x++; } while(x<=n); System.out.println(sum); } }
9th Sep 2023, 3:29 AM
Vinay
Vinay - avatar
+ 4
You are just incrementing value of x but not adding current value of x (1,2,3,4.....10) in another variable which might be 'total' or 'sum'
8th Sep 2023, 3:12 AM
A͢J
A͢J - avatar
+ 3
import java.util.*; class Main{ public static void main(String[] args){ Scanner scn=new Scanner(System.in); int x=scn.nextInt(); int n=1; int sum=0; do{ sum+=n; n++; }while(n<=x); System.out.println(sum); } }
9th Sep 2023, 5:39 PM
Vikas Pal
Vikas Pal - avatar
+ 2
Beta you are just incrementing x You need to initialise new variable Ex int sum = 0; sum = sum + x++;
8th Sep 2023, 4:02 AM
Vinay
Vinay - avatar
+ 2
And it should be like this While (x <= 10) This will add frim 0 to 10 but if you write While (x<10) it will add up to 9
8th Sep 2023, 10:32 AM
DENNIS OPOKU AMPONSAH
DENNIS OPOKU AMPONSAH - avatar
+ 2
Bro tomorrow I will solve for you
8th Sep 2023, 6:35 PM
Vinay
Vinay - avatar
+ 2
Try it bro it will work
9th Sep 2023, 5:39 PM
Vikas Pal
Vikas Pal - avatar
+ 1
Inside while loop compare x <= n
8th Sep 2023, 3:51 PM
Vinay
Vinay - avatar
0
Thank you so much, AJ, Vinay and Dennis. -- Unfortunately, even changing the code as you teach me i'm still gotting error :( ========================== CODE Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int x = n; int sum = 0; do{ sum = sum + x++; } while(sum <= x); System.out.println(sum); ========================== ERROR ------- Test Case 1 Input: 4 Your Output: 9 Expected Output: 10 ------- Test Case 2 Input: 42 Your Output: 85 Expected Output: 903 -------------------------------- Any suggestions :?
8th Sep 2023, 3:21 PM
Sidnei Souza
Sidnei Souza - avatar
0
Thank you so much Vinay, it doesn't work. Anyway, I will study a litle more before back to this test. I understood my error and your vision to help me but there is a bug to accept my code.
8th Sep 2023, 6:23 PM
Sidnei Souza
Sidnei Souza - avatar
0
Thank you Vinay, it really works! ^^ I needs to understand a litle more about this kind of problem :( Thank you so much!!
9th Sep 2023, 12:04 PM
Sidnei Souza
Sidnei Souza - avatar
0
Thank you Vikas, now I have learned one more ✌️😊
9th Sep 2023, 5:42 PM
Sidnei Souza
Sidnei Souza - avatar
0
Follow us
9th Sep 2023, 6:06 PM
Vikas Pal
Vikas Pal - avatar
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); { int num = sc.nextInt(), i = 1, sum = 0; while(i <= num) { sum += i; i++; } System.out.println(sum); } } } Working 100%
27th Feb 2024, 6:11 PM
Tony