1. Member Join Date
Apr 2010
Posts
2
Rep Power
0

## Help with loop

I have been given this assignment:
This method takes a range (i.e., the starting number and the ending number) and a skip number. The method computes the sum of the numbers in the range excluding all numbers that are multiples of the skip number. Note that both start and end are part of the computed sum, unless they are multiples of the skip number. For instance, given input 21, 25 and 4, the method returns 91 (i.e., 21 + 22 + 23 + 25 = 91).

The following is the method prototype for the method:
int rangeHash(int start, int end, int skip)

If end is smaller than start, or if start or end is a negative number, the method signals an error condition by returning -1. Also, whenever skip is less than or equal to 0, the method signals an error condition by returning -1.

The code I have written:

import java.util.Scanner;

public class rangeHash
{
public static void main (String[]args)
{
int start, end, skip;

Scanner keyboard = new Scanner(System.in);

start= keyboard.nextInt();
end = keyboard.nextInt();
skip = keyboard.nextInt();
System.out.println(rangeHash(start, end, skip));

}

public static long rangeHash(int start, int end, int skip)

if ((start>end)||(start<0)||(end<0)||(skip<=0))

else
while (start <= end)
{
if (start%skip == 0)
{
start++;
System.out.println(start);
}
else
start++;
System.out.println(start);
}

}

}

The program compiles fine the only problem is when it hits the first instance where the number is divisible by the skip it stops adding any numbers, even if it is not divisible. Can anyone see what I am doing wrong?I am an absolute beginner.:confused:  Reply With Quote

2. ## % is the remainder operator...  Reply With Quote

3. ## You need to add/change some debug statements to your code to see what is happening.
For example put id strings in ALL of the println()s so you can see which are outputing what. For example:
Java Code:
`           System.out.println("start=" + start + " skipping summing");`
Your logic is basically correct, just one statement too many.

Also for quicker debugging replace the scanner input with fixed input until you get the logic worked out. For example:
Java Code:
```   start = 21; //keyboard.nextInt();
end = 25; //keyboard.nextInt();
skip = 4; //keyboard.nextInt();```  Reply With Quote

4. Member Join Date
May 2010
Posts
8
Rep Power
0

## Norm is right. Your incrementing your start variable one too many times. Looks like you should be incrementing start no matter what happens with the modulus operation. Also, you could clean up the code a little bit by saying

Java Code:
```if (start % skip != 0)

Java Code:
```if (start%skip == 0)
{
//Do nothing
}
else  Reply With Quote

5. Member Join Date
Apr 2010
Posts
2
Rep Power
0

## Hi. Thanks for the reply, I will give it a try again when I get off work and let you know how it went.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•