Calculating value between two integers

I am trying to calculate the sum of all values between 2 integers

Page 1 of 1

4 Replies - 2877 Views - Last Post: 09 October 2007 - 10:25 PM Rate Topic: -----

#1 tcr071  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • 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;		
		}


Was This Post Helpful? 0
  • +
  • -

#3 tcr071  Icon User is offline

  • New D.I.C Head

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

Re: Calculating value between two integers

Posted 09 October 2007 - 08:52 PM

View Postjayman9, 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;		
		}	
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 spullen  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • 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);


Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • 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(); 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1