Welcome to Dream.In.Code
Become a Java Expert!

Join 149,577 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,734 people online right now. Registration is fast and FREE... Join Now!




Calculating value between two integers

 
Reply to this topicStart new topic

Calculating value between two integers, I am trying to calculate the sum of all values between 2 integers

tcr071
9 Oct, 2007 - 06:52 PM
Post #1

New D.I.C Head
*

Joined: 9 Oct, 2007
Posts: 5


My Contributions
CODE
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: 9 Oct, 2007 - 06:59 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Calculating Value Between Two Integers
9 Oct, 2007 - 07:42 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,306



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
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.

CODE

        int c = 0;


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

User is online!Profile CardPM
+Quote Post

tcr071
RE: Calculating Value Between Two Integers
9 Oct, 2007 - 07:52 PM
Post #3

New D.I.C Head
*

Joined: 9 Oct, 2007
Posts: 5


My Contributions
QUOTE(jayman9 @ 9 Oct, 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.

CODE

        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

CODE

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



User is offlineProfile CardPM
+Quote Post

spullen
RE: Calculating Value Between Two Integers
9 Oct, 2007 - 09:07 PM
Post #4

D.I.C Regular
Group Icon

Joined: 22 Mar, 2007
Posts: 330



Thanked: 1 times
Dream Kudos: 50
My Contributions
Maybe it's cause you are printing it every time.
CODE

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

Try:
CODE

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

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Calculating Value Between Two Integers
9 Oct, 2007 - 09:25 PM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,306



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
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.

CODE

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

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:35PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month