Tristan Coal-Crescent
Tristan Coal-Crescent

Reputation: 29

Writing a program using While Loop

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++;
        }
    }
}

Upvotes: 0

Views: 203

Answers (5)

Balahari
Balahari

Reputation: 7

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);

Upvotes: 0

ASHOK DUMARU
ASHOK DUMARU

Reputation: 1

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);
   }

}

Upvotes: 0

x_coder
x_coder

Reputation: 73

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

Upvotes: 0

Francisco Romero
Francisco Romero

Reputation: 13199

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;

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

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).

Upvotes: 1

Related Questions