# Printing out numbers that were looped.

Page 1 of 1

## 6 Replies - 995 Views - Last Post: 08 October 2012 - 01:56 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294473&amp;s=82998cb637767e9faf33f0a9e9255766&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Biscuit Tickler

Reputation: 0
• Posts: 13
• Joined: 01-October 12

# Printing out numbers that were looped.

Posted 05 October 2012 - 08:21 PM

I am supposed to make a program that lets the user input a small number and a large number and lists the numbers in between and then finds the sum and average. For example you would input 5 and 8. It should print out 5,6,7,8 and then on a new line it should say the sum of 5,6,7,8, is 26. On another line it should say "The average of 5,6,7,8, is 6.5. I have no problem with the loop, that works fine but how would I go about printing the numbers that are looped like the 5-8.
```import java.io.*;
import java.util.*;
public class SummingNumbers
{
public static void main (String[] args)
{
int sum = 0;
Scanner value = new Scanner(System.in);
System.out.print("Enter starting value: ");
int x = value.nextInt();
System.out.print("Enter ending value: ");
int y = value.nextInt();

for (; x<=y; x++)  //explain how this is working with just ; and no variable/number before it.
{

System.out.println(x);
sum += x;

}

System.out.println();

}
}

```

I had something in the System.out.println(); but deleted the variable and forgot to delete the line. Disregard that.

Is This A Good Question/Topic? 0

## Replies To: Printing out numbers that were looped.

### #2 Kinaces

Reputation: 78
• Posts: 230
• Joined: 04-October 12

## Re: Printing out numbers that were looped.

Posted 05 October 2012 - 08:29 PM

You do not actually want to use x in your for loop.
Since x is your min value you don't want to change it. In your for loop you are increasing x.

Anytime you want to print the numbers from x to y just create a loop.
Create a new int (EX: int i = x) in the for loop declaration and set is to x. Loop until Y. Then print out that variable in the for loop.

Now for the reason your for loop is working is because the first thing you set in a for loop is if you want to create a variable specifically for the for loop. Since you are using a pre-existing variable you do not have to have that first declaration.

### #3 Biscuit Tickler

Reputation: 0
• Posts: 13
• Joined: 01-October 12

## Re: Printing out numbers that were looped.

Posted 05 October 2012 - 09:03 PM

Kinaces, on 05 October 2012 - 08:29 PM, said:

You do not actually want to use x in your for loop.
Since x is your min value you don't want to change it. In your for loop you are increasing x.

Anytime you want to print the numbers from x to y just create a loop.
Create a new int (EX: int i = x) in the for loop declaration and set is to x. Loop until Y. Then print out that variable in the for loop.

Now for the reason your for loop is working is because the first thing you set in a for loop is if you want to create a variable specifically for the for loop. Since you are using a pre-existing variable you do not have to have that first declaration.

Ok, I fixed the for loop and now I need to figure out how to print the numbers that were looped.

### #4 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Printing out numbers that were looped.

Posted 06 October 2012 - 02:16 AM

What don't you understand about printing? A good start would to add a print statement to the loop that prints the number each time through the loop. From there, it's getting the format right.

### #5 Biscuit Tickler

Reputation: 0
• Posts: 13
• Joined: 01-October 12

## Re: Printing out numbers that were looped.

Posted 06 October 2012 - 08:43 AM

I had a friend help me. I had to use a string that held all of the numbers in the loop. Here is the code.
```import java.io.*;
import java.util.*;
public class SummingNumbers
{
public static void main (String[] args)
{
int totalValue = 0;
double totalNum = 0;
String temp = ""; //was forced to use strings because I couldn't think of any other way to do this program.
Scanner value = new Scanner(System.in);
System.out.print("Enter starting value: ");
int x = value.nextInt();

System.out.print("Enter ending value: ");
int y = value.nextInt();
System.out.println(" ");

for (int i = x; i<=y; i++) //renames x because x is already defined.
{
temp +=i + ","; //formats the looped number to have a comma beside of it when calculating the sum below.
System.out.println(i);

totalValue += i;
totalNum ++;

}

System.out.println("The sum of " + temp + " is " + totalValue);

System.out.println("The average of " + temp + " is " + answer);
}
}

```

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Printing out numbers that were looped.

Posted 08 October 2012 - 12:29 PM

Why not simply

```  for (int i = x; i<=y; i++) {
System.out.print(" " + i);
totalValue += i;

```

as far as totalNum is concerned no need to compute it in the loop. It is simply y - x + 1

### #7 rfs02

Reputation: 26
• Posts: 70
• Joined: 30-September 12

## Re: Printing out numbers that were looped.

Posted 08 October 2012 - 01:56 PM

Since these are guaranteed to be sequential numbers:

• Number of integers = max - min + 1
• Average of integers = (max + min) / 2
• Sum of all integers = Number of integers * Average

• totalNum = y - x + 1;
• answer = ((double)(y + x)/2);
• totalValue = totalNum * answer

No need to calculate anything in the loop, just print.

This post has been edited by rfs02: 08 October 2012 - 01:57 PM