Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sum numbers 1 through 10

Posted on 2006-10-25
2
216 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 500 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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.

808 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