# Calculating value between two integers

Page 1 of 1

## 4 Replies - 2877 Views - Last Post: 09 October 2007 - 10:25 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=34997&amp;s=e8e30263cee07d063c40eb72bd9bcb9c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tcr071

Reputation: 0
• Posts: 5
• Joined: 09-October 07

# Calculating value between two integers

Posted 09 October 2007 - 07:52 PM

import java.util.Scanner;

public class Lab06Ex2
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);

int a;
int b;
int c;
int d = 1;

System.out.println("Enter a number:  ");
a = keyboard.nextInt();

System.out.println("Enter another number:  ");
b = keyboard.nextInt();

while (a < B)
{
a += 1;
a = a + b;
System.out.print( a );
}

I am trying to write a program that asks the operator for two integers. I need to find the sum of all the integers between and including the 2 entered. I have been playing with this since 7:30 and I cannot get it to work correctly at all.

Ex. Operator enters 2 and 9. The system adds 2 + 3 + 4 + 5 +6 +8 + 9 and displays the answer, 44.

I just learned the while, do-while, and for loop on monday so this stuff is kind of new to me.

I cannot figure out the write algorithm to get it to add the numbers together. This is driving me nuts, it never works.

...sorry if this is not allowed.

This post has been edited by tcr071: 09 October 2007 - 07:59 PM

Is This A Good Question/Topic? 0

## Replies To: Calculating value between two integers

### #2 Jayman

• Student of Life

Reputation: 415
• Posts: 9,532
• Joined: 26-December 05

## Re: Calculating value between two integers

Posted 09 October 2007 - 08:42 PM

You have one typo, should be small b in your condition of the while loop. You had the right idea with your logic, you just needed a third variable to store the answer.

int c = 0;

while (a <= b) // should be small b
{
c = c + a;
System.out.print( c );
a += 1;
}

### #3 tcr071

Reputation: 0
• Posts: 5
• Joined: 09-October 07

## Re: Calculating value between two integers

Posted 09 October 2007 - 08:52 PM

jayman9, on 9 Oct, 2007 - 08:42 PM, said:

You have one typo, should be small b in your condition of the while loop. You had the right idea with your logic, you just needed a third variable to store the answer.

int c = 0;

while (a <= b) // should be small b
{
c = c + a;
System.out.print( c );
a += 1;
}

When I try to implement this I get a humongous numeric value no matter what to numbers I input. 2 and 3 ends up at 25. I can't even figure out how 25 is being derived. This is what was implemented

import java.util.Scanner;

public class Lab06Ex2
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);

int a;
int b;
int c = 0;

System.out.println("Enter a number:  ");
a = keyboard.nextInt();

System.out.println("Enter another number:  ");
b = keyboard.nextInt();

while (a <= b)
{
c = c + a;
System.out.print( c );
a += 1;
}
}
}

### #4 spullen

• D.I.C Regular

Reputation: 10
• Posts: 356
• Joined: 22-March 07

## Re: Calculating value between two integers

Posted 09 October 2007 - 10:07 PM

Maybe it's cause you are printing it every time.
while (a <= b)
{
c = c + a;
System.out.print( c );
a += 1;
}

Try:
while(a <= b){
c = c + a;
a += 1;
}
System.out.println(c);

### #5 Jayman

• Student of Life

Reputation: 415
• Posts: 9,532
• Joined: 26-December 05

## Re: Calculating value between two integers

Posted 09 October 2007 - 10:25 PM

Yes, I wasn't sure of the output that you wanted with that statement there.

If you are going for a literal: 1 2 3 4 5 6 7 8 9 = 45
then you will need it in this manner.

while (a <= b)
{
c = c + a;
System.out.print( a + " ");
a += 1;
}
System.out.print("= " + c);
System.out.println();