Solved

Sum numbers 1 through 10

Posted on 2006-10-25
2
214 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now