+ 3
why error?
i think i lack the display? the System.out.print function display the result. but what symbols should i print? too dizzy https://code.sololearn.com/ctay6MOKDmnW/?ref=app
16 Answers
+ 4
Yes, code is okay.
You read the number n in main, give that to the sum function and print the value returned:
// in main after scanner close
System.out.print(sum(n));
+ 2
Japheth
Just print sum after taking input
System.out.print(sum(n));
+ 2
A͢J Ani Jona 🕊 huhuhu thank u both of you i actually consumed a really big amount of time researching here and there. thank you so much will take note of that
+ 2
Japheth
Method is used to perform a particular task so here sum method is performing a task to get total of all numbers from 1 to n
So here sum is a method which is accepting one parameter which is your input.
As sum method is returning total of 1 to n so we have to print that returned value.
You can either print by just calling method or you can store returned value to a new variable and then you can print that variable like this:
int sum = sum(n);
System.out.print(sum);
+ 1
10904647. Means. Hougp
0
you didn't use sum method to get the result to print it
0
M O H A M E D im sorry how exactly?
is the code okay?is the display is the only thing im lacking of?
System.out.print(sum); like how
0
you need tp pass the variable to sum method
0
sum take one int parameter
0
should i print the result in main method?
0
so (sum(n)) is basically giving the n(which is the input) to the sum method? like distributing the n to the sum method?
0
You have just to write System.out.println(sum(n));
0
You haven't called the sum function with the input as a parameter.
A great example as already mentioned quite a few times is:
System.out.print(sum(n)).
That way you're calling the sum function with your input as a parameter (in this case n). It does the calculations inside the sum function and prints the result.
Hope that helped!
0
Manav Roy the close function is to close the Scanner. If you don't close the Scanner then java won't garbage collect the Scanner and it might lead to memory leaks.
0
Blessings ere beneath cthullic embryoderyies