?
Solved

Sum numbers 1 through 10

Posted on 2006-10-25
2
Medium Priority
?
225 Views
Last Modified: 2010-03-31
I can't quite get the running total correct. I am supposed to sum the numbers 1 through ten and print the result.
My numbers are off.


public class Sum
{
      public static void main(String args[])
      {
            int total = 2;
      
            for(int i = 1; i <= 10; i++)
            {
                  total += i;
                  System.out.printf("i = %d\n",i);      
                  System.out.printf("total= %d\n", total);      
            }
            
      }      
}

Any help would be greatly appreciated.
0
Comment
Question by:deekakes
2 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 2000 total points
ID: 17805045
hi

i believe the error is in
int total = 2;
it should be
int total = 0;


momi sabag
0
 

Author Comment

by:deekakes
ID: 17805084
Thank you so much...I fell asleep on that one!!!!!!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question