0

I want my program that to accept user number input and output the sum from 1 up to the input number (using while loop). Example: If input value is 4, the sum is 10 i.e., 1 + 2 + 3 + 4.

My code compiles but returns a never ending 1 until my jcreator stops responding.

import java.util.Scanner;
import java.io.*;

    public class SumLoopWhile {

    public static void main(String[] args) {
    int number;
    int sum = 1;
    Scanner in = new Scanner (System.in);
    System.out.println("Enter number: ");
    number = in.nextInt();

    while (sum <= 10) {
        System.out.println("Sum is: " + sum);
        number++;
        }
    }
}
3
  • 1
    You don't alter the value of sum at all in your while loop. The way you have it now, it's an infinite loop. Commented Sep 10, 2015 at 1:17
  • Kindly show me how to do it? @UnknownOctopus Commented Sep 10, 2015 at 1:18
  • why are you doing while (sum <= 10)? That makes zero sense to me. Commented Sep 10, 2015 at 1:24

5 Answers 5

1

You should be comparing the value to the number that was input, and adding to the sum. Finally, display the result after the loop. Something like

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter number: ");
    int number = in.nextInt();
    int sum = 0;
    int v = 0;
    while (v <= number) {
        sum += v;
        v++;
    }
    System.out.println("Sum is: " + sum);
}

Which will print Sum is: 10 when the input is 4 (as requested).

Sign up to request clarification or add additional context in comments.

Comments

0

I guess that what you want it's to modify the sum inside your while loop. Do it like this:

while (sum <= 10) {
    sum = sum + number;
    number++;
    }
}

System.out.println("Sum is: " + sum);

You have to put your System.out.println out of the loop because you want to print the total value, not each sum that it's calculated in each iteration.

Also, it should be nice that when you want to initialize some int that will be a "total" variable (like the result of a sum, rest or whatever), initialize it to zero.

int sum = 0;

1 Comment

It's working but when I entered 4, the output should be 10 that is 1+2+3+4 @Error404
0

Your output variable sum is having the same value throughout the program.it is not getting altered.the while loop becomes infinite loop

The Condition (sum <= 10) never becomes true and the while loop will run for infinite times

Comments

0
import javax.swing.JOptionPane;

public class SumUsingWhileLoop {

    public static void main(String[] args) {
        String input;
        input = JOptionPane.showInputDialog("Input the number:");
        int number;
        number = Integer.parseInt(input);
        int sum = 0;
        int i = 1;
        while (i <= number) {
            sum += i;
            i++;
        }

        JOptionPane.showMessageDialog(null, "Sum = " + sum);
        System.exit(0);
   }

}

Comments

0

The System.out.println should not be placed inside the while loop, because it will get executed as many times as the loop.If you want to print the sum value only once, then the statement System.out.println must be placed outside the loop block.

Just use another variable for counting the iterations.For example

while(count<=number)
{
    sum=sum+count;
    count++
}
System.out.println("Sum is: "+ sum);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.