Write a Java program to find the sum of all numbers between 1 to 10.
@e.mccormick
What have you done so far? What do you know about loops?
Overview of loops: http://www.tutorialspoint.com/java/java_loop_control.htm
So this is what I have so far and please check if it's right: public class Math { 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" +Sum); } } }
I would not call something math because there is a system math class. And no, that is not correct. You are not actually summing things up.
Then how would you write this program?
Look at the body of your loop. System.out.println("Sum of numbers from 1 to 10 is" +Sum); OK. so what is Sum? Where did you define it? How are you adding to it. And if the goal is to print out just the sum, would that not come after the loop?
Join our real-time social learning platform and learn together with your friends!