Java [Java] Did I write this program correctly? Please check

  • Thread starter Thread starter Math10
  • Start date Start date
  • Tags Tags
    Java Program
AI Thread Summary
The discussion centers around a Java program intended to calculate the sum of numbers from 1 to 10. The initial code provided incorrectly attempts to print the sum within a loop without actually calculating it. The output merely repeats a phrase ten times without yielding the correct sum. Participants highlight the need for an additional variable to store the cumulative total and suggest initializing it to zero, then adding the loop counter to this variable during each iteration. After the loop, the final sum should be printed. There is also an emphasis on the importance of running the program to identify errors before seeking help.
Math10
Messages
301
Reaction score
0
Write a Java program to find the sum of all numbers between 1 to 10.

So we all know that 1+2+3+4+5+6+7+8+9+10=55 and that answer to this problem is 55. Please check my work below:

Java:
public class Standard
{
       public static void main(String[] args)
       {
                // Create a for loop.
                for (int x=1; x<=10; x=x+1)
                {
                      // Print result.
                      System.out.println("Sum of numbers from 1 to 10 is" x=x+1);
                }
        }
}

Did I wrote this program correctly? If not, then please correct me. Thanks.
 
Last edited by a moderator:
Technology news on Phys.org
Have you actually tried it?, that's the only real test.
It doesn't look right to me though.
My experience is in C type languages, not Java, but it looks like this will simply loop 10 times and give you a number on each pass.
'x' is your loop counter variable, you have no other variable in which the final result is being computed.
 
I tried it and it didn't work. Like you said, it looped 10 times like this:

Sum of numbers from 1 to 10 is
Sum of numbers from 1 to 10 is
'' "
" "
For 10 times.
 
Yeah, I was wondering if it would even print a number.
I'm not going to do this for you but the general idea is that you need another variable, 't', I guess, for total will do.
Start that variable at zero and add the value of x to it each time you go the through the loop.
When the loop has been completed, then you print the value of t.
That should be enough to get you going in the right direction.
 
Math10 said:
Did I wrote this program correctly? If not, then please correct me. Thanks.
To add to what rootone said, before you post questions like this, run your program and see what it produces. If it doesn't run for some reason, tell us about that, including error messages.
 
Whoops, moved it down one too many, thanks rootone.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top