Solved

Sum Of Numbers

Posted on 2009-07-01
2
1,760 Views
Last Modified: 2013-12-14
Im trying to write a program that asks the user for a positive nonzero integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user entered a 50, the loop will find the sum of 1,2,3,4,....50.

i think the code i posted may be wrong but ive spent hours trying to figure it out and my teacher isnt helpful at all. :(

i really would appreciate it if a full solution code is posted. This will help me study for my other programs im having trouble with.
public class SumOfNumbers

{

        public static void main( String [] args)

        {

                

            double nonZero;

            double numbers;

            double totalNumbers;

            String input;

                

        

            for( int count = 1; count <= nonZero; count++ )

            {

                input = JOptionPane.showInputDialog(null, "Please enter a nonzero positive integer.");

 

              nonZero = Double.parseDouble(input);

 

            }   

                

            JOptionPane.showMessageDialog(null, "The sum of the integers is: " + nonZero);

        }

}

Open in new window

0
Comment
Question by:AgentC4
2 Comments
 
LVL 5

Accepted Solution

by:
Opalelement earned 500 total points
Comment Utility
Not sure if this will work since I don't have a java environment to test in ATM, but it has comments so you can see what it is supposed to do.
public class SumOfNumbers

{

        public static void main( String [] args)

        {

            double number; // The entered number after it is parsed

            double totalNumber = 0; // Total after adding

            String input; // Input from the input dialog

                

            input = JOptionPane.showInputDialog(null, "Please enter a nonzero positive integer."); // Ask for the number

            number = Double.parseDouble(input); // change it to a number

            for( int count = 1; count <= number; count++ )

            {

              totalNumber += count; // Loop through and keep adding the numbers

            }   

                

            JOptionPane.showMessageDialog(null, "The sum of the integers is: " + totalNumber); // Show the final

        }

}

Open in new window

0
 

Author Closing Comment

by:AgentC4
Comment Utility
You're a life saver, thank you so much yes this works perfectly. ill definitely study it and work more and do examples.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

744 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